Java Swing Jpanel不会刷新



在我的秋千应用程序中,我有两个面板。第一个面板有一个按钮。当我单击此按钮时,一些数据会更改,我希望第二个面板的内容也更改,从而显示新数据(通过Jlabel(。Repaint((不起作用。我尝试了各种更新(请参阅代码中的注释(。

我有一个对第二个面板的引用。我可以使用该参考来更改第二个面板颜色,但我无法重新粉刷它。我还可以在第二个面板中提及jlabel,然后更改其文本,但是这个问题的重点是将面板2刷新。

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(的建议,我能够实现自己的目标,甚至可以刷新(重新粉刷(面板2。为此,在Panel2类中创建了 Update 方法。所有面板2更改均在此进行,然后 Revalidate(( and Repaver((方法从 UPDACE 调用以刷新面板2。

问题是一个刷新/重新粉刷问题,而是实际上从未更改jlabel的文字。更改Data.text实际上并没有更改Jlabel的文本。您必须调用Jlabel的setText方法。如果您尝试在更改数据后立即打印出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";
}

最新更新