<分区>
我正在尝试创建一个简单的程序,当按下特定的单选按钮时,它会更改某些文本框中的文本。这是我第一次使用 Java,所以我不确定我错过了什么。这是我的代码。
public abstract class DoctorOption extends JFrame implements ActionListener {
JTextField myTxt = new JTextField(30);
JButton submit = new JButton("Submit");
JRadioButton mywellRB = new JRadioButton("click here if you are well", true);
JRadioButton myunwellRB = new JRadioButton("click here if you are unwell", false);
public static void main(String[] args) {
new DoctorOption() {
};
}
public DoctorOption() {
setSize(400, 120);
setTitle("Doctor Option");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(myTxt);
add(mywellRB);
add(myunwellRB);
add(submit);
submit.addActionListener(this);
myunwellRB.addActionListener(this);
mywellRB.addActionListener(this);
setVisible(true);
ButtonGroup buttons = new ButtonGroup();
buttons.add(mywellRB);
buttons.add(myunwellRB);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == mywellRB) {
myTxt.setText("in the pink! ");
myTxt.setBackground(Color.pink);
}
}
}
请你帮我理解我做错了什么,谢谢!