为什么在我更改屏幕尺寸之前不会出现JButton;

时间:2014-03-17 18:21:26

标签: java swing jframe jpanel jbutton

当我运行程序时,直到我改变屏幕大小(setResizable = true;)才会出现JButtons。

这是我的代码:

公共类MainMenu扩展了JPanel {

Kingdomcraft kd;
Screen screen;
JButton playSP;
JButton playMP;
JButton settings;
JButton fullscreen;
JButton quit;
JButton createWorld;
JButton addServer;
JSlider sound;
JSlider light;
JList worldList;
JList serverList;
JTextField worldName;
JTextField serverName;
JTextField serverIP;
JButton addNewWorld;
JButton addNewServer;

private Preferences prefs;
private int soundLevel;
private int lightLevel;

public static boolean isFullscreen = false;
public static boolean serverNameFilled = false;
public static boolean serverIPFilled = false;

public void run() {

    kd = new Kingdomcraft();
    screen = new Screen();
    playSP = new JButton("Singleplayer");
    playMP = new JButton("Multiplayer");
    settings = new JButton("Settings");
    fullscreen = new JButton("Fullscreen");
    quit = new JButton("Quit");
    createWorld = new JButton("Create World");
    addServer = new JButton("Add Server");
    sound = new JSlider();
    light = new JSlider();
    prefs = Preferences.userNodeForPackage(MainMenu.class);
    soundLevel = prefs.getInt("SOUND_LEVEL", 50);
    lightLevel = prefs.getInt("LIGHT_LEVEL", 100);
    worldList = new JList();
    serverList = new JList();
    worldName = new JTextField();
    serverName = new JTextField();
    serverIP = new JTextField();
    addNewWorld = new JButton("Add");
    addNewServer = new JButton("Add");

    this.setBackground(Color.BLACK);

    if (kd.inMainMenu) {

        add(playSP);

        playSP.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                remove(sound);
                remove(light);
                remove(fullscreen);
                remove(addServer);
                remove(serverName);
                remove(serverIP);
                remove(addNewServer);
                repaint();
                add(createWorld);

                createWorld.setSize(110, 25);
                createWorld.setLocation(playSP.getX() - (playSP.getWidth() / 2) - 5, playSP.getY() + 35);

                createWorld.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        add(worldName);

                        worldName.setSize(110, 25);
                        worldName.setLocation(createWorld.getX(), createWorld.getY() + 35);

                        worldName.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {

                                add(addNewWorld);

                                addNewWorld.setSize(110, 25);
                                addNewWorld.setLocation(worldName.getX(), worldName.getY() + 35);
                            }
                        });
                    }
                });
            }
        });

        add(playMP);

        playMP.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                remove(sound);
                remove(light);
                remove(fullscreen);
                remove(createWorld);
                remove(worldName);
                remove(addNewWorld);
                repaint();

                add(addServer);

                addServer.setSize(100, 25);
                addServer.setLocation(playMP.getX() - (playMP.getWidth() / 2) - 5, playMP.getY() + 35);

                addServer.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        add(serverName);

                        serverName.setSize(100, 25);
                        serverName.setLocation(addServer.getX(), addServer.getY() + 35);

                        serverName.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {

                                MainMenu.serverNameFilled = true;
                            }
                        });

                        add(serverIP);

                        serverIP.setSize(100, 25);
                        serverIP.setLocation(serverName.getX(), serverName.getY() + 35);

                        serverIP.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {

                                MainMenu.serverIPFilled = true;
                            }
                        });

                        if (serverNameFilled && serverIPFilled) {

                            add(addNewServer);

                            addNewServer.setSize(100, 25);
                            addNewServer.setLocation(serverIP.getX(), serverIP.getY() + 35);

                            addNewServer.addActionListener(new ActionListener() {

                                public void actionPerformed(ActionEvent e) {

                                }
                            });
                        }
                    }
                });
            }
        });

        add(settings);

        settings.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                remove(createWorld);
                remove(addServer);
                remove(worldName);
                remove(serverName);
                remove(serverIP);
                remove(addNewWorld);
                remove(addNewServer);
                repaint();
                add(sound);

                sound.setSize(settings.getWidth(), settings.getHeight());
                sound.setLocation(settings.getX() + (settings.getWidth() / 2) + 5, settings.getY() + 35);
                sound.setOpaque(false);
                sound.setMinimum(0);
                sound.setMaximum(100);
                sound.setValueIsAdjusting(true);
                sound.setValue(soundLevel);
                sound.setToolTipText("Audio: " + soundLevel + "%");
                sound.addChangeListener(new ChangeListener() {

                    public void stateChanged(ChangeEvent e) {

                        soundLevel = sound.getValue();

                        sound.setToolTipText("Audio: " + soundLevel + "%");

                        prefs.putInt("SOUND_LEVEL", soundLevel);
                    }
                });

                add(light);

                light.setSize(settings.getWidth(), settings.getHeight());
                light.setLocation(sound.getX(), sound.getY() + 35);
                light.setOpaque(false);
                light.setMinimum(50);
                light.setMaximum(150);
                light.setValueIsAdjusting(true);
                light.setValue(lightLevel);
                light.setToolTipText("Brightness: " + lightLevel + "%");
                light.addChangeListener(new ChangeListener() {

                    public void stateChanged(ChangeEvent e) {

                        lightLevel = light.getValue();

                        light.setToolTipText("Brightness: " + lightLevel + "%");

                        prefs.putInt("LIGHT_LEVEL", lightLevel);
                    }
                });

                add(fullscreen);

                fullscreen.setSize(100, settings.getHeight());
                fullscreen.setLocation(settings.getX() + (settings.getWidth() / 2) - 50, light.getY() + 35);

                fullscreen.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        MainMenu.isFullscreen = true;
                    }
                });
            }
        });

        add(quit);

        quit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.exit(ABORT);
            }
        });
    }
}

}

我知道这是很多代码可以浏览,但我只是简单地难以理解。

2 个答案:

答案 0 :(得分:1)

在swing中添加UI组件时,您需要调用组件的revalidate方法才能使更改生效。

答案 1 :(得分:0)

无法准确判断您在做什么,但我不确定您是否应该不断删除/添加单个组件。

也许您应该使用Card Layout,这将允许您根据用户请求的特定功能删除/添加面板。