jframe java对象,框架不工作
我正在尝试创建一个简单的程序,当按下特定的单选按钮时,可以更改某些文本框中的文本。这是我第一次使用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);
}
}
}
请你帮我理解我做错了什么,谢谢
# 1 楼答案
我看到的问题是,您没有使用任何布局,这意味着它将默认为BorderLayout。BorderLayout默认设置为将所有内容放入BorderLayout。居中,所以你会看到一件东西占据了整个窗口。将这行代码放入构造函数中:
For more info on Layouts
# 2 楼答案
您正在实现
ActionListener
接口,因此需要实现actionPerformed
方法。像这样改变你的主要方法