close
參考答案
import java.awt.event.*; import javax.swing.*; class JDD01 { public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MyJFrame().setVisible(true); } }); } } class MyJFrame extends javax.swing.JFrame { private ButtonGroup answers; private JRadioButton baseball; private JRadioButton basketball; private JRadioButton tennis; private JRadioButton other; private JButton okBtn; private JTextField otherBallGame; private JLabel question; private String favoriteBallGame="棒球"; public MyJFrame() { answers = new ButtonGroup(); okBtn = new JButton(); question = new JLabel(); baseball = new JRadioButton(); basketball = new JRadioButton(); tennis = new JRadioButton(); other = new JRadioButton(); otherBallGame = new JTextField(); setSize(340,290); setLocationRelativeTo(null); setLayout(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("問卷調查"); okBtn.setText("確定"); okBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ String tmp; if(favoriteBallGame.equals("")) tmp = otherBallGame.getText(); else tmp = favoriteBallGame; JOptionPane.showMessageDialog(MyJFrame.this, "你最喜好的球類運動是"+tmp,"test",JOptionPane.INFORMATION_MESSAGE); } }); question.setText("下列何者是您最喜好的球類運動?(單選)"); answers.add(baseball); baseball.setText("棒球"); baseball.setSelected(true); baseball.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ otherBallGame.setEnabled(false); favoriteBallGame="棒球"; } }); answers.add(basketball); basketball.setText("籃球"); basketball.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { otherBallGame.setEnabled(false); favoriteBallGame="籃球"; } }); answers.add(tennis); tennis.setText("網球"); tennis.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ otherBallGame.setEnabled(false); favoriteBallGame="網球"; } }); answers.add(other); other.setText("其它"); other.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ otherBallGame.setEnabled(true); favoriteBallGame = ""; } }); otherBallGame.setEnabled(false); question.setSize(240, 30); question.setLocation(50,20); baseball.setSize(100,30); baseball.setLocation(50,50); basketball.setSize(100,30); basketball.setLocation(50,80); tennis.setSize(100,30); tennis.setLocation(50,110); other.setSize(70,30); other.setLocation(50, 140); otherBallGame.setSize(100,20); otherBallGame.setLocation(120, 145); okBtn.setSize(100,20); okBtn.setLocation(120, 200); getContentPane().add(question); getContentPane().add(baseball); getContentPane().add(basketball); getContentPane().add(tennis); getContentPane().add(other); getContentPane().add(otherBallGame); getContentPane().add(okBtn); } }
執行結果
全站熱搜
留言列表