java - 如何在框布局的开头添加按钮?

标签 java swing boxlayout

我有一个面板,其中包含一个水平框,框内有 2 个按钮。 现在我扩展了这个类,并想在框的开头添加新按钮。 我尝试将按钮添加到框的末尾。

有人知道怎么做吗?

 private class MyBoxPanel extends BoxPanel {
         public JButton btnPrint;
        public MyConfirmationPanel() {
            btnPrint = new JButton("print");
            add(btnPrint);
            add(Box.createRigidArea(new Dimension(5, 0)));
        }

        protected void confirmActionPerformed(ActionEvent e) {
            for (PrinterInputListener listener : listeners)
                listener.printConfirmed(printerPanel.getPrint().getId());
        }

        protected void cancelActionPerformed(ActionEvent e) {
            for (PrinterInputListener listener : listeners)
                listener.printCancelled();
        }
    }

最佳答案

What i tried addes button to the end of box.

是的,add(component) 方法只是将组件添加到容器的末尾。

如果要将组件添加到开头,则需要指定索引值 0。请阅读容器 API 以获取适当的方法。我不记得它是 add(component, index) 还是 add(index, component)

然后,一旦添加了需要调用的组件

panel.revalidate();
panel.repaint();

关于java - 如何在框布局的开头添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21328215/

相关文章:

java - 排序 Java 多维数组

java - 如何处理 Selenium 中的多个警报弹出确认?

java - Libgdx 上的游标输入返回 "incorrectly"

Java BoxLayout 使用全宽

java - 如何使已受 BoxLayout 影响的标签居中?

java - 执行 JSF 导航时,不会调用映射到前向调度程序的过滤器

java - 如果不实现 Serializable 接口(interface),是否可以使类在 Java 中持久化?

java - JLabel 未向左对齐

java - 在 JTable 中用鼠标移动行

java - JPanel 调整大小显示另一个 JPanel