简体   繁体   English

在jpanel.paintComponent()中调用setText()方法时,为什么JLabel不刷新?

[英]Why JLabel isn't refreshing when setText() method is called within jpanel.paintComponent()?

After searching for more than 3 hours for similar problem and didn't find any clue i hope i will get my rescue hear. 经过3个多小时的类似问题搜索,但没有找到任何线索,我希望我能听到我的救助消息。 I'm trying to write a problem that stores in queue up to 5 lines that are draw inside JPanel using paintComponent() method. 我正在尝试编写一个问题,该问题将使用paintComponent()方法在JPanel中绘制的最多5行存储在队列中。 Also i want to update JLabel text to count size of queue. 我也想更新JLabel文本以计算队列大小。 I'm trying to update the JLabel by setText method inside Jpanel.paintComponent() method. 我正在尝试通过Jpanel.paintComponent()方法内的setText方法更新JLabel。 The problem that i have encountered and can't overcome on them are: 1. When paintComponent runs the line: label.setText("... "+ lineQueue.size()); 我遇到的无法解决的问题是:1.当paintComponent运行以下行时:label.setText(“ ...” + lineQueue.size()); inside DrawPanel class i get an exception means that label is equal to null. 在DrawPanel类中,我得到一个异常,表示label等于null。 How does it possible if DrawPanel constructor initialized it? 如果DrawPanel构造函数初始化它,怎么可能? 2. In order to overcome first problem i put if (label != null) than label.setText("... " + lineQueue.size()) but label text isn't updated no matter what happen to JFrame . 2.为了克服第一个问题,我把if(label!= null)设置为label.setText(“ ...” + lineQueue.size()), 但是无论JFrame发生什么,标签文本都不会更新 Can someone explain me what is the problem? 有人可以向我解释什么问题吗? It makes me crazy :( 这让我发疯了:(

My Code (4 files): 我的代码(4个文件):

   public class Point
{
    private int x;
    private int y;
    public Point (int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }
}

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class MyLine
{
    private Point p1;
    private Point p2;
    private Color color;

    public MyLine (Point point1 ,Point point2, Color color)
    {
        p1 = point1;
        p2 = point2;
        this.color = color;
    }

    public void drawLine (Graphics g)
    {
        g.setColor(color);
        g.drawLine (p1.getX(),p1.getY(),p2.getX(),p2.getY());
    }

}

import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.util.Queue;

public class DrawPanel extends JPanel
{

    private LinkedList<MyLine> lineQueue;
    private JLabel label;

    public DrawPanel(JLabel label)
    {
        label = this.label;
        lineQueue = new LinkedList <MyLine>();
    }   

    public void paintComponent (Graphics g)
    {

        super.paintComponent(g);

        Random rand = new Random();
        Point p1 = new Point (rand.nextInt(400),rand.nextInt(400));
        Point p2 = new Point (rand.nextInt(400),rand.nextInt(400));
        Color color = new Color (rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));

        if (lineQueue.size() >= 5)
            lineQueue.remove();
        lineQueue.add(new MyLine (p1,p2,color));

        ListIterator<MyLine> iterator = lineQueue.listIterator(0);
        while (iterator.hasNext() == true)
            iterator.next().drawLine(g);
        if (label!=null)
        {
            label.setText("... "+ lineQueue.size());
        }

    }
}

import java.awt.Color;
import javax.swing.*;
import java.awt.BorderLayout;

public class TestDrawLine
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame ();
        frame.setLayout(new BorderLayout());
        frame.setSize(400,350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label = new JLabel();

        DrawPanel drawPanel = new DrawPanel(label);
        drawPanel.setBackground(Color.BLACK);
        frame.add (drawPanel,BorderLayout.CENTER);

        JPanel southPanel = new JPanel();
        southPanel.add (label);
        frame.add (southPanel,BorderLayout.SOUTH);

        frame.setVisible(true);

    }
}
label = this.label;

This initializes the argument with the instance variable. 这将使用实例变量初始化参数。 You need to do the exact inverse: 您需要进行精确的逆运算:

this.label = label;

which initializes the instance variable with the argument. 使用参数初始化实例变量。

In order to overcome first problem i put if (label != null) than label.setText("... " + lineQueue.size()) 为了克服第一个问题,我把if(label!= null)设置为label.setText(“ ...” + lineQueue.size())

That can't possibly fix the issue. 那不可能解决这个问题。 All this does is that it doesn't do anything anymore instead of causing an exception. 所有这一切都是因为它不再执行任何操作,而是导致异常。 The label is still null, and you're just not doing anything with it anymore. 该标签仍然为空,您将不再使用它。

That said: the paintComponent() method is not supposed to modify the state of your component (or of anything else). 就是说: paintComponent()方法不应该修改组件(或其他任何组件)的状态。 It's only supposed to paint the component. 它仅应绘制组件。 That method will be called many times, whenever swing decides that the component needs to be repainted. 每当swing确定需要重新绘制组件时,该方法将被调用多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM