简体   繁体   English

如何创建一个包含参数的JList?

[英]How to create a JList which holds Parameters?

Hi i'm trying to make a JList but it takes 2 Parameters. 嗨,我正在尝试制作一个JList但它需要2个参数。 The list is a list of servers. 该列表是服务器列表。

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);
                }
            });

        }
    }
}

The world list appears when you click the Multiplayer JButton . 单击多人游戏JButton时会出现世界列表。 Also when you click the Multiplayer JButton a JButton appears named Add Server. 此外,当您单击多人游戏JButtonJButton显示为名为Add Server。 Effectively when you add a new server that server would appear on the newly created JList. 有效地,当您添加新服务器时,该服务器将出现在新创建的JList上。

How would I do this? 我该怎么做?

edit: By the way the 2 parameters are Strings serverName, and ServerIP. 编辑:顺便说一句,2个参数是字符串serverName和ServerIP。

You Could... 你可以...

Create a key/value property object which holds the two distinct values. 创建一个包含两个不同值的键/值属性对象。 You would then pass this to the JList as a single Object. 然后,您将其作为单个Object传递给JList

You would need to supply a custom cell renderer in order to render the value appropriately. 您需要提供自定义单元格渲染器才能正确呈现值。

See How to use lists and Writing a Custom Cell Renderer for more details 有关更多详细信息,请参见如何使用列表编写自定义单元格渲染器

You Could... 你可以...

Use a JTable . 使用JTable While this would allow you to supply each value (key/value) as a separate column, I would still wrap the two in a key/value object, as it would make the management of these values simpler. 虽然这将允许您将每个值(键/​​值)作为单独的列提供,但我仍然将这两个值包装在键/值对象中,因为它会使这些值的管理更简单。

This would allow you to interact with each side individually should you wish. 如果您愿意,这将允许您单独与每一方进行交互。

See How to Use Tables for more details 有关更多详细信息,请参见如何使用表

You Should... 你应该...

Make use of appropriate layout managers. 使用适当的布局管理器。 Pixel perfect layouts ( this.setLayout(null); ) are an illusion in modern GUIs. 像素完美布局( this.setLayout(null); )是现代GUI中的错觉。 There are so many aspects of the rendering pipeline you simply don't have control over, which would quickly turn your "pretty" layout on your system into alphabet soup on different systems 渲染管道有很多方面你根本无法控制,这会很快将你系统上的“漂亮”布局变成不同系统上的字母表汤

Take a look at Laying Out Components Within a Container for more details. 有关更多详细信息,请查看在容器布置组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM