我不明白为什么这段代码不起作用(JRadioButtons 的新功能)
I do not understand why this code won't work(new to JRadioButtons)
我正在学习 JRadioButtons
,但我不知道为什么它在我正在观看的教程中有效,但在我的代码中却无效。有人可以看一下吗?
主要Class:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Screen screen = new Screen();
screen.setVisible(true);
}
}
这是屏幕 Class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class Screen extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JRadioButton b1, b2;
ButtonGroup group;
JTextArea tb;
public Screen(){
super("First GUI");
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel p1 = new JPanel();
JTextArea tb = new JTextArea("Text Area");
group = new ButtonGroup();
group.add(b1);
group.add(b2);
b1 = new JRadioButton("Hello");
b1.setActionCommand("HELLO!");
b1.addActionListener(this);
b2 = new JRadioButton("Goodbye");
b2.setActionCommand("Goodbye! =)");
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(tb);
add(p1);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tb.setText(e.getActionCommand());
}
}
在我的新 Java 脑袋中,这应该可以完美运行。我初始化按钮,初始化组。单击其中一个按钮后出现错误:AWT-EventQueue-0
。我不知道那是什么意思,所以我不知道如何解决这个问题。
您已经声明了同一个变量两次。如果您在全局和局部范围内声明相同的变量(JTextArea tb
),它将是一个单独的对象。从 Screen()
构造函数中删除局部范围内的声明,这样它就可以工作了。
试试这个
tb = new JTextArea("Text Area");
而不是
JTextArea tb = new JTextArea("Text Area");
由于您当前的代码,tb
仍未在全局范围内初始化。
我正在学习 JRadioButtons
,但我不知道为什么它在我正在观看的教程中有效,但在我的代码中却无效。有人可以看一下吗?
主要Class:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Screen screen = new Screen();
screen.setVisible(true);
}
}
这是屏幕 Class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class Screen extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JRadioButton b1, b2;
ButtonGroup group;
JTextArea tb;
public Screen(){
super("First GUI");
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel p1 = new JPanel();
JTextArea tb = new JTextArea("Text Area");
group = new ButtonGroup();
group.add(b1);
group.add(b2);
b1 = new JRadioButton("Hello");
b1.setActionCommand("HELLO!");
b1.addActionListener(this);
b2 = new JRadioButton("Goodbye");
b2.setActionCommand("Goodbye! =)");
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(tb);
add(p1);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tb.setText(e.getActionCommand());
}
}
在我的新 Java 脑袋中,这应该可以完美运行。我初始化按钮,初始化组。单击其中一个按钮后出现错误:AWT-EventQueue-0
。我不知道那是什么意思,所以我不知道如何解决这个问题。
您已经声明了同一个变量两次。如果您在全局和局部范围内声明相同的变量(JTextArea tb
),它将是一个单独的对象。从 Screen()
构造函数中删除局部范围内的声明,这样它就可以工作了。
试试这个
tb = new JTextArea("Text Area");
而不是
JTextArea tb = new JTextArea("Text Area");
由于您当前的代码,tb
仍未在全局范围内初始化。