gpt4 book ai didi

java - 当 JCheckBox 文本相同时,如何确定哪个 JCheckBox 导致了事件

转载 作者:行者123 更新时间:2023-12-04 06:13:15 27 4
gpt4 key购买 nike

我正在开发一个需要确定选择了哪个 JCheckBox 的程序。我正在使用三个不同的按钮组,其中两个有重叠的文本。我需要能够确定是哪个触发了事件,以便将适当的费用(COSTPERROOM 与 COSTPERCAR)添加到总费用(costOfHome)中。我无法弄清楚如果文本相同,如何区分复选框源。我正在考虑尝试将一个按钮组上的文本更改为“一”“二”等字符串,但这会带来一个更大的问题,即我如何首先创建复选框。任何想法将不胜感激,在此先感谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JMyNewHome extends JFrame implements ItemListener {

// class private variables
private int costOfHome = 0;

// class arrays
private String[] homeNamesArray = {"Aspen", "Brittany", "Colonial", "Dartmour"};
private int[] homeCostArray = {100000, 120000, 180000, 250000};

// class constants
private final int MAXROOMS = 3;
private final int MAXCARS = 4;
private final int COSTPERROOM = 10500;
private final int COSTPERCAR = 7775;

JLabel costLabel = new JLabel();

// constructor
public JMyNewHome ()
{
super("My New Home");
setSize(450,150);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Font labelFont = new Font("Time New Roman", Font.BOLD, 24);

setJLabelString(costLabel, costOfHome);
costLabel.setFont(labelFont);
add(costLabel);

JCheckBox[] homesCheckBoxes = new JCheckBox[homeNamesArray.length];
ButtonGroup homeSelection = new ButtonGroup();
for (int i = 0; i < homeNamesArray.length; i++)
{
homesCheckBoxes[i] = new JCheckBox(homeNamesArray[i], false);
homeSelection.add(homesCheckBoxes[i]);
homesCheckBoxes[i].addItemListener(this);
add(homesCheckBoxes[i]);
}

JLabel roomLabel = new JLabel("Number of Rooms in Home");
add(roomLabel);

ButtonGroup roomSelection = new ButtonGroup();
JCheckBox[] roomCheckBoxes = new JCheckBox[MAXROOMS];
for (int i = 0; i < MAXROOMS; i++)
{
String intToString = Integer.toString(i + 2);
roomCheckBoxes[i] = new JCheckBox(intToString);
roomSelection.add(roomCheckBoxes[i]);
roomCheckBoxes[i].addItemListener(this);
add(roomCheckBoxes[i]);
}

JLabel carLabel = new JLabel("Size of Garage (number of cars)");
add(carLabel);

ButtonGroup carSelection = new ButtonGroup();
JCheckBox[] carCheckBoxes = new JCheckBox[MAXCARS];
for (int i = 0; i < MAXCARS; i++)
{
String intToString = Integer.toString(i);
carCheckBoxes[i] = new JCheckBox(intToString);
carSelection.add(carCheckBoxes[i]);
carCheckBoxes[i].addItemListener(this);
add(carCheckBoxes[i]);
}

setVisible(true);

}

private void setJLabelString(JLabel label, int cost)
{
String costOfHomeString = Integer.toString(cost);
label.setText("Cost of Configured Home: $ " + costOfHomeString + ".00");
invalidate();
validate();
repaint();
}

public void itemStateChanged(ItemEvent e) {
JCheckBox source = (JCheckBox) e.getItem();
String sourceText = source.getText();
//JLabel testLabel = new JLabel(sourceText);
//add(testLabel);
//invalidate();
//validate();
//repaint();
for (int i = 0; i < homeNamesArray.length; i++)
{
if (sourceText == homeNamesArray[i])
{
setJLabelString(costLabel, costOfHome + homeCostArray[i]);
}
}
}
}

最佳答案

我会

  • 为此使用 JRadioButtons 而不是 JCheckBoxes,因为我认为拥有一组仅允许一个选择而不是一组 JCheckBoxes 的 JRadioButtons 是 GUI 标准。
  • 尽管您可能有“重叠文本”,但您可以将按钮的 actionCommand 设置为您想要的任何内容。因此,一组按钮可以具有“房间数 2”、“房间数 3”、...
  • 但更好的是,如果您调用 getSelection(),ButtonGroup 可以告诉您选择了哪个切换按钮(复选框或单选按钮)。在它上面,它将为您获取所选按钮的 ButtonModel(如果未选择任何按钮,则为 null),然后您可以通过其 getActionCommand() 从模型中获取 actionCommand方法。只需首先检查所选模型是否为空。
  • 学习使用布局管理器,因为它们可以让您的工作更轻松。

  • 例如,如果您有两个 ButtonGroup:
    ButtonGroup fooBtnGroup = new ButtonGroup();
    ButtonGroup barBtnGroup = new ButtonGroup();

    如果将一堆 JRadioButtons 添加到这些 ButtonGroups 中,则可以检查为哪个组选择了哪些按钮,如下所示(以下代码在 JButton 的 ActionListener 中):
    ButtonModel fooModel = fooBtnGroup.getSelection();
    String fooSelection = fooModel == null ? "No foo selected" : fooModel.getActionCommand();

    ButtonModel barModel = barBtnGroup.getSelection();
    String barSelection = barModel == null ? "No bar selected" : barModel.getActionCommand();

    System.out.println("Foo selected: " + fooSelection);
    System.out.println("Bar selected: " + barSelection);

    当然,假设您已经为按钮设置了 actionCommand。

    关于java - 当 JCheckBox 文本相同时,如何确定哪个 JCheckBox 导致了事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7542218/

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