繁体   English   中英

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

[英]JButtons don't show until I minimize and bring the window up again

我在多台计算机上遇到问题(这不只是一台计算机给我带来了问题)。 我将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

我也遇到了同样的问题,我要做的就是在创建所有面板和按钮后使框架可见。

EX)

frame.setVisible(true);
frame.add(panel);
panel.setVisible(true);
panel.add(button);
button.setVisible(true);

因此,关键是在创建所有面板和按钮之后使框架可见。

尝试一下,也许会对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM