正在试验JComponent并正在为用户做一个简短的3问题测验。由于我找不到代码搞乱的确切区域(或我),我将把代码包括在它出现的地方。当我单击JFrame上的按钮(button1除外)时,它不再只是保持选中状态,而是与其他按钮一起消失,并显示sysout。
public class SimpleQuiz implements ActionListener
{
private JFrame frame;
private JLabel label;
private JButton button;
private JButton buttona;
private JButton buttonb;
private JMenuBar menubar;
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private JMenuItem start;
private JMenuItem exit;
private JMenu fileMenu;
private ButtonGroup group;
public static void main(String[] args)
{
new SimpleQuiz();
}
public SimpleQuiz()
{
//Creates the frame, label, button, textfield. and menubar.
frame = new JFrame("Classwork 10");
label = new JLabel("Q1: What is the airspeed velocity of an unladen swallow?");
button = new JButton("Show Answer");
buttona = new JButton("Show Answer");
buttonb = new JButton("Show Answer");
menubar = new JMenuBar();
button1 = new JRadioButton("24 miles an hour");
button2 = new JRadioButton("17 miles an hour");
button3 = new JRadioButton("32 miles an hour");
fileMenu = new JMenu("File");
exit = new JMenuItem("Exit");
start = new JMenuItem("Start Quiz");
group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
//Setting dimensions and properties for the JFrame
frame.setSize(500, 500);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
//Setting dimensions and properties for the JButton
button.setSize(175,50);
button.setLocation(175, 350);
button.setActionCommand("exe");
button.addActionListener(this);
button.setVisible(false);
buttona.setSize(175,50);
buttona.setLocation(175, 350);
buttona.setActionCommand("b");
buttona.addActionListener(this);
buttona.setVisible(false);
buttonb.setSize(175,50);
buttonb.setLocation(175, 350);
buttonb.setActionCommand("c");
buttonb.addActionListener(this);
buttonb.setVisible(false);
button1.setSize(175,15);
button1.setLocation(150, 300);
button1.setActionCommand("t");
button1.addActionListener(this);
button1.setVisible(false);
button2.setSize(175,15);
button2.setLocation(150, 275);
button2.setActionCommand("v");
button2.addActionListener(this);
button2.setVisible(false);
button3.setSize(175,15);
button3.setLocation(150, 250);
button3.setActionCommand("i");
button3.addActionListener(this);
button3.setVisible(false);
//Setting location and dimensions of the first question
label.setSize(500,25);
label.setLocation(25, 225);
label.setVisible(false);
//Setting dimensions and properties of the JMenuBar and subitems.
fileMenu.setMnemonic(KeyEvent.VK_F);
menubar.setSize(500, 25);
menubar.add(fileMenu);
fileMenu.add(start);
fileMenu.add(exit);
start.setActionCommand("start");
start.addActionListener(this);
exit.setActionCommand("exit");
exit.addActionListener(this);
menubar.setVisible(true);
//adding the JComponents to the frame
frame.setJMenuBar(menubar);
frame.add(button);
frame.add(buttona);
frame.add(buttonb);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(label);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("start"))
{
start.setEnabled(false);
label.setVisible(true);
button1.setVisible(true);
button2.setVisible(true);
button3.setVisible(true);
button.setVisible(true);
}//End for actioncommand == start.
if(e.getActionCommand().equals("exe"))
{
if (button1.isSelected())
{
System.out.println("You are correct.");
label.setText("Q2: Both your arms are cut off. Your opponent taunts you. What do you say?");
button1.setVisible(false);
button2.setVisible(false);
button3.setVisible(false);
group.clearSelection();
}//end of if button1 isSelected()
}//end of actioncomand == exe
if (button2.isSelected())
{
System.out.println("You are incorrect. The correct answer is: 24 miles an hour");
button1.setVisible(false);
button2.setVisible(false);
button3.setVisible(false);
group.clearSelection();
}//end of button2.isselected
if (button3.isSelected())
{
System.out.println("You are incorrect. The correct answer is: 24 miles an hour");
button1.setVisible(false);
button2.setVisible(false);
button3.setVisible(false);
group.clearSelection();
}//end of button3.isselected.
if (e.getActionCommand().equals("b"))
{
}
}
}
澄清一下,我还没有完成,但由于我刚刚开始这个项目并发现了这个错误,我想我会来找一些帮助:)
对不起我的英语不好,我来自意大利。答案 0 :(得分:0)
有一些问题,但要解决您的具体问题:当您单击按钮时,它会触发actionPerformed()事件。在actionPerformed()方法中,您将按钮设置为不可见:
desc, count