private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(checkboxcheck1 == 1)
{
textFieldSum.setText(String.valueOf(counter));
}
else if (checkboxcheck2 == 1)
{
textFieldSum1.setText(String.valueOf(counter1));
}
else
{
checkboxcheck1 = 0;
}
}
我创建了2个复选框,1个按钮和2个文本字段。当我单击复选框,然后单击按钮时,文本字段将显示结果。如果我同时点击2复选框,我想让按钮显示结果。有人能帮帮我吗。我想写一个循环,但我不知道如何写它。非常感谢你们的帮助。
编辑1:这是我的全部代码
public class Count extends JFrame
{
private JLabel labelSum = new JLabel("Display: ");
private JTextField textFieldSum = new JTextField(1);
private JCheckBox chckbxNewCheckBox1 = new JCheckBox("A");
private JButton btnYes = new JButton("YES");
private JTextField textFieldSum1 = new JTextField(1);
private JCheckBox chckbxNewCheckBox2 = new JCheckBox("B");
private int counter;
private int counter1;
private int checkboxcheck1 = 0;
private int checkboxcheck2 = 0;
public Count()
{
super("Yolo Coto");
super.setBounds(150, 150, 500, 500);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
super.getContentPane().setLayout(gridBagLayout);
//checkbox
add(chckbxNewCheckBox1);
textFieldSum.setEditable(false);
CheckBoxHandler handler = new CheckBoxHandler();
chckbxNewCheckBox1.addItemListener(handler);
//textfield
add(textFieldSum);
//button
add(btnYes);
ButtonHandler handler1 = new ButtonHandler();
btnYes.addActionListener(handler1);
//checkbox2
add(chckbxNewCheckBox2);
textFieldSum1.setEditable(false);
chckbxNewCheckBox2.addItemListener(handler);
//textfield2
add(textFieldSum1);
//readfile
BufferedReader reader;
int counterA = 0;
int counterB = 0;
try {
reader = new BufferedReader(new FileReader("in.txt"));
int data;
while ((data = reader.read()) != -1)
{
char charA = 'A';
char charB = 'B';
// char[] charArray = { 'A', 'B', 'C', 'D', 'E' };
if (charA == (char) data)
{
counterA++;
}
if (charB == (char) data)
{
counterB++;
}
}
counter1 = counterB;
counter = counterA;
reader.close();
}
catch (IOException ioException)
{
System.err.println("Error Opening File: Terminating");
System.exit(1);
}
}
private class CheckBoxHandler implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
if(chckbxNewCheckBox1.isSelected())
{
checkboxcheck1 = 1;
}
else if(chckbxNewCheckBox2.isSelected())
{
checkboxcheck2 = 1;
}
else if (chckbxNewCheckBox1.isSelected() && chckbxNewCheckBox2.isSelected())
{
checkboxcheck1 = 1;
checkboxcheck2 = 1;
}
}
}
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(checkboxcheck1 == 1)
{
textFieldSum.setText(String.valueOf(counter));
}
else if (checkboxcheck2 == 1)
{
textFieldSum1.setText(String.valueOf(counter1));
}
else if (checkboxcheck1 == 1 && checkboxcheck2 == 1)
{
textFieldSum.setText(String.valueOf(counter));
textFieldSum1.setText(String.valueOf(counter1));
}
else
{
checkboxcheck1 = 0;
}
}
我想要的只是当我单击两个复选框并单击按钮时,它会在该文本字段中显示结果。或者我点击单个复选框,它仍然在单个文本字段中显示结果。因为我的真实程序中还有20个复选框。基本上,我想要的只是点击任何复选框,然后点击它将显示结果的按钮。
答案 0 :(得分:1)
如果你的意思是必须点击两个复选框才能显示按钮,那么可能会这样吗?
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(checkboxcheck1 == 1 && checkboxcheck2 == 1)
{
textFieldSum.setText(String.valueOf(counter));
}
}
答案 1 :(得分:0)
听起来你想要一个包含两种条件的if语句。和&&运营商就是你想要的。
import [something] from scripts
答案 2 :(得分:0)
private class CheckBoxHandler implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
if (chckbxNewCheckBox1.isSelected() && chckbxNewCheckBox2.isSelected())
{
checkboxcheck1 = 1;
checkboxcheck2 = 1;
}
else if(chckbxNewCheckBox1.isSelected())
{
checkboxcheck1 = 1;
}
else if(chckbxNewCheckBox2.isSelected())
{
checkboxcheck2 = 1;
}
}
}
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (checkboxcheck1 == 1 && checkboxcheck2 == 1)
{
textFieldSum.setText(String.valueOf(counter));
textFieldSum1.setText(String.valueOf(counter1));
}
else if(checkboxcheck1 == 1)
{
textFieldSum.setText(String.valueOf(counter));
}
else if (checkboxcheck2 == 1)
{
textFieldSum1.setText(String.valueOf(counter1));
}
else
{
checkboxcheck1 = 0;
}
}
}