java swing jpanel不会刷新

java swing jpanel will not refresh

在我的 swing 应用程序中,我在一个框架中有两个面板。 第一个面板有一个按钮。当我单击此按钮时,一些数据发生变化,我希望第二个面板的内容也发生变化,以显示新数据(通过 jlabel)。重绘()不起作用。我尝试了各种更新(见代码中的注释)。

我参考了第二个面板。我可以使用该参考更改第二个面板的颜色,但我无法重新绘制它。我也可以在第二个面板中引用jlabel并更改其文本,但本题的重点是让panel2刷新。

SSCCE 如下。 请看一下。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class Repaint_Test
{
    public static void main(String args[]){     
        Frame frame = new Frame();      
    }
}

class Frame extends JFrame
{
    public Frame()  {   
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,250);
        this.setLocationRelativeTo(null);
        this.setLayout(new GridLayout(1,2));        
        Panel1 p1 = new Panel1();
        Panel2 p2 = new Panel2();
        Data.panel2 = p2; // getting the reference of panel2    
        this.add(p1);
        this.add(p2);       
        this.setVisible(true);
    }
}

class Panel1 extends JPanel
{
    public Panel1(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel1:"));
        JButton button = new JButton("Push");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){         
                Data.text = "text"; // changing commonly accessed data              
                Data.panel2.setBackground(Color.PINK); // this works
                //Data.panel2.invalidate();
                //Data.panel2.validate();
                //Data.panel2.updateUI();
                //Data.panel2.revalidate();
                Data.panel2.repaint();                // this does not work             
            }
        });
        this.add(button);       
    }   
}

class Panel2 extends JPanel
{
    public Panel2(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel2:"));     
        add(new JLabel(Data.text));
    }   
}

class Data
{   
    public static JPanel panel2 = null; 
    public static String text = "ooooo";
}

解决方案:

感谢 Austin Patel 的建议,我得以实现我的目标,甚至刷新(重绘)panel2。为此,在 Panel2 class 中创建了 update 方法。所有 panel2 更改都在那里进行,然后从 update 调用 revalidate()repaint() 方法刷新 panel2。

问题是 refresh/repaint 问题,但您从未真正更改过 JLabel 的文本。更改 Data.text 实际上不会更改 JLabel 的文本。您必须调用 JLabel 的 setText 方法。如果您尝试在更改 Data.text 后立即打印出 JLabel 的文本,您会注意到 JLabel 的文本没有更改。这是我的解决方案:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class Repaint_Test
{
    public static void main(String args[]){
        Frame frame = new Frame();
    }
}

class Frame extends JFrame
{
    private static final long serialVersionUID = 1L;
    public Frame()  {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,250);
        this.setLocationRelativeTo(null);
        this.setLayout(new GridLayout(1,2));
        Panel1 p1 = new Panel1();
        Panel2 p2 = new Panel2();
        Data.panel2 = p2; // getting the reference of panel2    
        this.add(p1);
        this.add(p2);
        this.setVisible(true);
    }
}

class Panel1 extends JPanel
{
    private static final long serialVersionUID = 1L;
    public Panel1(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel1:"));
        JButton button = new JButton("Push");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Data.text = "text"; // changing commonly accessed data              
                Data.panel2.setBackground(Color.PINK); // this works
                //Data.panel2.invalidate();
                //Data.panel2.validate();
                //Data.panel2.updateUI();
                //Data.panel2.revalidate();
//                Data.panel2.repaint();                // this does not work
                Data.panel2.updateText(Data.text);
            }
        });
        this.add(button);
    }
}

class Panel2 extends JPanel
{
    private static final long serialVersionUID = 1L;
    private JLabel label;

    public Panel2(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel2:"));

        label = new JLabel("Initial Text");
        add(label);
    }

    public void updateText(String text) {
        label.setText(text);
    }
}

class Data
{
    public static Panel2 panel2 = null;
    public static String text = "ooooo";
}