2011-09-24 18 views
2

我正在處理需要確定選擇哪個JCheckBox的程序。我正在使用三個不同的按鈕組,其中兩個有重疊的文本。我需要能夠確定哪些事件觸發了事件,以便我可以將合適的費用(COSTPERROOM與COSTPERCAR)添加到總計(costOfHome)中。我不知道如何區分複選框來源,如果文本是相同的。我正在考慮嘗試將一個按鈕組上的文本更改爲像「one」,「two」等字符串,但是這首先引入了一個更大的問題,那就是我如何創建了複選框。任何想法將不勝感激,提前致謝!如何確定哪個JCheckBox導致事件JCheckBox文本是相同的

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]); 
      } 
     } 
    } 
} 
+1

而不是'的String [] homeNamesArray'&'INT [] homeCostArray',它會更好有一個'Home [] homesArray'數組,其中'Home'是封裝'String name'和'int cost'的POJO。 –

回答

3

我會

  • 使用JradioButton將爲此而不是JCheckBoxes,因爲我認爲這是GUI標準有一套JradioButton將的,只允許一個選擇,而不是一組JCheckBoxes的。
  • 雖然你可能有「重疊文本」,你可以設置按鈕的actionCommand任何你想要的。因此,一組按鈕可以具有「房間計數2」,「房間計數3」的動作命令...
  • 但是,更好的是,ButtonGroup可以告訴你哪個切換按鈕(複選框或單選按鈕)因爲如果你打電話給getSelection(),它會得到所選按鈕的ButtonModel(如果沒有被選中,則爲null),然後你可以通過getActionCommand()方法從模型中獲得actionCommand。首先檢查選擇的模型是否爲空。
  • 學習如何使用佈局管理器,因爲它們可以讓您的工作更輕鬆。

舉例來說,如果你有兩個ButtonGroups:

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

如果添加了一堆JradioButton將與這些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。