簡體   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