再次更新JFrame [英] updating JFrame again
本文介绍了再次更新JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面显示的代码将向JFrame添加3个JLabel,然后删除3个JLabel。
2秒后它将重新绘制3个JLabel。
The following code shown below will add 3 JLabels to a JFrame and then remove the 3 JLabels. After 2 seconds it will repaint back the 3 JLabels.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class RepaintFrameTest extends JFrame {
private JPanel panel = new JPanel();
private JLabel labelOne = new JLabel("label1");
private JLabel labelTwo = new JLabel("label2");
private JLabel labelThree = new JLabel("label3");
public RepaintFrameTest() {
panel.add(labelOne);
panel.add(labelTwo);
panel.add(labelThree);
add(panel);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(1024,768);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
RepaintFrameTest frameOne = new RepaintFrameTest();
frameOne.getContentPane().removeAll();
frameOne.getContentPane().validate();
frameOne.getContentPane().repaint();
new Thread(new RepaintThread()).start();
}
}
class RepaintThread implements Runnable {
@Override
public void run() {
try {
Thread.sleep(2000);
}catch (InterruptedException interruptedException) {
System.out.println( "Thread is interrupted when it is sleeping" +interruptedException);
}
RepaintFrameTest frameTwo = new RepaintFrameTest();
frameTwo.getContentPane().validate();
frameTwo.getContentPane().repaint();
}
}
我遇到的一个小问题是它重新粉刷成一个新框架(frameTwo)而不是旧框架(frameOne)。
A slight problem I faced is that it's repainting to a new frame(frameTwo) instead of an old one(frameOne).
如何使其重新绘制现有框架而不是重新绘制新框架?
How do I make it such that it will repaint the existing frame instead of repainting to a new one?
推荐答案
- Simpy使用CardLayout来交换视图。
- 使用Swing Timer而不是当前的后台线程,因为您当前的代码不是Swing线程安全的,并且存在有害的不可预测线程异常的风险。
- 我会给主要的JPanel CardLayout,然后使用合适的String常量添加一个持有JPanel的JLabel。
- 然后我使用JPanel添加到主CardLayout,这是一个空的JLabel,在add方法中使用另一个String常量。即,
add(new JLabel(),BLANK_COMPONENT);
- 然后我在Swing Timer中只需调用
我的CardLayout对象上的next()
,传入主要的CardLayout-using组件:cardLayout.next(myMainJPanel);
- Simpy use a CardLayout to swap views in and out.
- Use a Swing Timer rather than your current background Thread as your current code is not Swing thread safe and carries risk of pernicious unpredictable thread exceptions.
- I would give the main JPanel the CardLayout, and then add a JLabel holding JPanel with a suitable String constant.
- I'd then add to the main CardLayout using JPanel, an empty JLabel using another String constant in the add method. i.e.,
add(new JLabel(), BLANK_COMPONENT);
- I'd then in my Swing Timer simply call
next()
on my CardLayout object, passing in the main CardLayout-using component:cardLayout.next(myMainJPanel);
例如,
For example,
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class RepaintTest extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final int LABEL_COUNT = 3;
private static final String LABEL_PANEL = "label panel";
private static final Object BLANK_COMPONENT = "blank component";
private static final int TIMER_DELAY = 2000;
private CardLayout cardLayout = new CardLayout();
public RepaintTest() {
JPanel labelPanel = new JPanel();
for (int i = 0; i < LABEL_COUNT; i++) {
labelPanel.add(new JLabel("Label " + (i + 1)));
}
setLayout(cardLayout);
add(labelPanel, LABEL_PANEL);
add(new JLabel(), BLANK_COMPONENT);
new Timer(TIMER_DELAY, new TimerListener()).start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.next(RepaintTest.this);
}
}
private static void createAndShowGui() {
RepaintTest mainPanel = new RepaintTest();
JFrame frame = new JFrame("RepaintTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这篇关于再次更新JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文