2

我正在开发一个需要确定选择了哪个 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]);
            }
        }
    }
}
4

2 回答 2

3

我会

  • 为此使用 JRadioButtons 而不是 JCheckBoxes,因为我认为拥有一组仅允许一个选择而不是一组 JCheckBoxes 的 JRadioButtons 是 GUI 标准。
  • 尽管您可能有“重叠文本”,但您可以将按钮的 actionCommand 设置为您想要的任何内容。因此,一组按钮可能具有“房间数 2”、“房间数 3”、...
  • 但更好的是,ButtonGroup 可以告诉您选择了哪个切换按钮(复选框或单选按钮),因为如果您调用getSelection()它,它将为您提供所选按钮的 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。

于 2011-09-24T22:12:47.333 回答
0

复选框与任何其他摆动组件一样具有项目侦听器。我会将它们解耦,并简单地将侦听器添加到每个 {

checkBox.addActionListener(actionListener); 

}

http://www.java2s.com/Code/Java/Swing-JFC/CheckBoxItemListener.htm

于 2011-09-24T23:09:52.477 回答