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();
  }
}

感谢您的时间。

4

3 回答 3

1

这不是问题,因为您将在按下 JButton 时仅在按钮的 ActionListener 代码中轮询 JCheckBox 状态,因此所有这些都将得到处理。这是事件驱动编程的关键。该代码仅因事件而发生,因此可以确定事件发生时的程序状态,并生成您的报告。如果您尝试在类构造或其他一些非事件驱动的方式中获取 JCheckBox 状态,您只会遇到麻烦。

编辑:根据 JB Nizet 的帖子,我没有看到您试图将状态存储在变量中。不好。再次,编写您的 ActionListener 代码并轮询您的 JCheckBoxes。1+ 给 JB 尼泽特。

于 2012-12-02T18:27:57.373 回答
1

将复选框的选择状态存储在单独的布尔字段中是没有意义的。相反,当单击按钮时,询问复选框是否直接选中:

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

像你正在做的那样做可能会导致正确的结果,但你正在复制功能,为错误引入新的路径,并使其比应有的复杂。

于 2012-12-02T18:29:18.597 回答
1

换行:

boxOneSelected = true;

至:

boxOneSelected = checkBoxOne.isSelected();

关于是否应在侦听器中进行此检查和分配存在问题,但您确实想询问复选框是否被选中作为解决方案的核心部分。

于 2012-12-02T18:30:46.927 回答