【问题标题】:JRadioButton and component changesJRadioButton 和组件更改
【发布时间】:2015-04-01 23:55:49
【问题描述】:

我创建了一个 JDialog 框,其中有 2 个单选按钮,当我单击另一个按钮时,它将更改 JLabel(例如:单击全职按钮时的月薪和单击兼职按钮时的小时薪)

所以我的问题是我该怎么做?我是否为 radioButton 创建 ActionListener 并在 actionPerformed 类中创建那些 JPanel?

【问题讨论】:

  • 您缺少哪一部分? JDialog 还是动作?
  • 对不起,动作部分不是 JDialog

标签: java swing jdialog jradiobutton


【解决方案1】:

我认为最好的方法是为按钮创建一个动作监听器。 When one is selected change the text with monthLabel.setText("Monthly Salary");

【讨论】:

    【解决方案2】:

    是的,你需要监听器,我推荐ItemListener 而不是ActionListener,但不需要在监听器中创建面板。而是更改标签文本本身。

    【讨论】:

      【解决方案3】:

      你可以这样做:

      radio1.addActionListener(new ActionListener(){
         label.setText("Clicked from radio 1");
      });
      
      radio2.addActionListener(new ActionListener(){
         label.setText("Clicked from radio 2");
      });
      

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 2011-02-04
        • 2012-03-03
        • 1970-01-01
        • 1970-01-01
        • 2015-05-21
        • 2015-09-22
        • 2021-07-03
        • 1970-01-01
        相关资源
        最近更新 更多