【发布时间】:2014-07-21 22:24:52
【问题描述】:
在设计基于文本的游戏的过程中,我决定采用在 windows builder 中制作 GUI 的路线,请注意,我刚刚开始学习 GUI,所以请放轻松。
无论如何,问题是在程序启动时出现的假设是 GUI 的窗口实际上是空白的并且不允许我退出,所以我必须在 eclipse 中终止程序。
还要注意在主启动中执行时,它是作为mainGameGUI执行的,而不是在构造函数下。
这是代码。
private static JFrame frame;
private static JTextField txtBeginByTyping;
public static void mainGameGUI() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
final JTextArea textArea = new JTextArea();
textArea.setBounds(10, 415, 371, 205);
textArea.setRows(8);
textArea.setEditable(false);
frame.getContentPane().add(textArea);
JProgressBar progressBar = new JProgressBar();
progressBar.setBackground(Color.RED);
progressBar.setToolTipText("");
progressBar.setForeground(Color.RED);
progressBar.setValue(Player.currentHp);
progressBar.setMaximum(Player.maxHp);
progressBar.setBounds(401, 517, 173, 40);
frame.getContentPane().add(progressBar);
JLabel lblYourHealth = new JLabel("Health");
lblYourHealth.setHorizontalAlignment(SwingConstants.CENTER);
lblYourHealth.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
lblYourHealth.setBounds(401, 470, 173, 36);
frame.getContentPane().add(lblYourHealth);
JLabel lblDestructivePower = new JLabel("Destruction: " + Player.totalDamage);
lblDestructivePower.setHorizontalAlignment(SwingConstants.LEFT);
lblDestructivePower.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
lblDestructivePower.setBounds(401, 568, 173, 40);
frame.getContentPane().add(lblDestructivePower);
JLabel lblNewLabel = new JLabel("Gold Coins: " + Player.currency);
lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
lblNewLabel.setBounds(401, 619, 173, 32);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Realm of the Gods");
lblNewLabel_1.setBounds(10, 11, 564, 60);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Images go here");
lblNewLabel_2.setBounds(10, 82, 564, 322);
frame.getContentPane().add(lblNewLabel_2);
txtBeginByTyping = new JTextField();
txtBeginByTyping.setText("");
txtBeginByTyping.setBounds(10, 631, 381, 20);
frame.getContentPane().add(txtBeginByTyping);
txtBeginByTyping.setColumns(10);
txtBeginByTyping.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
inputReader.input = txtBeginByTyping.getText();
textArea.append(inputReader.input + "\n");
txtBeginByTyping.setText("");
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(380, 415, 11, 205);
frame.getContentPane().add(scrollPane);
JLabel lblNewLabel_3 = new JLabel("Town of Neo");
lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_3.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
lblNewLabel_3.setBounds(401, 416, 173, 40);
frame.getContentPane().add(lblNewLabel_3);
}
【问题讨论】:
-
您的描述听起来像是一个经典的 Swing 线程问题,错误我没有看到您发布了相关代码来解释这一点。这个“inputReader”到底是什么东西?我敢打赌,你最喜欢的饮料是 6 包,就是这个对象在捆绑你的 GUI。否则,我同意 Braj 提出的次要建议,1+ 对他的回答。
-
差不多但它实际上只是我创建的一种方法,但我也同意braj
-
你要不要给我们看这个方法?来吧,不要让我们悬而未决......就像你对last question所做的那样。
-
Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。
标签: java swing user-interface interface null