当另一个类中的 JLabel 更新时,如何更新 JLabel



我在这个问题上遇到了障碍,希望能得到一些见解。

以下是我想要完成的基本要点:

-我有一个piece类(扩展JPanel),它有一个JRadioButton数组,以及一个根据选择的按钮更新的JLabel。这部分工作正常。

-我有一个"大师"JFrame类,它调用了几个不同的piece并将它们放在一个框架上 - 这样做是为了防止事情变得笨拙,让每个部分都成为自己的类更容易。

-出于各种目的,我想在master框架上放一个JLabel,该也显示选择了哪个按钮 - 本质上,我想放一个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();
    }
    }

"大师"(再次,非常简单的例子):

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 标签中的某种侦听器,以便在标签更改时侦听?我很困惑。谢谢你的帮助!!

您可以使用某种观察者模式,其中JPanel和框架都使用公共对象/模型来更改游戏的当前状态,但它还提供事件通知,以便两者可以相应地更新自己

这也与模型-视图-控制器有关,这将允许您将视觉对象与逻辑分离,从而分离您的代码

我宁愿有一个控制逻辑并通知相关方的模型,但在您的情况下,您"可以"只使用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这样的顶级容器扩展,它将你锁定在一个用例中,降低了你的类的可重用性,而且你也没有真正向类添加任何新功能。

最新更新