按 jtoolbar 按钮时在不同的 JPanel 之间切换
我正在设计一个父 Jpanel 包含 JToolBar && 内部JPanel。通过JToolBar的操作,我需要用我已经设计的新JPanel替换内部JPanel 。 按下jtoolbar按钮时在不同的JPanel之间切换。 如何在 NetBeans IDE 中执行此操作?
final CardLayout c = new CardLayout();
jPanel2 = new JPanel(c);
jPanel2.add(new BarChartPanel(), "CHART");
jPanel2.add(new ReportViewPanel(), "REPORT");
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("Images/barimages.jpg");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
JButton btnChart = new JButton(aceOfDiamonds);
btnChart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.show(jPanel2, "CHART");
}
});
jToolBar1.add(btnChart);
jToolBar1.addSeparator();
jToolBar1.setFloatable(false);
这里的“jPanel2”是我的内部面板,但是当单击工具栏的按钮时,它没有显示!
Im designing a parent Jpanel contains JToolBar && inner JPanel.By the JToolBar's action i need to replace the inner JPanel with new JPanel which where i designed already. switching between different JPanels when pressing jtoolbar buttons.
How can I do that in NetBeans IDE?
final CardLayout c = new CardLayout();
jPanel2 = new JPanel(c);
jPanel2.add(new BarChartPanel(), "CHART");
jPanel2.add(new ReportViewPanel(), "REPORT");
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("Images/barimages.jpg");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
JButton btnChart = new JButton(aceOfDiamonds);
btnChart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.show(jPanel2, "CHART");
}
});
jToolBar1.add(btnChart);
jToolBar1.addSeparator();
jToolBar1.setFloatable(false);
Here 'jPanel2' is my inner panel but while clicking the toolbars's button it's not diplaying!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
CardLayout
为“主面板的内面板”的布局,如图此处。Use a
CardLayout
for the layout of the "main panel's inner panel", as shown here.对于多个面板,您可能需要这样的东西:(不是完美的代码,但这就是方法,我只是为您指明方向):
现在,在问题中,更改主面板的外观如果你想改变外观和感觉,这是不可能的,如果你只想改变外观,比如背景,你可以通过覆盖
paintComponent( )
方法。For multiple panel, you might want something like this : (Not a perfect code,but this is the way,i'm just pointing you the way) :
and now, in the question, change the look of the main panel if you mean to change the look and feel, its not possible, and if you mean to just change the appearance,say background, you can do that by overriding the
paintComponent()
method.