繁体   English   中英

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

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

经过3个多小时的类似问题搜索,但没有找到任何线索,我希望我能听到我的救助消息。 我正在尝试编写一个问题,该问题将使用paintComponent()方法在JPanel中绘制的最多5行存储在队列中。 我也想更新JLabel文本以计算队列大小。 我正在尝试通过Jpanel.paintComponent()方法内的setText方法更新JLabel。 我遇到的无法解决的问题是:1.当paintComponent运行以下行时:label.setText(“ ...” + lineQueue.size()); 在DrawPanel类中,我得到一个异常,表示label等于null。 如果DrawPanel构造函数初始化它,怎么可能? 2.为了克服第一个问题,我把if(label!= null)设置为label.setText(“ ...” + lineQueue.size()), 但是无论JFrame发生什么,标签文本都不会更新 有人可以向我解释什么问题吗? 这让我发疯了:(

我的代码(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.label = label;

使用参数初始化实例变量。

为了克服第一个问题,我把if(label!= null)设置为label.setText(“ ...” + lineQueue.size())

那不可能解决这个问题。 所有这一切都是因为它不再执行任何操作,而是导致异常。 该标签仍然为空,您将不再使用它。

就是说: paintComponent()方法不应该修改组件(或其他任何组件)的状态。 它仅应绘制组件。 每当swing确定需要重新绘制组件时,该方法将被调用多次。

暂无
暂无

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

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