3

我在不止一台计算机上遇到问题(这意味着不仅仅是一台计算机给我带来了问题)。我使用 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);
        }
    });
}


}
4

2 回答 2

5

您在调用后添加按钮

setVisible(true);

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

编辑:

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

于 2013-05-10T22:13:59.017 回答
0

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

前任)

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

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

试试看,也许它会帮助你。

于 2016-11-30T19:38:11.507 回答