我的编使用框架和面板,它们是循环创建的。 框架被创建并正常显示,但是由于某些原因 面板未创建和/或未显示。
任何想法。
欢呼。
class GUIThread extends Thread
{
public boolean threadClose;
public GUIThread()
{
SwingUtilities.invokeLater(this);
}
@Override
public void run()
{
JFrame lFrame = null;
JPanel lPanel = null;
boolean lFrameFlag = false;
threadClose = false;
while( !threadClose )
{
if(lFrameFlag == false)
{
lPanel = new JPanel();
lPanel.setSize(580,356);
lPanel.setLocation(10,10);
lPanel.setVisible(true);
lPanel.setBorder( BorderFactory.createLineBorder(Color.BLACK) );
lFrame = new JFrame();
lFrame.setSize(600,400);
lFrame.setLocation(200,200);
lFrame.add(lPanel);
lFrame.setVisible(true);
lFrameFlag = true;
}
}
}
}
public class GUITestHarness
{
public static void main(String[] args)
{
GUIThread lGUIThread = new GUIThread();
}
}
运行框架时显示,但面板不显示。
答案 0 :(得分:1)
问题是while循环。如果执行继续,将显示该面板,但是由于while循环是一个无限循环,因此该框架永远不会更新,因为执行被卡在了循环中。
因此,如果尝试不使用无限循环,它应该可以工作。像这样:
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
//here it's better to implement Runnable than extend Thread
//in this case it's the same, but extending thread can lead to strange problems in other cases
public class Frame implements Runnable {
public static void main(String[] args) {
new Frame();
}
public Frame() {
SwingUtilities.invokeLater(this);
}
@Override
public void run() {
JFrame lFrame = null;
JPanel lPanel = null;
lPanel = new JPanel();
lPanel.setSize(580, 356);
lPanel.setLocation(10, 10);
lPanel.setVisible(true);
lPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lFrame = new JFrame();
lFrame.setSize(600, 400);
lFrame.setLocation(200, 200);
lFrame.add(lPanel);
lFrame.setVisible(true);
}
}