0

我正在尝试根据 JRadioButton 选择动态更改内容...我的简化代码看起来像这样。

//import
public class Thing {
  //
  JPanel pnlMain, pnl1, pnl2, pnlRt, pnlLt;
  JRadioBtn btn1, btn2;
  //
  Thing () {
    //
    //initialize panels, add to them, etc.
    pnlMain.add(pnlLt);
    pnlMain.add(pnl1);
    pnlMain.add(pnlRt);
    //
    //Get it showing and stuff.
    //
    }
  //
  //One instance of this class connected to all radio buttons.
  class Evt implements ActionListener {
    public void actionImplemented (ActionEvent evt) {
      //
      pnlMain.remove(1);
      //
      if (evt.getActionCommand().equals("Radio 1"))
        pnlMain.add(pnl1);
      else pnlMain.add(pnl2);
      //
      pnlMain.validate();
      //
      }
    }
  //
  public static void main (String[] args) {
    new Thing();
    }
  //
  }

这让我可以更改面板,但我无法更改回我之前选择的面板......我不明白为什么。请帮忙!!!

4

2 回答 2

3

您应该改用 CardLayout ,因为这正是它的用途。在这里查看教程

于 2012-02-10T19:12:43.057 回答
2

使用适当的布局管理器。在这种情况下,我建议使用CardLayout. 这使开发人员能够将面板交换的“复杂性”委托给布局管理器,这是应该的。

于 2012-02-10T19:12:06.247 回答