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