1
一直在使用JComponent進行實驗,併爲用戶做了一個簡短的3個問題測驗。由於我找不到代碼搞亂(或我)的確切區域,因此我將代碼放在出現的地方。當我點擊JFrame上的一個按鈕(button1除外),而不是僅僅保持選中狀態,它與其他按鈕一起消失,並顯示sysout。JRadioButton被選中後消失
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"))
{
}
}
}
爲了澄清,我還沒有完成,但是因爲我剛開始在這個項目上,並抓住了這個錯誤,我想我會來尋求幫助:)
PS對不起我英語不好,我來自意大利。
,我有它設置,以便當顯示答案按鈕被點擊它只是檢查。如果我是正確的,動作命令「exe」僅用於顯示答案按鈕。 – nicholas
然後你有一個錯位的左括號。我只是重新格式化了您的問題中的代碼,並添加了註釋以顯示每個塊的結束位置。只有button1.isSelected()在「exe」檢查中。 – Kylar
啊,我明白了。謝謝 – nicholas