Java:GUI不响应吗?

时间:2014-06-01 19:42:58

标签: java swing user-interface interface null

在设计基于文本的游戏的过程中,我决定采用在Windows构建器中制作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);
}

1 个答案:

答案 0 :(得分:4)

在添加其中的所有组件后,最后调用frame.setVisible(true);

有些观点:

  1. 根本不要使用null布局,而是使用正确的布局管理器。

    阅读更多A Visual Guide to Layout Managers,其中详细描述了所有布局管理器以及示例代码。

  2. 使用SwingUtilities.invokeLater()确保EDT已正确初始化。

    了解更多