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



我正在编写一个程序,该程序需要确定选中了哪个JCheckBox。我使用三个不同的按钮组,其中两个有重叠的文本。我需要能够确定哪个触发了事件,这样我就可以将适当的费用(COSTPERROOM vs 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,因为我认为拥有一组只允许一个选择的JRadioButtons而不是一组JCheckBoxes是GUI标准。
  • 虽然你可能有"重叠的文本",你可以设置按钮的actionCommand为任何你想要的。所以一组按钮可以有actioncommand,如"room count 2", "room count 3",…
  • 但是更好的是,ButtonGroup可以告诉你哪个切换按钮(复选框或单选按钮)已经被选中,因为如果你在它上面调用getSelection(),它会给你选定按钮的ButtonModel(或null,如果没有被选中),然后你可以通过它的getActionCommand()方法从模型中获得actionCommand。只是首先检查所选择的模型不是空的。
  • 学习使用布局管理器,因为它们可以使你的工作更容易。

例如,如果你有两个ButtonGroups:

ButtonGroup fooBtnGroup = new ButtonGroup();
ButtonGroup barBtnGroup = new ButtonGroup();

如果你在这些ButtonGroups中添加了一堆jradiobutton,你就可以像这样检查哪个按钮被哪个组选中了(下面的代码是在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

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

checkBox.addActionListener(actionListener); 

}

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

相关内容

  • 没有找到相关文章

最新更新