Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在框布局的开头添加按钮?_Java_Swing_Boxlayout - Fatal编程技术网

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

Java 如何在框布局的开头添加按钮?,java,swing,boxlayout,Java,Swing,Boxlayout,我有一个面板,其中包含一个水平框,框内有2个按钮。 现在我扩展了这个类,希望在框的开头添加新按钮。 我试着在盒子的末端加上一个按钮 有人知道怎么做吗? private class MyBoxPanel extends BoxPanel { public JButton btnPrint; public MyConfirmationPanel() { btnPrint = new JButton("print");

我有一个面板,其中包含一个水平框,框内有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();
        }
    }
我试着在盒子的末端加上一个按钮

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

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

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

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

>我试着在盒子的末端加上一个按钮最后一个JComponent添加到(ZOrder)1的末尾。您必须获取componentat(int),2。添加jbutton3。使用当前JCmoponet移动到索引+1,4。然后,若要调用重新验证和重新绘制,请使用此面板创建一个具有边框布局的面板,并设置中间和东部的2个初始按钮,添加3个西部按钮?在MyBoxPanel中,我可以添加任何我想要的内容,但在BoxPanel中,我不允许更改或添加任何内容。