2012-12-02 104 views
1

我遇到了GUI中複選框的問題。我有5個複選框和一個名爲「生成報告」的按鈕。我想要做的是當我按下「生成報告」時,我想檢查選中哪些複選框,以便我可以用複選框中選中的信息「生成報告」。我知道如何檢查選中的是哪一個,但是如果選中了一個複選框,然後在點擊「生成報告」之前取消選中,恐怕程序在選擇之後不知道它是否被取消選擇。已選中複選框

這裏是我的代碼:

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

public class MonthReportGUI implements ItemListener 
{ 
static Calendar calendar = Calendar.getInstance(); 

JFrame frame = new JFrame("Month Report"); 
JPanel mainPanel = new JPanel(); 
JPanel comboPanel = new JPanel(); 
JLabel info = new JLabel("Use the check boxes to select the information to include in the month report"); 
JCheckBox checkBoxOne = new JCheckBox("Number accomplished"); 
JCheckBox checkBoxTwo = new JCheckBox("Number not accomplished"); 
JCheckBox checkBoxThree = new JCheckBox("Total Number of Jobs"); 
JCheckBox checkBoxFour = new JCheckBox("Month Salary"); 
JCheckBox checkBoxFive = new JCheckBox("Average wage per job"); 

Boolean boxOneSelected = false; 

static String [] monthList = {"January", "February", "March", "April", "May", "June", "July", "August", "September", 
"October", "November", "December"}; 

static String [] createYearList() 
{ 
    String [] yearList = new String[89]; 
    String year = (calendar.get(Calendar.YEAR)) + ""; 
    for(int i = 0; i < 88; i++) 
     yearList[i] = (Integer.parseInt(year) + i) + ""; 
    return(yearList); 
} 
JLabel monthL = new JLabel("Month:"); 
JLabel yearL = new JLabel("Year:"); 
static JComboBox monthCB = new JComboBox(monthList); 
static JComboBox yearCB = new JComboBox(createYearList()); 
JButton generate = new JButton("Generate Report"); // ACTION HAS TO BE ADDED 

Boolean oneSelected = false; 
Boolean twoSelected = false; 
Boolean threeSelected = false; 
Boolean fourSelected = false; 
Boolean fiveSelected = false; 

MonthReportGUI() 
{ 
    mainPanel.setLayout(new GridLayout(8,1)); 
    mainPanel.add(info, BorderLayout.CENTER); 
    mainPanel.add(checkBoxOne, BorderLayout.CENTER); 
    mainPanel.add(checkBoxTwo, BorderLayout.CENTER); 
    mainPanel.add(checkBoxThree, BorderLayout.CENTER); 
    mainPanel.add(checkBoxFour, BorderLayout.CENTER); 
    mainPanel.add(checkBoxFive, BorderLayout.CENTER); 
    mainPanel.add(comboPanel); 
    mainPanel.add(generate, BorderLayout.CENTER); 

    comboPanel.add(monthL); 
    comboPanel.add(monthCB); 
    comboPanel.add(yearL); 
    comboPanel.add(yearCB); 

    checkBoxOne.addItemListener(this); 
    checkBoxTwo.addItemListener(this); 
    checkBoxThree.addItemListener(this); 
    checkBoxFour.addItemListener(this); 
    checkBoxFive.addItemListener(this); 

    frame.add(mainPanel); 
    frame.pack(); 
    frame.setVisible(true); //set false 
    } 

    public void itemStateChanged(ItemEvent e) //perform action to know which are selected to use for writting report 
    { 
    Object source = e.getItemSelectable(); 
    if (source == checkBoxOne) 
    { 
     System.out.println(boxOneSelected); 
     boxOneSelected = true; 
     System.out.println(boxOneSelected); 
    } 
    } 

    public static void main (String agrs[]) 
    { 
    MonthReportGUI monthReport = new MonthReportGUI(); 
    } 
} 

謝謝您的時間。

回答

1

在單獨的布爾型字段中存儲複選框的選擇狀態沒有意義。相反,當點擊該按鈕時,詢問複選框,如果它直接選擇:

button.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     generateReport(checkbox1.isSelected(), 
         checkbox2.isSelected(), 
         checkbox3.isSelected(), 
         checkbox4.isSelected(), 
         checkbox5.isSelected()); 
    } 
}); 

做它像你這樣做可能會導致正確的結果,但你複製的功能,引入了新的路徑錯誤,並使其比應該更復雜。

+0

謝謝你真的很有幫助!它的工作:D – greinodacosta

1

這是一個非問題,因爲在JButton按下時只會在按鈕的ActionListener代碼中輪詢JCheckBox狀態,所有這些都將被處理。這是事件驅動編程的關鍵。代碼僅出現在事件中,因此可以確定事件發生時的程序狀態,並生成報告。如果嘗試在類構造或其他非事件驅動的方式下獲取JCheckBox狀態,只會遇到麻煩。

編輯:根據JB Nizet的帖子,我沒有看到你試圖將狀態存儲在變量中。不好。再次,編寫你的ActionListener代碼,然後輪詢你的JCheckBoxes。 1 +到JB Nizet。

1

更改行:

boxOneSelected = true; 

到:

boxOneSelected = checkBoxOne.isSelected(); 

有關於是否該檢查和分配應在聽衆提出的問題,但你要問的複選框是否它被選中或不作爲解決方案的核心部分。