我在多台计算机上遇到了一个问题(这意味着它不仅仅是一台计算机给我的问题)。我使用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);
}
});
}
}
答案 0 :(得分:4)
您在调用
后添加按钮setVisible(true);
调用在该行之前绘制按钮的方法。
修改强>
如果您在GUI可见后添加按钮,则可能需要在添加按钮后调用validate()
。您可以在此处找到有关详细信息:http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#validate%28%29
答案 1 :(得分:0)
我也遇到了同样的问题,我做的是在创建所有面板后使框架可见。纽扣。
ex)
frame.setVisible(true);
frame.add(panel);
panel.setVisible(true);
panel.add(button);
button.setVisible(true);
因此,关键是在创建所有面板和按钮后使框架可见。
试试吧,也许它会对你有帮助。