在JFrame中用JPanel替换JPanel [英] Replacing JPanel with JPanel in a JFrame

查看:157
本文介绍了在JFrame中用JPanel替换JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展JFrame的类,它有一个BorderLayout。它有两个类型为JPanel的私有实例变量。它们代表按钮面板,称为flipButton和confidenceButtons。单击按钮时,按钮面板将被另一个按钮面板替换。也就是说,如果单击flipButton中的按钮,则flipButton将替换为confidenceButton。我尝试这样做:

I have a class that extends JFrame, and it has a BorderLayout. It has two private instance variables of type JPanel. They represent panels of buttons and are called flipButton and confidenceButtons. When you click on the button, the panel of buttons is replaced by the other panel of buttons. That is, if you click on a button in flipButton, flipButton is replaced by confidenceButtons. I tried to do it like this:


  private class FlipListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      remove(flipButton); 
      add(confidenceButtons,BorderLayout.SOUTH);
      validate();
      ...
    }
  } 
  private class ColorListener implements ActionListener{
    ...
    public void actionPerformed(ActionEvent e){
      ...
      remove(confidenceButtons); 
      add(flipButton,BorderLayout.SOUTH);
      validate();
    }
  }

flipButton中的按钮具有FlipListeners,而confidenceButtons中的按钮具有ColorListeners。程序运行时,单击按钮将删除面板,但不会添加任何内容来替换它。我究竟做错了什么?

The buttons in flipButton have FlipListeners and the ones in confidenceButtons have ColorListeners. When the program is run, clicking on a button will remove the panel, but nothing is added to replace it. What am I doing wrong?

编辑

CardLayout原来是一个简单易用的解决方案。事实证明,上面的代码确实有效;问题是我的代码的另一部分中的拼写错误。 ><。但是,我总是在使用这些方法时遇到了麻烦,我发现CardLayout为我简化了它。谢谢。

CardLayout turned out to be a simple and easy solution. It turns out that the above code does work; the problem was a typo in another section of my code. >.< However, I've always had trouble using these methods, and CardLayout, I find, simplifies it for me. Thanks.

推荐答案

使用 CardLayout ,如图所示此处

这篇关于在JFrame中用JPanel替换JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆