我意識到您需要使用事件調度線程來避免死機和死鎖,但我在這裏遇到了凍結問題。由於封裝EDT,我懷疑我有這個問題。當掃描儀等待用戶輸入時GUI凍結
這與主靜方法的類:
@SuppressWarnings("serial")
public class WelcomeScreen extends JFrame implements ActionListener {
/*
* initialize variables
*/
private JButton btnSubmit;
private JTextField nameInput;
private JLabel enterName;
public WelcomeScreen() {
/*
* build the welcome frame with all components
*/
setAlwaysOnTop(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setBounds(0, 0, 514, 256);
getContentPane().setLayout(null);
// welcome label
JLabel label_welcome = new JLabel("<html>Welcome to <br/>Game</html>");
label_welcome.setHorizontalAlignment(SwingConstants.CENTER);
label_welcome.setBounds(159, 11, 190, 32);
getContentPane().add(label_welcome);
// create the warrior button
btnSubmit = new JButton("Start");
btnSubmit.setBounds(209, 129, 89, 23);
btnSubmit.addActionListener(this);
btnSubmit.setFocusable(false);
getContentPane().add(btnSubmit);
enterName = new JLabel("Enter Player Name:");
enterName.setBounds(50, 60, 150, 50);
getContentPane().add(enterName);
nameInput = new JTextField();
nameInput.setBounds(212, 70, 125, 25);
getContentPane().add(nameInput);
// set frame visible
setVisible(true);
}
/*
* action listener
*/
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnSubmit)) {
dispose();
new Core();
}
}
/*
* <<<<<MAIN STATIC METHOD>>>>>
*/
public static void main(String[] args) {
// safely start the welcome frame
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WelcomeScreen();
}
});
}
}
而這是一個擴展的JFrame第二類。這是凍結的GUI。如果您查看gameEngine()方法,您會注意到String input = keyboard.next();,這是造成凍結。如果該行被註釋掉,GUI將顯示兩個字符串「test1」和「test2」。
@SuppressWarnings("serial")
public class Core extends JFrame {
/*
* initialize variables
*/
private Scanner keyboard;
private JTextArea textArea;
public Core() {
/*
* create the frame that displays output messages
*/
setBounds(0, 0, 800, 400);
setResizable(false);
setLocationRelativeTo(null);
setAlwaysOnTop(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 774, 349);
panel.setLayout(null);
getContentPane().add(panel);
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
scroll.setBounds(0, 0, 774, 349);
textArea.setBounds(10, 11, 443, 279);
panel.add(scroll);
// THIS ISN'T EVEN SHOWING UP
appendMessage("test1");
// set frame visible
setVisible(true);
// NEITHER IS THIS
appendMessage("test2");
/*
* start game engine
*/
gameEngine();
}
/*
* start gameEngine
*/
public void gameEngine() {
// instantiate scanner
keyboard = new Scanner(System.in);
// <<<<<<<<<<<<<<<THIS HERE IS CAUSING THE GUI TO FREEZE>>>>>>>>>>>>>>>
String input = keyboard.next();
}
// append a message to the GUI
public void appendMessage(String s) {
textArea.append("> " + s + "\n");
}
}
[併發中的擺動](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)的原因。現在,你需要問「爲什麼?」你是否試圖在GUI中從控制檯獲取用戶輸入? – MadProgrammer
避免使用'null'佈局,像素完美的佈局是現代UI設計中的幻想。影響組件的個體大小的因素太多,其中沒有一個可以控制。 Swing旨在與佈局經理一起工作,放棄這些將導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正 – MadProgrammer
@MadProgrammer長話短說,我正在研究一種簡單的基於文本的方法,基於遊戲。我發現使JTextField「行爲」等同於控制檯輸入非常困難。該程序不會等待JTextField輸入。這就是爲什麼在這種情況下,用戶在控制檯中鍵入命令,並在GUI中顯示消息。 – Dragneel