java - 在我最小化并再次打开窗口之前,JButtons 不会显示

标签 java swing jframe jbutton

我在不止一台计算机上遇到问题(这意味着不只是一台计算机给我这个问题)。我使用 Eclipse 作为我的 Java IDE,但我似乎无法找到我的问题的答案。我已经将几个 JButton 添加到我的视频游戏的启动器中,当我调试项目时,按钮不会出现,直到我将鼠标滑到它们上面,或者最小化并重新打开窗口。我已经导出项目并且大多数按钮都可以使用,但有时我仍然必须最小化并重新打开窗口。我不想必须这样做,因为它将成为一款商业视频游戏,人们不应该这样做。

如果您需要,这里是我的启动器窗口的代码:

package net.renderedspoon.sombrero.gui;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import net.renderedspoon.sombrero.Configuration;
import net.renderedspoon.sombrero.Game;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import net.renderedspoon.sombrero.RunGame;

public class Launcher extends JFrame {
private static final long serialVersionUID = 1L;

protected JPanel window = new JPanel();
private JButton play, options, help, exit;
private Rectangle rplay, roptions, rhelp, rexit;

Configuration config = new Configuration();

private int width = 250;
private int height = 350;
protected int button_width = 80;
protected int button_height = 40;

public Launcher(int id) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    setTitle("Launcher || Sombrero");
    setSize(new Dimension(width, height));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(window);
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
    window.setLayout(null);
    requestFocus();
    if(id == 0) {
        drawButtons();
    }
}

private void drawButtons() {
    play = new JButton("Play");
    rplay = new Rectangle((width / 2) - (button_width / 2), 50, button_width, button_height);
    play.setBounds(rplay);
    window.add(play);

    options = new JButton("Options");
    roptions = new Rectangle((width / 2) - (button_width / 2), 100, button_width, button_height);
    options.setBounds(roptions);
    window.add(options);

    help = new JButton("Help");
    rhelp = new Rectangle((width / 2) - (button_width / 2), 150, button_width, button_height);
    help.setBounds(rhelp);
    window.add(help);

    exit = new JButton("Quit");
    rexit = new Rectangle((width / 2) - (button_width / 2), 200, button_width, button_height);
    exit.setBounds(rexit);
    window.add(exit);

    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: PLAY");
            dispose();
            new RunGame();
        }
    });

    options.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: OPTIONS");
            dispose();
            new Options();
        }
    });

    help.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: HELP");
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: EXIT");
            System.exit(0);
        }
    });
}


}

最佳答案

你在调用之后添加按钮

setVisible(true);

在该行之前调用绘制按钮的方法。

编辑:

如果您在 GUI 已经可见后添加按钮,您可能需要在添加按钮后调用 validate()。您可以在此处找到更多详细信息:http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#validate%28%29

关于java - 在我最小化并再次打开窗口之前,JButtons 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491469/

相关文章:

java - `name` 作为 freemarker 中断中的变量名

java - JOptionPane 中的排列

java - 如何将排序的 Map 转换为 JTable 的二维数组

java - 如何停止java桌面应用程序中移动鼠标的无限循环

java - 错误: opening Service Client using correct connection string

java - 尝试在 Play 框架中运行应用程序时出现错误

java - 如何在两个测试类之间共享ExternalResource?

java - 父类(super class)中的按钮仍在子类中

java - 在 borderlayout 中使用 JPanel 错误打包 JFrame

java - JFrame 上的 MouseListener 只监听边框