2013-07-03 3 views
1

라디오 버튼 그룹에서 선택한 옵션을 가져 오는 방법은 무엇입니까? 정답으로 선택한 옵션을 확인할 수있는 방법은 무엇입니까? 이 on.Just 4 개 라디오 버튼이 질문에 각각 포함하고 있어요 코드입니다 (초보자입니다) 그리고 그들은 grouped.Now 나는 정답라디오 버튼 그룹에서 선택한 옵션과 정답의 비교

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class MyRadioButton { 

    private JFrame frame = new JFrame("RadioRadio"); 
    private JLabel timerl = new JLabel("Press Button to start"); 
    private JPanel butp = new JPanel(); 
    private JPanel panel1=new JPanel(); 
    private JLabel q1=new JLabel("Qn 1:Who is the first President Of India?"); 
    private JLabel q2=new JLabel("Qn 2:Who is the first Prime Minister Of India?"); 

    private JPanel panel2=new JPanel(); 
    private JPanel panel3=new JPanel(); 
    private JLabel label=new JLabel(); 
    private JButton button = new JButton("Start Exam"); 
    private JRadioButton rd1=new JRadioButton("a)Nehru"); 
    private JRadioButton rd2=new JRadioButton("b)R.Prasad"); 
    private JRadioButton rd3=new JRadioButton("c)S.R.Krishnan"); 
    private JRadioButton rd4=new JRadioButton("d)GV.Mavlankar"); 
    private JRadioButton rd5=new JRadioButton("a)Nehru"); 
    private JRadioButton rd6=new JRadioButton("b)R.Prasad"); 
    private JRadioButton rd7=new JRadioButton("c)S.R.Krishnan"); 
    private JRadioButton rd8=new JRadioButton("d)GV.Mavlankar"); 

    ButtonGroup group1 = new ButtonGroup(); 
    ButtonGroup group2 = new ButtonGroup(); 

    public int result=0; 
    private Timer mytimer; 
    private String ss = "Time Remaining %02d Seconds!"; 
    private int elapsedSeconds = 0; 
    private int total = 60; 
    JLayeredPane pane=new JLayeredPane(); 


    public MyRadioButton() { 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 


       if (mytimer != null && mytimer.isRunning()) { 
        //String t = String.format("Result:Your Score is %d", result); 
        //label.setText(t); 
        mytimer.stop(); 
        elapsedSeconds = 0; 
        timerl.setText("Exam Terminated"); 
       } else { 
        panel1.setBounds(0, 100, 500, 100); 
        panel1.add(q1); 
        pane.add(panel1,pane.DEFAULT_LAYER); 
        panel2.setBounds(0,200,500,100); 
        panel2.add(q2); 
        pane.add(panel2,pane.DEFAULT_LAYER); 
        panel3.setBounds(0, 300, 500, 100); 
        pane.add(panel3,pane.DEFAULT_LAYER); 
        panel3.add(label); 


        panel1.add(rd1); 
        panel1.add(rd2); 
        panel1.add(rd3); 
        panel1.add(rd4); 
        panel2.add(rd5); 
        panel2.add(rd6); 
        panel2.add(rd7); 
        panel2.add(rd8); 
        group1.add(rd1); 
        group1.add(rd2); 
        group1.add(rd3); 
        group1.add(rd4); 
        group2.add(rd5); 
        group2.add(rd6); 
        group2.add(rd7); 
        group2.add(rd8); 
        mytimer = new Timer(1000, new TimerListener()); 
        mytimer.start(); 
        String t = String.format(ss, total); 
        timerl.setText(t); 

       } 
      } 
     }); 

     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(500,500); 
     frame.add(pane); 
     butp.setBounds(0,0,500,100); 
     butp.add(button); 
     butp.add(timerl); 
     pane.add(butp,pane.DEFAULT_LAYER); 
     frame.setVisible(true); 
    } 

    private class TimerListener implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      elapsedSeconds++; 
      if (elapsedSeconds == total) { 
       mytimer.stop(); 
       elapsedSeconds = 0; 
       timerl.setText("Time Up"); 
      } else { 
       String t = String.format(ss, total - elapsedSeconds); 
       timerl.setText(t); 
      } 
     } 
    } 

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       MyRadioButton r = new MyRadioButton(); 

      } 
     }); 
    } 
} 
+1

시작 라디오 버튼), 그리고 _ 특정 질문을하십시오. – trashgod

+1

관련 없음 : 구성 요소의 수동 크기 조정/위치 지정을 절대하지 마십시오. 그게 바로 적합한 LayoutManager의 독점적 인 작업입니다. – kleopatra

답변

2

당신에게 확인 후 점수를 표시해야했다 예를 들어 클래스 필드에서 현재 선택된 라디오 버튼 값과 같이 설정할 수있는 각 버튼에 ActionListener을 추가 할 수 있습니다.

... 
String firstAnswer; 
String secondAnswer; 
... 

private FirstQuestionActionListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) { 
    firstAnswer = e.getActionCommand(); 
    } 
} 

private SecondQuestionActionListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) { 
    secondAnswer = e.getActionCommand(); 
    } 
} 

그리고 이런 식으로 사용하는 다음 [튜토리얼]에서 설명하는 예 (http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#으로 rb1.addActionListener(new FirstQuestionActionListener());

+0

감사합니다. .. 그랬어! –

관련 문제