我有一个面板,其中包含一个水平框,其中有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();
}
}
答案 0 :(得分:1)
我尝试将按钮添加到框的末尾。
是的,add(component)
方法只是将组件添加到容器的末尾。
如果要将组件添加到开头,则需要指定索引值为0.请阅读Container API以获取相应的方法。我不记得是add(component, index)
还是add(index, component)
。
然后,一旦添加了需要调用的组件
panel.revalidate();
panel.repaint();