[英]How to update JLabel text when varible from another class changes?
[英]How to update a JLabel when a JLabel in another class updates?
我在这方面遇到了障碍,并希望获得一些见识。
这是我要完成的工作的基本要点:
-我有一个piece
类(扩展JPanel
),它具有JRadioButton
数组,还有一个JLabel
根据选择的按钮进行更新。 这部分工作正常。
-我有一个“主” JFrame
类,该类调用几个不同的piece
并将它们放在框架上–这样做是为了避免事情变得笨拙,将每个块作为自己的类会更容易。
-对于各种目的,我想提出一个JLabel
的上master
帧还示出了被选择哪个按钮-在本质上,我想提出一个JLabel
那是一个在的模仿piece
类和更新每当piece
标签不。 我不知道如果我只是被愚蠢的,但我不能让上了一个master
更新每当piece
人做-它只是停留在最初的一个。 有没有办法让这个标签在其他任何时候都更新?
“片断”(基本示例,不是真实的东西):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class Piece2 extends JPanel {
//variables
final JLabel on = new JLabel("NONE");
String names[] = {
"button 1","button 2","button 3"
};
private ActionListener buttonAction = new ActionListener(){
public void actionPerformed (ActionEvent ae){
String buttonText = ((JRadioButton) ae.getSource()).getText();
on.setText(buttonText);
}
};
void createList(){
Box contentPanel = Box.createVerticalBox();
ButtonGroup buttons123 = new ButtonGroup();
final JRadioButton choiceButtons[]=new JRadioButton[names.length];
for(int i=0;i<(names.length);i++){
choiceButtons[i] = new JRadioButton(names[i]);
choiceButtons[i].addActionListener(buttonAction);
buttons123.add(choiceButtons[i]);
contentPanel.add(choiceButtons[i]);
}
contentPanel.add(on);
this.add(contentPanel);
setVisible(true);
};
public static void main(String[] args) {
// TODO Auto-generated method stub
new Piece2().createList();
}
}
“ MASTER”(再次,非常简单的例子):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.event.*;
import java.util.*;
public class CollectGUI extends JFrame{
JLabel p2on = new JLabel("NONE");
private void createDialog(){
this.setSize(2000,1000);
this.setLocation(0,0);
this.setTitle("TITLE");
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BorderLayout());
final Piece2 piece = new Piece2();
piece.createList();
mainpanel.add(piece, BorderLayout.WEST);
//THIS IS THE PART WHERE I GET STUCK
p2on.setText(piece.on.getText());
//I KNOW IT ONLY SETS IT ONCE, HOW DO I GET IT TO UPDATE WHEN ON DOES?
mainpanel.add(p2on, BorderLayout.EAST);
this.add(mainpanel);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new CollectGUI().createDialog();
}
}
因此,就像我说的那样,我可以将标签放在那儿并设置一次,但是还没有弄清楚如何在每次打开时更新它……是否有一种方法可以将“附加”到片段类动作侦听器大师班? 还是可以将某种侦听器添加到p2on标签中,以便在on标签更改时进行侦听? 我很沮丧。 谢谢您的帮助!!
您可以使用某种Observer Pattern ,其中JPanel
和frame都使用一个公共的对象/模型来更改游戏的当前状态,但是它还提供事件通知,以便两者可以相应地更新自己。
这也与Model-View-Controller有关 ,它允许您将视觉与逻辑分离,从而将代码解耦
我希望有一个模型可以控制逻辑并通知感兴趣的各方,但是在您的情况下,您可以“使用” PropertyChangeListener
。
因此,在Piece2
类的ActionListener
,您需要触发一个属性更改事件...
private ActionListener buttonAction = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String buttonText = ((JRadioButton) ae.getSource()).getText();
on.setText(buttonText);
firePropertyChange("button", null, buttonText);
}
};
在您的CollectionGUI
,监视该事件
piece.addPropertyChangeListener("button", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
p2on.setText(piece.on.getText());
}
});
我也不鼓励您直接从顶级容器(如JFrame
扩展,它将您锁定在单个用例中,并降低了类的可重用性,并且您实际上也没有在类中添加任何新功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.