当来自另一个方法的 setText 时,JPanel 上的 JLabel 不更新
JLabel on JPanel doesn't update when setText from another method
感谢您的帮助。我是一个新手,正在尝试一些非常简单的事情。
我想要一个 window (面板),其中包含每隔几秒更改一次的文本 (JLabel)。除了正在更新的 JLabel(文本确实发生了变化)之外,整个应用程序都运行良好,但它没有显示在 window(面板)上。 window 从头到尾保持相同的文本,即使我有检查点告诉我 JLabel 的文本正在更新和更改。我到处寻找相同的问题并找到了一些评论,但 none 提供了帮助。有人说是 ActionListener,但当我摆脱它时,它并没有什么不同。有人说线程中的循环不允许 JLabel 刷新,所以我尝试让 JLabel 和它的更新在一个单独的线程上进行,但也没有用。我唯一没有尝试过的是使用 swingWorker(不太了解如何)。线程尝试使用具有可运行方法的线程 class。同样,这样一切都完美无缺,只是 JLabel 没有重新绘制自身。
这是没有线程的代码。一切尽在其中 class.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class SWUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel panel;
JLabel label;
private Font font;
//Set up the interface. SWUI is working perfectly
SWUI() {
System.out.println("run started"); //just to check that the method gets called.
panel = new JPanel();
panel.setLayout(new BorderLayout(1, 1));
panel.setPreferredSize(new Dimension(1000, 400));
panel.setBackground(Color.white);
getContentPane().add(panel);
label = new JLabel("Ready"); //These four statements are working
label.setHorizontalAlignment(JLabel.CENTER);
font = new Font("Comic Sans MS", Font.PLAIN, 70);
label.setFont(font);
panel.add(label, BorderLayout.CENTER);
System.out.println("run done and draw to start");
label.setText("label is updatable"); //Checks that the whole method gets done and that the label is indeed able to get updated. It works.
}
// draw() mostly sets the Frame up, names it, sets closing behavior. It's working.
public void draw() {
System.out.println("draw started"); //To check that the method is getting invoked.
SWUI frame = new SWUI();
frame.setTitle("Sight Words Rock");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
System.out.println("draw done"); //To check that the whole method went through. It's all working.
}
//Should change the label to 4 sentences every 1 second. The text of the label does change, but it doesn't show on the interface (panel).
private void updateLabel(String word){
label.setText(word);
//label.revalidate(); //I tried these three statements one at a time and both together and they made no difference.
//label.updateUI();
//label.repaint();
System.out.println("The label now is: "+ label.getText()); //This confirms that the setText() is indeed changing the text
// of the label, but it doesn't show on the interface.
}
//Main gets the draw() started to initiate the interface and gets the sentences changed and sent to updateLabel() every second.
public static void main(String args[])
throws InterruptedException {
SWUI ui = new SWUI();
ui.draw();
String importantInfo[] = {
"Mares eat oats",
"Dogs eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0;
i < importantInfo.length;
i++) {
//Pause for 1 second
Thread.sleep(1000);
//Print a message
System.out.println(importantInfo[i]);
ui.label.setText(importantInfo[i]);
ui.updateLabel(importantInfo[i]);
}
}
//This will come later once I figure how to fix the label. Someone else suggested that the problem were the listeners not letting the thread
// do the repainting(), but I tried getting rid of the "implements ActionListener" and the actionPerformed methods and it didn't make a
// difference. It still didn't work.
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
您在 SWUI
中的 draw
方法创建了一个全新的框架,而您的更新转到了从未真正设置为可见的初始框架。要修复,您的 draw 方法应该看起来更像这样。
public void draw() {
System.out.println("draw started"); //To check that the method is getting invoked.
setTitle("Sight Words Rock");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
pack();
setVisible(true);
System.out.println("draw done"); //To check that the whole method went through. It's all working.
}
感谢您的帮助。我是一个新手,正在尝试一些非常简单的事情。 我想要一个 window (面板),其中包含每隔几秒更改一次的文本 (JLabel)。除了正在更新的 JLabel(文本确实发生了变化)之外,整个应用程序都运行良好,但它没有显示在 window(面板)上。 window 从头到尾保持相同的文本,即使我有检查点告诉我 JLabel 的文本正在更新和更改。我到处寻找相同的问题并找到了一些评论,但 none 提供了帮助。有人说是 ActionListener,但当我摆脱它时,它并没有什么不同。有人说线程中的循环不允许 JLabel 刷新,所以我尝试让 JLabel 和它的更新在一个单独的线程上进行,但也没有用。我唯一没有尝试过的是使用 swingWorker(不太了解如何)。线程尝试使用具有可运行方法的线程 class。同样,这样一切都完美无缺,只是 JLabel 没有重新绘制自身。
这是没有线程的代码。一切尽在其中 class.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class SWUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel panel;
JLabel label;
private Font font;
//Set up the interface. SWUI is working perfectly
SWUI() {
System.out.println("run started"); //just to check that the method gets called.
panel = new JPanel();
panel.setLayout(new BorderLayout(1, 1));
panel.setPreferredSize(new Dimension(1000, 400));
panel.setBackground(Color.white);
getContentPane().add(panel);
label = new JLabel("Ready"); //These four statements are working
label.setHorizontalAlignment(JLabel.CENTER);
font = new Font("Comic Sans MS", Font.PLAIN, 70);
label.setFont(font);
panel.add(label, BorderLayout.CENTER);
System.out.println("run done and draw to start");
label.setText("label is updatable"); //Checks that the whole method gets done and that the label is indeed able to get updated. It works.
}
// draw() mostly sets the Frame up, names it, sets closing behavior. It's working.
public void draw() {
System.out.println("draw started"); //To check that the method is getting invoked.
SWUI frame = new SWUI();
frame.setTitle("Sight Words Rock");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
System.out.println("draw done"); //To check that the whole method went through. It's all working.
}
//Should change the label to 4 sentences every 1 second. The text of the label does change, but it doesn't show on the interface (panel).
private void updateLabel(String word){
label.setText(word);
//label.revalidate(); //I tried these three statements one at a time and both together and they made no difference.
//label.updateUI();
//label.repaint();
System.out.println("The label now is: "+ label.getText()); //This confirms that the setText() is indeed changing the text
// of the label, but it doesn't show on the interface.
}
//Main gets the draw() started to initiate the interface and gets the sentences changed and sent to updateLabel() every second.
public static void main(String args[])
throws InterruptedException {
SWUI ui = new SWUI();
ui.draw();
String importantInfo[] = {
"Mares eat oats",
"Dogs eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0;
i < importantInfo.length;
i++) {
//Pause for 1 second
Thread.sleep(1000);
//Print a message
System.out.println(importantInfo[i]);
ui.label.setText(importantInfo[i]);
ui.updateLabel(importantInfo[i]);
}
}
//This will come later once I figure how to fix the label. Someone else suggested that the problem were the listeners not letting the thread
// do the repainting(), but I tried getting rid of the "implements ActionListener" and the actionPerformed methods and it didn't make a
// difference. It still didn't work.
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
您在 SWUI
中的 draw
方法创建了一个全新的框架,而您的更新转到了从未真正设置为可见的初始框架。要修复,您的 draw 方法应该看起来更像这样。
public void draw() {
System.out.println("draw started"); //To check that the method is getting invoked.
setTitle("Sight Words Rock");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
pack();
setVisible(true);
System.out.println("draw done"); //To check that the whole method went through. It's all working.
}