两个JCheckbox来源?

时间:2015-03-07 22:17:35

标签: java swing listener actionlistener

我正在为我的任务写一个小体重计划。我有2个JRadioButton用于性别,5个JRadioButton用于高度类别。我为每个按钮添加了一个ActionListener。 在actionPerformed函数中,如何设置一个if()条件,让我根据性别和身高决定理想体重?

if(e.getSource() == genderM && e.getSource() == h60 )

似乎没有用。

问题明确指出应该在没有提交按钮的情况下完成。

这是我正在使用的代码:

public class IdealWeight extends JFrame implements ActionListener {

JLabel lblHeight;
JLabel lblGender;
JLabel lblIdeal;
JRadioButton genderM;
JRadioButton genderF;
JRadioButton h60;
JRadioButton h64;
JRadioButton h68;
JRadioButton h72;
JRadioButton h76;
JTextField txtIdealWeight;

public IdealWeight(){
    super("Ideal Wight");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    p1.setLayout(new GridLayout(3,1));
    p2.setLayout(new GridLayout(6,1));
    lblGender = new JLabel("Your gender: ");
    lblHeight = new JLabel("Your height: ");
    lblIdeal = new JLabel("Your ideal weight: ");
    this.setLayout(new GridLayout(2,3));
    ButtonGroup genderGroup = new ButtonGroup();
    ButtonGroup weightGroup = new ButtonGroup();
    genderM = new JRadioButton("Male: ");
    genderM.addActionListener(this);
    genderF = new JRadioButton("Female: ");
    genderF.addActionListener(this);
    h60 = new JRadioButton("60 to 64 inches");
    h60.addActionListener(this);
    h64  = new JRadioButton("64 to 68 inches");
    h64.addActionListener(this);
    h68  = new JRadioButton("68 to 72 inches");
    h68.addActionListener(this);
    h72  = new JRadioButton("72 to 76 inches");
    h72.addActionListener(this);
    h76  = new JRadioButton("76 to 80 inches");
    h76.addActionListener(this);
    txtIdealWeight = new JTextField();
    txtIdealWeight.setEditable(false);
    txtIdealWeight.setColumns(5);

    genderGroup.add(genderM);
    genderGroup.add(genderF);
    weightGroup.add(h60);
    weightGroup.add(h64);
    weightGroup.add(h68);
    weightGroup.add(h72);
    weightGroup.add(h76);
    p1.add(lblGender);
    p1.add(genderM);
    p1.add(genderF);

    p2.add(lblHeight);
    p2.add(h60);
    p2.add(h64);
    p2.add(h68);
    p2.add(h72);
    p2.add(h76);

    p3.add(lblIdeal);
    p3.add(txtIdealWeight);


    this.add(p1);
    this.add(p2);
    this.add(p3);

    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(400,400));


}

2 个答案:

答案 0 :(得分:3)

由于用户需要在准确处理之前输入所有字段中的信息,我不会在JCheckBoxes或JRadioButtons上使用ActionListeners,而是使用单个JButton,比如称为submitButton,然后从GUI中提取数据在其ActionListener中。

您可以从您正在使用的每个ButtonGroup对象中获取所选项目,因为它将返回所选JRadioButton的ButtonModel,如果没有选择任何内容,则返回null。

如果您需要更多帮助,请询问并编辑您的问题,以便向我们展示更相关的代码。


修改
您在评论中说明:

  

问题明确指出应该在没有提交按钮的情况下完成

这是关键信息,应该是原始问题的一部分。

然后使用一个ActionListener,不要担心来源。而是在ActionListener中,查询所有JRadioButtons的状态,然后对其进行操作,或者从ButtonGroups中获取模型并执行相同的操作。


例如:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TwoButtonGroups extends JPanel {
   public static final String[] LABELS_1 = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
   public static final String[] LABELS_2 = {"Fubar", "Snafu", "DILLIGAF"};
   private ButtonGroup buttonGroup1 = new ButtonGroup();
   private ButtonGroup buttonGroup2 = new ButtonGroup();

   public TwoButtonGroups() {
      JPanel panel1 = new JPanel(new GridLayout(0, 1));
      JPanel panel2 = new JPanel(new GridLayout(0, 1));

      MyActionListener myActionListener = new MyActionListener();
      for (String label1 : LABELS_1) {
         JRadioButton radioButton = new JRadioButton(label1);
         radioButton.setActionCommand(label1);
         radioButton.addActionListener(myActionListener);
         buttonGroup1.add(radioButton);
         panel1.add(radioButton);
      }
      for (String label2 : LABELS_2) {
         JRadioButton radioButton = new JRadioButton(label2);
         radioButton.setActionCommand(label2);
         radioButton.addActionListener(myActionListener);
         buttonGroup2.add(radioButton);
         panel2.add(radioButton);
      }

      add(panel1);
      add(panel2);
   }

   private class MyActionListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         ButtonModel model1 = buttonGroup1.getSelection();
         ButtonModel model2 = buttonGroup2.getSelection();
         if (model1 == null || model2 == null) {
            return;  // not selected
         }

         System.out.printf("Selections: %s and %s%n", model1.getActionCommand(), model2.getActionCommand() );
      }
   }

   private static void createAndShowGui() {
      TwoButtonGroups mainPanel = new TwoButtonGroups();

      JFrame frame = new JFrame("TwoButtonGroups");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:2)

条件

e.getSource() == genderM && e.getSource() == h60

永远不会返回true,因为源是genderM或h60。也许你的意思是逻辑或。

e.getSource() == genderM || e.getSource() == h60

作为替代方案,我会忽略事件的来源,而是使用组件的状态代替..

@Override
public void actionPerformed(ActionEvent e) {
    if (genderM.isSelected() && h60.isSelected()) {

    }
}