在寻找类似问题超过 3 个小时后,没有找到任何线索,我希望我能得到救援。 我正在尝试编写一个问题,在队列中存储最多 5 行,这些行是使用 PaintComponent() 方法在 JPanel 内绘制的。我还想更新 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;
它用参数初始化实例变量。
In order to overcome first problem i put if (label != null) than label.setText("... " + lineQueue.size())
这不可能解决问题。所做的只是它不再执行任何操作而不是引发异常。该标签仍然为空,您只是不再用它做任何事情。
也就是说:paintComponent()
方法不应该修改组件(或其他任何东西)的状态。它只是应该绘制组件。每当 swing 决定需要重新绘制组件时,该方法就会被调用多次。
关于java - 为什么在 jpanel.paintComponent() 中调用 setText() 方法时 JLabel 不刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55985861/