您好我试图制作一个JList
,但需要2个参数。该列表是服务器列表。
public class MainMenu extends JPanel {
Kingdomcraft kd;
Screen screen;
JButton playSP;
JButton playMP;
JButton settings;
JButton quit;
JButton createWorld;
JButton addServer;
JSlider sound;
JSlider light;
@SuppressWarnings("rawtypes")
JList worldList;
JList serverList;
private Preferences prefs;
private int soundLevel;
private int lightLevel;
public void run() {
kd = new Kingdomcraft();
screen = new Screen();
playSP = new JButton("Singleplayer");
playMP = new JButton("Multiplayer");
settings = new JButton("Settings");
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", 50);
worldList = new JList();
serverList = new JList();
this.setBackground(Color.BLACK);
this.setLayout(null);
if (kd.inMainMenu) {
add(createWorld);
createWorld.setBounds(170, 10, 150, 35);
add(playSP);
playSP.setBounds(10, 10, 150, 35);
playSP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(addServer);
repaint();
add(createWorld);
createWorld.setBounds(170, 10, 150, 35);
}
});
add(playMP);
playMP.setBounds(10, 60, 150, 35);
playMP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(createWorld);
repaint();
add(addServer);
addServer.setBounds(170, 60, 150, 35);
}
});
add(settings);
settings.setBounds(10, 110, 150, 35);
settings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(createWorld);
remove(addServer);
repaint();
add(sound);
sound.setBounds(170, 110, 150, 35);
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.setBounds(330, 110, 150, 35);
light.setMinimum(0);
light.setMaximum(100);
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(quit);
quit.setBounds(10, 160, 150, 35);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(ABORT);
}
});
}
}
}
单击多人游戏JButton
时会显示世界列表。
此外,当您单击多人游戏JButton
时,JButton
显示为名为“添加服务器”。
有效地,当您添加新服务器时,该服务器将出现在新创建的JList上。
我该怎么做?
编辑:顺便说一句,2个参数是字符串serverName和ServerIP。
答案 0 :(得分:2)
创建一个包含两个不同值的键/值属性对象。然后,您将其作为单个对象传递给JList
。
您需要提供自定义单元格渲染器才能正确渲染值。
有关详细信息,请参阅How to use lists和Writing a Custom Cell Renderer
使用JTable
。虽然这将允许您将每个值(键/值)作为单独的列提供,但我仍然将这两个值包装在键/值对象中,因为它会使这些值的管理更简单。
如果您愿意,这将允许您单独与每一方进行互动。
有关详细信息,请参阅How to Use Tables
使用适当的布局管理器。像素完美布局(this.setLayout(null);
)是现代GUI中的一种幻觉。渲染管道有很多方面你根本无法控制,这会很快将你系统上的“漂亮”布局变成不同系统上的字母汤