gpt4 book ai didi

java - 两个 JCheckbox 源?

转载 作者:行者123 更新时间:2023-12-01 16:59:21 25 4
gpt4 key购买 nike

我正在为我的作业编写一个小型体重程序。我有 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));


}

最佳答案

由于用户需要在所有字段中输入信息才能进行准确处理,因此我不会在 JCheckBox 或 JRadioButton 上使用 ActionListener,而是使用一个 JButton(例如称为“submitButton”),然后从 GUI 中提取数据在其 ActionListener 中。

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

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

<小时/>

编辑
您在评论中指出:

The problem specifically states that it should be done without a submit button

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

然后使用一个单独的 ActionListener,并且不用担心来源。相反,在 ActionListener 中,要么查询所有 JRadioButton 的状态,然后对其进行操作,要么从 ButtonGroup 中获取模型并执行相同的操作。

<小时/>

例如:

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();
}
});
}
}

关于java - 两个 JCheckbox 源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28920686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com