2010-01-06 67 views
0

我正在开发Java桌面应用程序。在GUI中,我希望该用户可以根据需要动态添加任意数量的工具栏。为了实现这一点,以下是我已经做过的事情:在Java Swing GUI中动态添加工具栏

  • 采取了mainPanel中并设置其布局的BorderLayout
  • 然后采取topPanel,并把它添加到mainPanel中的BorderLayout.NORTH
  • 设置topPanel的布局,BoxLayout的
  • 然后采取5个板命名toolbar1Panel,toolbar2Panel,...
  • Afterthat,增加了一个工具栏,每个在前面的步骤中创建的toolbarPanel的。
  • 现在有一个名为其上又添加到topPanel的“toolbar1Panel补充说:”第一个工具栏上的“添加”按钮仅添加了一个toolbarPanel即toolbar1Panel在topPanel

现在我已经实现了上面的“添加”按钮“的actionPerformed()”方法如下:

// to add second toolbar Panel to the topPanel dynamically 
topPanel.add(toolbar2Panel); 

但问题是,它不工作。意味着没有工具栏被添加到topPanel。

有什么我失踪的。

代码是Netbeans生成的,所以我认为它只会给别人添加混乱,这就是为什么我没有在这里粘贴任何代码。

+0

什么是对topPanel布局管理器? – 2010-01-06 16:33:59

+0

很高兴看到更多的代码。 – Poindexter 2010-01-06 16:34:20

+0

您是否已将toolbar1Panel ...添加到顶部面板? – 2010-01-06 16:34:36

回答

3

向BoxLayout添加另一个工具栏后,您可能需要(re | in)?验证面板。

我已经重复这样做了,但我无法理解3个左右方法调用的逻辑;所以我只是尝试,直到我打的作品之一:

topPanel.validate(); 
topPanel.invalidate(); 
topPanel.revalidate(); 
topPanel.layout(); 

(至少)其中之一应该强迫你的GUI重新计算其布局,使北面板更大,因此表现出第二(和连续的)工具栏。

+0

万岁!它正在工作。 非常感谢你。自从早上我一直在尝试这件事。你已经在我的所有3个职位中给出答案。非常感谢。 – 2010-01-06 17:00:46

+0

非常好!很高兴听到我能够帮助。你最终使用哪个验证系列? – 2010-01-06 17:42:47

+0

我用了revalidate() – 2010-01-07 08:22:06

2

没有指定顶部面板的布局,它可能会假定不正确的布局。

向它添加两个工具栏面板可能只是将第一个替换为第二个,或者忽略第二个。

只是为了测试,将topPanel的布局设置为FlowLayout并重试。

+0

其实我已经将topPanel的布局添加为BoxLayout。我忘了写在上面的描述。它也不是以这种布局运行的。 – 2010-01-06 16:43:27

0

我认为你在测试之前试图做的太多。我会这样做的方式是从一些非常简单的事情开始,例如一个面板,一个静态标签。如果按照您的预期显示,请使用菜单项添加工具栏。那样有用吗。然后肆意添加件。

很可能你会遇到一个简单案例的问题,然后可以弄清楚,或者你会有一个简单的案例在这里发布。

交替地,作为开始点从网络pinck一些工作示例。剪下来,然后建立你的情况。

0

它有帮助吗? 你想达到什么?

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JToolBar; 
import javax.swing.SwingUtilities; 

public class AddingToolbars { 
    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable(){ 

      public void run() { 
       AddingToolbars me = new AddingToolbars(); 
       me.initGui(); 

      } 

     }); 

    } 

    private JPanel topPanel; 
    private JPanel mainPanel; 
    private JFrame frame; 

    private void initGui() { 
     frame = new JFrame(); 

     mainPanel = new JPanel(new BorderLayout()); 
     frame.setContentPane(mainPanel); 

     topPanel = new JPanel(); 
     BoxLayout bLayout = new BoxLayout(topPanel,BoxLayout.Y_AXIS); 
     topPanel.setLayout(bLayout); 
     mainPanel.add(topPanel,BorderLayout.NORTH); 

     JButton addButton = new JButton("Add toolbar"); 
     mainPanel.add(addButton,BorderLayout.CENTER); 

     addButton.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e) { 
       addNewToolBar(); 
      } 

     }); 

     frame.setSize(500,500); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

    } 

    protected void addNewToolBar() { 
     JToolBar tb = new JToolBar(); 
     tb.add(new JButton("b1")); 
     tb.add(new JButton("b2")); 
     tb.add(new JButton("b3")); 

     topPanel.add(tb); 
     mainPanel.validate(); 
    } 
}