Java JPanel单选按钮不随计时器更改文本,但在单击时更改
在谷歌搜索1小时和调试4小时后,我不得不问: 我试图更改每个计时器中两个单选按钮的文本,但不知何故,即使单击它们,文本也不会更改Java JPanel单选按钮不随计时器更改文本,但在单击时更改,java,swing,Java,Swing,在谷歌搜索1小时和调试4小时后,我不得不问: 我试图更改每个计时器中两个单选按钮的文本,但不知何故,即使单击它们,文本也不会更改 public class JTestinf extends JPanel implements ActionListener { JRadioButton one = new JRadioButton(); JRadioButton two = new JRadioButton(); int i = 0; public static
public class JTestinf extends JPanel implements ActionListener {
JRadioButton one = new JRadioButton();
JRadioButton two = new JRadioButton();
int i = 0;
public static void main(String[] args) {
JFrame window = new JFrame("RadioButtonDemo");
JComponent ContentPane = new JTestinf();
window.setContentPane(ContentPane);
window.pack();
window.setVisible(true);
Timer time = new Timer(500, new JTestinf());
time.start();
}
public JTestinf() {
one.setText(""+ i++);
two.setText(""+ i++);
ButtonGroup group = new ButtonGroup();
group.add(one);
group.add(two);
one.addActionListener(this);
two.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(one);
radioPanel.add(two);
add(radioPanel, BorderLayout.LINE_START);
}
public void actionPerformed(ActionEvent e) {
one.setText(""+ i++);
two.setText(""+ i++);
System.err.println("actionPerformed: ActionEvent="+e.getActionCommand()+" i="+i);
}
}
另外,
i
包含两个不同的整数,一个是计时器计数,另一个是点击计数。这是怎么发生的?您有两个JTestInf对象:
JFrame window = new JFrame("RadioButtonDemo");
JComponent ContentPane = new JTestinf(); // **** one
window.setContentPane(ContentPane);
window.pack();
window.setVisible(true);
Timer time = new Timer(500, new JTestinf()); // **** two
只用一个
i、 e
更好的是,不要让GUI视图类实现侦听器控制接口。把它们分开
此外,您的计时器应该有自己的ActionListener,它与JRadioButton listener不同,因为它们的操作非常不同。此外,您通常不提供JRadioButton的ActionListener,而是将它们添加到ButtonGroup,并在另一个事件中从ButtonGroup中提取所选按钮,例如从另一个JButton中提取ActionListener
你问 但我为什么(以及如何)将它们分开呢 因为否则你给了一个班级太多的责任。例如,通过让您的GUI实现ActionListener,它几乎会迫使您为计时器和JRadioButtons提供相同的侦听器,这是您不想做的,因为它们的操作非常不同。如果侦听器代码很小,请考虑使用匿名内部类。
比如说
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTestinf extends JPanel {
JRadioButton one = new JRadioButton();
JRadioButton two = new JRadioButton();
int i = 0;
public static void main(String[] args) {
JFrame window = new JFrame("RadioButtonDemo");
// JComponent ContentPane = new JTestinf();
JTestinf jTestinf = new JTestinf();
window.setContentPane(jTestinf);
window.pack();
window.setVisible(true);
TimerListener timerListener = new TimerListener(jTestinf);
Timer time = new Timer(500, timerListener);
time.start();
}
public JTestinf() {
one.setText("" + i++);
two.setText("" + i++);
ButtonGroup group = new ButtonGroup();
group.add(one);
group.add(two);
one.addActionListener(new RadioListener());
two.addActionListener(new RadioListener());
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(one);
radioPanel.add(two);
one.setActionCommand("one");
two.setActionCommand("two");
add(radioPanel, BorderLayout.LINE_START);
}
private class RadioListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO put in code for radio button
System.out.println("JRadioButton Pressed: " + e.getActionCommand());
}
}
public void setRadioText(int index) {
one.setText("" + index);
two.setText("" + (index + 1));
}
}
class TimerListener implements ActionListener {
private int index = 0;
private JTestinf jTestinf;
public TimerListener(JTestinf jTestinf) {
this.jTestinf = jTestinf;
}
public void actionPerformed(ActionEvent e) {
index++;
jTestinf.setRadioText(index);
}
}
在论坛上发布完整的演示代码,而不是在其他网站上发布。代码应为复制问题所需的最低代码。此外,每次调用actionPerformed()方法时,您都会将“I”的值增加两次。谢谢:)但是为什么(以及如何)将它们分开?@BMicraft:请参阅侦听器同时使用内部类和外部类的代码示例。感谢您提供的示例。它确实帮助了我:)我如何在类
JTestinf
中使用全局变量在类TimerListener
中?如果侦听器是jtf中的内部类,那么您可以简单地使用该变量。如果它是一个外部类,那么给JTF一个公共方法,允许外部类改变它的状态——我在上面的代码中给出了一个例子。:)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTestinf extends JPanel {
JRadioButton one = new JRadioButton();
JRadioButton two = new JRadioButton();
int i = 0;
public static void main(String[] args) {
JFrame window = new JFrame("RadioButtonDemo");
// JComponent ContentPane = new JTestinf();
JTestinf jTestinf = new JTestinf();
window.setContentPane(jTestinf);
window.pack();
window.setVisible(true);
TimerListener timerListener = new TimerListener(jTestinf);
Timer time = new Timer(500, timerListener);
time.start();
}
public JTestinf() {
one.setText("" + i++);
two.setText("" + i++);
ButtonGroup group = new ButtonGroup();
group.add(one);
group.add(two);
one.addActionListener(new RadioListener());
two.addActionListener(new RadioListener());
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(one);
radioPanel.add(two);
one.setActionCommand("one");
two.setActionCommand("two");
add(radioPanel, BorderLayout.LINE_START);
}
private class RadioListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO put in code for radio button
System.out.println("JRadioButton Pressed: " + e.getActionCommand());
}
}
public void setRadioText(int index) {
one.setText("" + index);
two.setText("" + (index + 1));
}
}
class TimerListener implements ActionListener {
private int index = 0;
private JTestinf jTestinf;
public TimerListener(JTestinf jTestinf) {
this.jTestinf = jTestinf;
}
public void actionPerformed(ActionEvent e) {
index++;
jTestinf.setRadioText(index);
}
}