gpt4 book ai didi

java - 为什么在我更改屏幕尺寸之前 JButton 不出现?

转载 作者:行者123 更新时间:2023-12-01 13:16:04 26 4
gpt4 key购买 nike

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

这是我的代码:

公共(public)类 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);
}
});
}
}

}

我知道这是很多需要浏览的代码,但我只是被难住了。

最佳答案

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

关于java - 为什么在我更改屏幕尺寸之前 JButton 不出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com