2014-01-24 21 views
0

我有一个面板,其中包含一个水平框内的框有2个按钮。 现在我扩展这个类,并希望在框的开头添加新的按钮。 我试着addes按钮到盒子的尽头。如何在框布局开始处添加按钮?

任何机构都知道该怎么做呢?

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(); 
     } 
    } 
+2

>我试图就将此按钮框的末尾。 - 将最后一个JComponent添加到(ZOrder)的末尾,1.您必须getComponentAt(int),2.添加JButton 3.将当前JCmoponet移动到索引+1,4,然后调用revalidate并重新绘制 – mKorbel

+1

你用这个面板创建了一个带有borderlayout的面板,并且设置了2个初始按钮的中心和东部,向西增加了3个? – Fabiotocchi

+0

在MyBoxPanel中,我可以添加任何我想要的东西,但在BoxPanel中,我不允许更改或添加任何东西。 – michdraft

回答

1

什么,我试图就将此按钮框的末尾。

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

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

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

panel.revalidate(); 
panel.repaint(); 
相关问题