2017-04-18 12 views
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悪い英語、私はもともとイタリア出身です。

答えて

0

いくつか問題がありますが、具体的な質問を解決するには:ボタンをクリックすると、actionPerformed()イベントがトリガーされます。そして、あなたのactionPerformed()メソッドでは、ボタンを非表示に設定しています。

if (button3.isSelected()) 
{ 
    System.out.println("You are incorrect. The correct answer is: 24 miles an hour"); 
    button1.setVisible(false);//set button 1 to invisible 
    button2.setVisible(false);//set button 2 invisible 
    button3.setVisible(false);//set button 3 invisible. 
    group.clearSelection(); 
} 
+0

私はそれを見るから、[回答を表示]ボタンがクリックされたときだけチェックするように設定しました。アクションコマンド "exe"は、もし私が正しいなら、show answersボタンのためだけです。 – nicholas

+0

それから、あなたは間違った接尾括弧を持っています。私はちょうどあなたの質問のコードを再フォーマットし、各ブロックがどこで終了するかを示すコメントを追加しました。 button1.isSelected()のみが "exe"のチェックの内側にあります。 – Kylar

+0

ああ、そうです。ありがとうございました – nicholas

関連する問題