【问题标题】:How can I determine which JCheckBox caused an event when the JCheckBox text is the same当 JCheckBox 文本相同时,如何确定哪个 JCheckBox 导致了事件
【发布时间】:2011-09-24 22:04:46
【问题描述】:

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

【问题讨论】:

  • String[] homeNamesArray & int[] homeCostArray相比,最好有一个Home[] homesArray数组,其中Home是一个封装了String name & int cost的POJO。

标签: java swing event-handling jcheckbox buttongroup


【解决方案1】:

我愿意

  • 为此使用 JRadioButtons 而不是 JCheckBoxes,因为我认为拥有一组仅允许一个选择而不是一组 JCheckBoxes 的 JRadioButtons 是 GUI 标准。
  • 虽然您可能有“重叠文本”,但您可以将按钮的 actionCommand 设置为您想要的任何内容。因此,一组按钮的 actionCommand 可以是“room count 2”、“room count 3”……
  • 但更好的是,ButtonGroup 可以告诉您选择了哪个切换按钮(复选框或单选按钮),因为如果您在其上调用 getSelection(),它将为您获取所选按钮的 ButtonModel(如果没有被选中),然后您可以通过其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。

【讨论】:

    【解决方案2】:

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

    checkBox.addActionListener(actionListener); 
    

    }

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多