我是 Java UI 和 Swing 的新手,我不明白为什么会这样。
public class ZAsciiMapWindow extends JFrame implements KeyListener, Runnable {
...
// SWING STUFF
private JTextArea displayArea = null;
private JTextField typingArea = null;
public ZAsciiMapWindow(final ZMap map, final ZHuman player) {
super("ZAsciiMapWindow");
this.map = map;
this.player = player;
}
...
public void show() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(this);
}
private void addComponentsToPane() {
this.typingArea = new JTextField(20);
this.typingArea.addKeyListener(this);
this.typingArea.setFocusTraversalKeysEnabled(false);
this.displayArea = new JTextArea();
this.displayArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(this.displayArea);
scrollPane.setPreferredSize(new Dimension(375, 125));
getContentPane().add(this.typingArea, BorderLayout.PAGE_START);
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI() {
//Create and set up the window.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
this.addComponentsToPane();
//Display the window.
this.pack();
this.setVisible(true);
}
@Override
public void run() {
createAndShowGUI();
}
}
然后,当我new ZAsciiMapWindow(x, y).show()
从 my调用时main()
,它永远不会显示 JFrame。如果我调试我发现它一直createAndShowGUI()
在调用无限。
为什么会这样?提前致谢。