Java 为什么我不能将JPanel添加到JFrame?
代码如下:Java 为什么我不能将JPanel添加到JFrame?,java,user-interface,swing,jframe,jpanel,Java,User Interface,Swing,Jframe,Jpanel,代码如下: import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.event.*; import java.awt.*; public class GameWindow { private String[] players; private JFrame frame; // C
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.*;
import java.awt.*;
public class GameWindow {
private String[] players;
private JFrame frame;
// Constructor.
public GameWindow(String[] players) {
this.players = players;
}
// Start the window in the EDT.
public void start() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showWindow();
controller.start();
}
});
}
// Defines the general properties of and starts the window.
public void showWindow() {
frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setVisible(true);
}
// The thread controlling changes of panels in the main window.
private Thread controller = new Thread() {
public void run() {
frame.add(generatePartnerSelectionPanel());
frame.invalidate();
frame.validate();
}
};
// Generate the panel for the selection of a partner.
private JPanel generatePartnerSelectionPanel() {
JPanel panel = new JPanel();
panel.add(new JLabel("Pleas select a partner:"));
return panel;
}
}
我应该看到“请选择合作伙伴”,但我没有。为什么?
我想这是因为我没有从线程的run方法中看到frame
添加:
可能我需要在事件调度线程中执行所有更新。。。我现在就查
添加了2:
我试图修改控制器的代码。这无助于:
private Thread controller = new Thread() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.getContentPane().add(generatePartnerSelectionPanel());
frame.invalidate();
frame.validate();
}
});
}
};
添加了3:
嗯。以下是代码的完整版本(不起作用):
添加了4:
以下代码也不起作用。顺便问一下,我为什么要从开始
中删除调用器
?我确实需要在事件分派线程中启动GUI,然后invokelater
执行此操作
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.*;
import java.awt.*;
public class GameWindow {
private String[] players;
private JFrame frame;
// Constructor.
public GameWindow(String[] players) {
this.players = players;
}
// Start the window in the EDT.
public void start() {
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
showWindow();
controller.start();
// }
// });
}
// Defines the general properties of and starts the window.
public void showWindow() {
frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setVisible(true);
}
// The thread controlling changes of panels in the main window.
private Thread controller = new Thread() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.getContentPane().add(generatePartnerSelectionPanel());
frame.invalidate();
frame.validate();
}
});
}
};
// Generate the panel for the selection of a partner.
private JPanel generatePartnerSelectionPanel() {
JPanel panel = new JPanel();
panel.add(new JLabel("Pleas select a partner:"));
return panel;
}
}
添加了5:
我解决了这个问题
start
和showWindow
方法。我从主程序调用了错误的方法(showWindow
,而不是start
)。因此,我替换了start
方法(以避免与线程开头混淆),然后从主类调用startWindow
,它解决了问题您正在从
invokeLater
调用创建更新线程。这不是使用调用器的方式<代码>调用器用于从单独的线程更新UI组件。调用invokeLater
,从单独的线程传递更新UI组件的Runnable
有关更多信息,请参阅
例如:
// Start the window in the EDT.
public void start() {
showWindow();
controller.start();
}
// Defines the general properties of and starts the window.
public void showWindow() {
frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setVisible(true);
}
// The thread controlling changes of panels in the main window.
private Thread controller = new Thread() {
public void run() {
// some long running process, I assume, but at
// some point you want to update UI:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.add(generatePartnerSelectionPanel());
frame.invalidate();
frame.validate();
}
});
}
};
你是对的。我同意我需要使用invokeLater更新GUI组件。我在代码中做了这些更改(我在问题的更正中也提到了这一点)。但是它并没有解决问题。您是否按照我的建议更改了
start
?好吧,我从start
中删除了invokeLater
,它没有任何帮助。此外,我认为我们应该在那里使用invokeLater
。据我所知,GUI应该在事件调度线程中启动,并且invokeLater会这样做。re:您的更新。您是否按照我的建议更改了start
?您仍然需要从start
方法中删除invokelater
调用。是的,我删除了invokelater
。代码仍然不起作用。顺便说一句,我认为我不应该从一开始就删除invokeLater
,因为它在事件调度线程中启动GUI(这是应该的)。我还意识到,在重新更新我的帖子之后,您从未调用过它。。。。
// Start the window in the EDT.
public void start() {
showWindow();
controller.start();
}
// Defines the general properties of and starts the window.
public void showWindow() {
frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setVisible(true);
}
// The thread controlling changes of panels in the main window.
private Thread controller = new Thread() {
public void run() {
// some long running process, I assume, but at
// some point you want to update UI:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.add(generatePartnerSelectionPanel());
frame.invalidate();
frame.validate();
}
});
}
};