我有一个面板,其中包含一个水平框,框内有 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/