在JavaSwingGUI中动态添加工具栏
我正在开发一个Java桌面应用程序。在GUI中,我希望用户可以根据需要动态添加任意多的工具栏。为了实现这一点,我已经做了以下几件事:在JavaSwingGUI中动态添加工具栏,java,user-interface,swing,Java,User Interface,Swing,我正在开发一个Java桌面应用程序。在GUI中,我希望用户可以根据需要动态添加任意多的工具栏。为了实现这一点,我已经做了以下几件事: 已获取主面板并将其布局设置为BorderLayout 然后取一个topPanel并将其添加到主面板的BorderLayout.NORTH 将topPanel的布局设置为BoxLayout 然后取5个面板,分别命名为toolbar1Panel、toolbar2Panel、 之后,在前面步骤中创建的每个工具栏面板中添加一个工具栏 仅添加了一个toolbarPanel
- 已获取主面板并将其布局设置为BorderLayout
- 然后取一个topPanel并将其添加到主面板的BorderLayout.NORTH
- 将topPanel的布局设置为BoxLayout
- 然后取5个面板,分别命名为toolbar1Panel、toolbar2Panel、
- 之后,在前面步骤中创建的每个工具栏面板中添加一个工具栏
- 仅添加了一个toolbarPanel,即topPanel上的toolbar1Panel
// to add second toolbar Panel to the topPanel dynamically
topPanel.add(toolbar2Panel);
但问题是它不起作用。表示没有向topPanel添加工具栏
有什么我遗漏的吗
代码是Netbeans生成的,因此我认为它只会给其他人带来混乱,这就是为什么我没有在这里粘贴任何代码。如果没有指定顶部面板的布局,它可能会假定错误的布局 向其中添加两个工具栏面板可能只是将第一个工具栏面板替换为第二个工具栏面板,或者忽略第二个工具栏面板
仅用于测试,请将topPanel的布局设置为FlowLayout,然后重试。如果未指定顶部面板的布局,则可能假定该布局不正确 向其中添加两个工具栏面板可能只是将第一个工具栏面板替换为第二个工具栏面板,或者忽略第二个工具栏面板
仅用于测试,请将topPanel的布局设置为FlowLayout,然后重试。我认为您在测试之前做的太多了。我的方法是从一些非常简单的东西开始,例如一个面板,一个静态标签。当它按预期显示时,添加带有菜单项的工具栏。那行吗。然后慢慢地加上碎片 很可能你会在一个简单的案例中遇到问题,然后可以解决它,或者你会在这里发布一个简单的案例
另外,作为起点,从网络上截取一些工作示例。减少它,然后根据您的案例进行构建。我认为您在测试之前试图做得太多了。我的方法是从一些非常简单的东西开始,例如一个面板,一个静态标签。当它按预期显示时,添加带有菜单项的工具栏。那行吗。然后慢慢地加上碎片 很可能你会在一个简单的案例中遇到问题,然后可以解决它,或者你会在这里发布一个简单的案例
另外,作为起点,从网络上截取一些工作示例。减少它,然后根据您的案例进行构建。在BoxLayout中添加另一个工具栏后,您可能需要(重新加入)验证面板 我已经多次这样做了,但我不理解3个左右方法调用背后的逻辑;所以我只是尝试一下,直到找到一个有效的:
topPanel.validate();
topPanel.invalidate();
topPanel.revalidate();
topPanel.layout();
(至少)其中一个会迫使您的GUI重新计算其布局,使北面板变大,从而显示您添加的第二个(和连续的)工具栏。将另一个工具栏添加到BoxLayout后,您可能需要(重新)验证面板 我已经多次这样做了,但我不理解3个左右方法调用背后的逻辑;所以我只是尝试一下,直到找到一个有效的:
topPanel.validate();
topPanel.invalidate();
topPanel.revalidate();
topPanel.layout();
(至少)其中一个会迫使您的GUI重新计算其布局,使北面板变大,从而显示您添加的第二个(和连续的)工具栏。有帮助吗?
这是你想要实现的吗
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();
}
}
有帮助吗?
这是你想要实现的吗
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();
}
}
topPanel的布局管理器是什么?希望能看到更多的代码。您是否添加了toolbar1Panel。。。到顶部面板?使用BorderLayout.PAGE\u工具栏的开始您已经发布了2-3次此问题:)topPanel的布局管理器是什么?如果能看到更多代码,那就太好了。您是否添加了toolbar1Panel。。。到顶部面板?使用BorderLayout.PAGE_工具栏开始你现在发布了2-3次这个问题:)实际上我已经将topPanel的布局添加为BoxLayout。我忘了写上面的描述。实际上,我已经将topPanel的布局添加为BoxLayout。我忘了写上面的描述。它也不是用那种布局运行的。万岁!它起作用了。A非常感谢你。从早上开始我就一直在试这个东西。你在我的3篇文章中都给出了答案。非常感谢,太好了!很高兴听到我能帮忙。你最后用的是哪一个家族?万岁!它起作用了。A非常感谢你。从早上开始我就一直在试这个东西。你在我的3篇文章中都给出了答案。非常感谢,太好了!很高兴听到我能帮忙。您最终使用的验证族是哪一个?