编程语言
首页 > 编程语言> > Java Swing布局问题和代码优化

Java Swing布局问题和代码优化

作者:互联网

为了训练Java,我编写了自己的程序.
我喜欢玩CSGO,我们永远无法决定要播放哪个地图.
所以我想写一个程序,你可以选择你想要的地图,然后它随机地从选择中给出每个地图一次.

还有一点,但它的代码已经工作.

我已经使用IntelliJ(我的IDE)的Swing Form构建器尝试了一次,
但由于我不太明白它在那里做了什么,我想手工重做.所以我重新编写了程序,但我无法让它看起来正确.

我第一次使用IntelliJ Swing Builder:
Try1

没有GUIBuilder的帮助,它现在看起来像这样:
Try2
所以我用谷歌搜索了几个小时来获得正确的布局.目前我正在使用GridBagLayout(似乎最符合我的需求).但我无法做到对.它应该看起来像第一张照片.

问题1:

我对GridBag做错了什么,因为有很多未使用的空间和东西

问题2:我知道代码改进有很多,你有什么建议?
我仍然有问题知道什么时候私人和访问方法&东西,
所以有任何重要的错误吗?

这是我的代码:

它的3个类:

第一
第一
主类(仅用于启动)

import javax.swing.*;

public class App {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new MainFrame("Map Chooser v0.1");
                frame.setSize(500, 250);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

** The Swing MainFrame:**

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class MainFrame extends JFrame {

    ImageIcon dust2;

    public MainFrame(String title) {
        super(title);

        GridLayout gridMain = new GridLayout(10, 10, 5, 5);
        GridLayout gridCB = new GridLayout(3, 5, 5, 5);
        GridLayout gridButton = new GridLayout(1, 1, 10, 10);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(gridMain);
        mainPanel.setBackground(Color.DARK_GRAY);

        JPanel checkboxPanel = new JPanel();
        checkboxPanel.setLayout(gridCB);
        checkboxPanel.setBackground(Color.DARK_GRAY);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(gridButton);
        buttonPanel.setBackground(Color.DARK_GRAY);

        JPanel labelPanel = new JPanel();
        labelPanel.setBackground(Color.DARK_GRAY);

        mainPanel.add(checkboxPanel);
        mainPanel.add(buttonPanel);
        mainPanel.add(labelPanel);

        MapRound map = new MapRound();
        //GridBagConstraints gbc = new GridBagConstraints();
        GridBagConstraints gbc2 = new GridBagConstraints();
        GridBagConstraints gbc3 = new GridBagConstraints();
        gbc2.weighty = 0.5;
        gbc2.weightx = 0.5;

        gbc3.weightx = 1;
        gbc3.weighty = 1;
        gbc3.fill = GridBagConstraints.ABOVE_BASELINE;

        //Checkboxes
        JCheckBox dust2CB = new JCheckBox("Dust II");
        dust2CB.setBackground(Color.DARK_GRAY);
        dust2CB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 0;
        gbc2.gridy = 0;
        checkboxPanel.add(dust2CB, gbc2);
        dust2CB.setSelected(false);

        JCheckBox trainCB = new JCheckBox("Train");
        trainCB.setBackground(Color.DARK_GRAY);
        trainCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 0;
        gbc2.gridy = 1;
        checkboxPanel.add(trainCB, gbc2);
        trainCB.setSelected(false);

        JCheckBox mirageCB = new JCheckBox("Mirage");
        mirageCB.setBackground(Color.DARK_GRAY);
        mirageCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 0;
        gbc2.gridy = 2;
        checkboxPanel.add(mirageCB, gbc2);
        mirageCB.setSelected(false);

        JCheckBox infernoCB = new JCheckBox("Inferno");
        infernoCB.setBackground(Color.DARK_GRAY);
        infernoCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 1;
        gbc2.gridy = 0;
        checkboxPanel.add(infernoCB, gbc2);
        infernoCB.setSelected(false);

        JCheckBox cobblestoneCB = new JCheckBox("Cobblestone");
        cobblestoneCB.setBackground(Color.DARK_GRAY);
        cobblestoneCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 1;
        gbc2.gridy = 1;
        checkboxPanel.add(cobblestoneCB, gbc2);
        cobblestoneCB.setSelected(false);

        JCheckBox overpassCB = new JCheckBox("Overpass");
        overpassCB.setBackground(Color.DARK_GRAY);
        overpassCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 1;
        gbc2.gridy = 2;
        checkboxPanel.add(overpassCB, gbc2);
        overpassCB.setSelected(false);

        JCheckBox cacheCB = new JCheckBox("Cache");
        cacheCB.setBackground(Color.DARK_GRAY);
        cacheCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 2;
        gbc2.gridy = 0;
        checkboxPanel.add(cacheCB, gbc2);
        cacheCB.setSelected(false);

        JCheckBox aztecCB = new JCheckBox("Aztec");
        aztecCB.setBackground(Color.DARK_GRAY);
        aztecCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 2;
        gbc2.gridy = 1;
        checkboxPanel.add(aztecCB, gbc2);
        aztecCB.setSelected(false);

        JCheckBox dustCB = new JCheckBox("Dust");
        dustCB.setBackground(Color.DARK_GRAY);
        dustCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 2;
        gbc2.gridy = 2;
        checkboxPanel.add(dustCB, gbc2);
        dustCB.setSelected(false);

        JCheckBox vertigoCB = new JCheckBox("Vertigo");
        vertigoCB.setBackground(Color.DARK_GRAY);
        vertigoCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 3;
        gbc2.gridy = 0;
        checkboxPanel.add(vertigoCB, gbc2);
        vertigoCB.setSelected(false);

        JCheckBox nukeCB = new JCheckBox("Nuke");
        nukeCB.setBackground(Color.DARK_GRAY);
        nukeCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 3;
        gbc2.gridy = 1;
        checkboxPanel.add(nukeCB, gbc2);
        nukeCB.setSelected(false);

        JCheckBox officeCB = new JCheckBox("Office");
        officeCB.setBackground(Color.DARK_GRAY);
        officeCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 3;
        gbc2.gridy = 2;
        checkboxPanel.add(officeCB, gbc2);
        officeCB.setSelected(false);

        JCheckBox italyCB = new JCheckBox("Italy");
        italyCB.setBackground(Color.DARK_GRAY);
        italyCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 9;
        gbc2.gridy = 0;
        checkboxPanel.add(italyCB, gbc2);
        italyCB.setSelected(false);

        JCheckBox assaultCB = new JCheckBox("Assault");
        assaultCB.setBackground(Color.DARK_GRAY);
        assaultCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 9;
        gbc2.gridy = 1;
        checkboxPanel.add(assaultCB, gbc2);
        assaultCB.setSelected(false);

        JCheckBox militiaCB = new JCheckBox("Militia");
        militiaCB.setBackground(Color.DARK_GRAY);
        militiaCB.setForeground(Color.LIGHT_GRAY);
        gbc2.gridx = 9;
        gbc2.gridy = 2;
        checkboxPanel.add(militiaCB, gbc2);
        militiaCB.setSelected(false);

        //Buttons
        JButton startButton = new JButton("Start");
        startButton.setBackground(Color.DARK_GRAY);
        startButton.setForeground(Color.LIGHT_GRAY);
        gbc3.gridx = 0;
        gbc3.gridy = 0;
        gbc3.insets = new Insets(10, 10, 10, 10);
        startButton.setPreferredSize(new Dimension(400, 400));
        buttonPanel.add(startButton, gbc3);

        JButton newmapButton = new JButton("New Map");
        newmapButton.setBackground(Color.DARK_GRAY);
        newmapButton.setForeground(Color.LIGHT_GRAY);
        gbc3.gridx = 0;
        gbc3.gridy = 1;
        gbc3.weightx = 1.0;
        gbc3.weighty = 1.0;
        gbc3.insets = new Insets(0, 0, 10, 10);
        buttonPanel.add(newmapButton, gbc3);
        newmapButton.setEnabled(false);

        JButton resetButton = new JButton("Reset");
        resetButton.setBackground(Color.DARK_GRAY);
        resetButton.setForeground(Color.LIGHT_GRAY);
        gbc3.gridx = 0;
        gbc3.gridy = 3;
        gbc3.insets = new Insets(0, 0, 10, 10);
        buttonPanel.add(resetButton, gbc3);
        resetButton.setEnabled(false);

        //Labels
        JLabel result = new JLabel("Press START to begin");
        gbc3.gridx = 0;
        gbc3.gridy = 4;
        gbc3.insets = new Insets(0, 0, 10, 10);
        labelPanel.add(result, gbc3);

        //Verhalten
        dust2CB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {

                if (dust2CB.isSelected()) {
                    map.mList.add("DustII");
                } else {
                    map.removeMap("DustII");
                }
            }
        });

        trainCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (trainCB.isSelected()) {
                    map.mList.add("Train");
                } else {
                    map.removeMap("Train");
                }
            }
        });

        mirageCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (mirageCB.isSelected()) {
                    map.mList.add("Mirage");
                } else {
                    map.removeMap("Mirage");
                }
            }
        });

        infernoCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (infernoCB.isSelected()) {
                    map.mList.add("Inferno");
                } else {
                    map.removeMap("Inferno");
                }
            }
        });

        cobblestoneCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (cobblestoneCB.isSelected()) {
                    map.mList.add("Cobblestone");
                } else {
                    map.removeMap("Cobblestone");
                }
            }
        });

        overpassCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (overpassCB.isSelected()) {
                    map.mList.add("Overpass");
                } else {
                    map.removeMap("Overpass");
                }
            }
        });

        cacheCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (cacheCB.isSelected()) {
                    map.mList.add("Cache");
                } else {
                    map.removeMap("Cache");
                }
            }
        });

        aztecCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (aztecCB.isSelected()) {
                    map.mList.add("Aztec");
                } else {
                    map.removeMap("Aztec");
                }
            }
        });

        dustCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (dustCB.isSelected()) {
                    map.mList.add("Dust");
                } else {
                    map.removeMap("Dust");
                }
            }
        });

        vertigoCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (vertigoCB.isSelected()) {
                    map.mList.add("Vertigo");
                } else {
                    map.removeMap("Vertigo");
                }
            }
        });

        nukeCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (nukeCB.isSelected()) {
                    map.mList.add("Nuke");
                } else {
                    map.removeMap("Nuke");
                }
            }
        });

        officeCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (officeCB.isSelected()) {
                    map.mList.add("Office");
                } else {
                    map.removeMap("Office");
                }
            }
        });

        italyCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (italyCB.isSelected()) {
                    map.mList.add("Italy");
                } else {
                    map.removeMap("Italy");
                }
            }
        });

        assaultCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (assaultCB.isSelected()) {
                    map.mList.add("Assault");
                } else {
                    map.removeMap("Assault");
                }
            }
        });

        militiaCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (militiaCB.isSelected()) {
                    map.mList.add("Militia");
                } else {
                    map.removeMap("Militia");
                }
            }
        });

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e2) {

                if (map.mList.size() == 0) {
                    JOptionPane.showMessageDialog
                            (null, "Please add one or more    maps to the selection pool!");
                } else {

                    map.scambleMap();
                    startButton.setEnabled(false);
                    newmapButton.setEnabled(true);
                    resetButton.setEnabled(true);

                    dust2CB.setEnabled(false);
                    trainCB.setEnabled(false);
                    mirageCB.setEnabled(false);
                    infernoCB.setEnabled(false);
                    cobblestoneCB.setEnabled(false);
                    overpassCB.setEnabled(false);
                    cacheCB.setEnabled(false);
                    aztecCB.setEnabled(false);
                    dustCB.setEnabled(false);
                    vertigoCB.setEnabled(false);
                    nukeCB.setEnabled(false);
                    officeCB.setEnabled(false);
                    italyCB.setEnabled(false);
                    assaultCB.setEnabled(false);
                    militiaCB.setEnabled(false);
                }
            }
        });

        newmapButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e2) {
                boolean preventloop = true;

                try {
                    while (preventloop) {
                        String xyz = map.nextMap();
                        result.setText("<html><font size=10><font color=green>
                                "+ xyz + </font></font></html>");
                        preventloop = false;
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null,
                            "You played all maps!\nThe game has been reset");
                    map.resetStatus();
                    result.setText("Press START to begin");
                    newmapButton.setEnabled(false);
                    startButton.setEnabled(true);
                    dust2CB.setEnabled(true);
                    trainCB.setEnabled(true);
                    mirageCB.setEnabled(true);
                    infernoCB.setEnabled(true);
                    cobblestoneCB.setEnabled(true);
                    overpassCB.setEnabled(true);
                    cacheCB.setEnabled(true);
                    aztecCB.setEnabled(true);
                    dustCB.setEnabled(true);
                    vertigoCB.setEnabled(true);
                    nukeCB.setEnabled(true);
                    officeCB.setEnabled(true);
                    italyCB.setEnabled(true);
                    assaultCB.setEnabled(true);
                    militiaCB.setEnabled(true);
                }
            }
        });

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                map.resetStatus();
                result.setText("Press START to begin");
                newmapButton.setEnabled(false);
                startButton.setEnabled(true);

                dust2CB.setEnabled(true);
                trainCB.setEnabled(true);
                mirageCB.setEnabled(true);
                infernoCB.setEnabled(true);
                cobblestoneCB.setEnabled(true);
                overpassCB.setEnabled(true);
                cacheCB.setEnabled(true);
                aztecCB.setEnabled(true);
                dustCB.setEnabled(true);
                vertigoCB.setEnabled(true);
                nukeCB.setEnabled(true);
                officeCB.setEnabled(true);
                italyCB.setEnabled(true);
                assaultCB.setEnabled(true);
                militiaCB.setEnabled(true);
            }
        });

        this.add(mainPanel);
        this.setVisible(true);
    }
}

最后一个:
(包含Arraylist的Map Object的蓝图

import java.util.ArrayList;
import java.util.Collections;

公共类MapRound {
    ArrayList mList = new ArrayList<>();
    int counter = 0;

//Constructor
public MapRound() {
}

public String nextMap() {
    String result = mList.get(counter);
    counter = counter + 1;
    return result;
}

public void resetStatus() {
    counter = 0;
    Collections.shuffle(mList);
}

public String removeMap(String index) {
    mList.remove(index);
    return index;
}

public void scambleMap() {
    Collections.shuffle(mList);
}

}

如果您有任何想法或建议,我会很高兴听到他们!

解决方法:

查看代码,我看到正在使用GridLayout布局管理器(而不是GridBagLayout).虽然名称非常相似,但它们的行为却截然不同.这也意味着在向其中一个面板添加组件时传递的GridBagConstraints对象将无法正确理解. (如果布局管理器在传递了无法处理的约束对象时会给出错误消息,那就太好了.)

有关这两个(和其他)布局管理器的更多信息,请参见这个很好的Visual Guide to Layout Managers.

我认为BoxLayout布局管理器和一些面板周围的空边框对你的程序也很有用.您可以使用标签在GUI的顶部和底部显示图像.我认为可以删除所有与GridBagConstraints相关的代码.截图示例:

Screenshot with new MainFrame class

您的MainFrame类可以像这样修改:

public class MainFrame extends JFrame {

    //ImageIcon dust2;
    public MainFrame(String title) {
        super(title);

        //GridLayout gridMain = new GridLayout(5, 0, 5, 5);
        GridLayout gridCB = new GridLayout(3, 5, 5, 5);
        //GridLayout gridButton = new GridLayout(4, 1, 10, 10);

        JPanel mainPanel = new JPanel();
        BoxLayout mainLayout = new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS);
        mainPanel.setLayout(mainLayout);
        mainPanel.setBackground(Color.DARK_GRAY);

        JPanel checkboxPanel = new JPanel();
        checkboxPanel.setLayout(gridCB);
        checkboxPanel.setBackground(Color.DARK_GRAY);

        JPanel buttonPanel = new JPanel();
        BoxLayout buttonLayout = new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS);
        buttonPanel.setLayout(buttonLayout);
        buttonPanel.setBorder(new EmptyBorder(6, 6, 6, 6));
        buttonPanel.setBackground(Color.DARK_GRAY);

        JPanel labelPanel = new JPanel();
        labelPanel.setBackground(Color.DARK_GRAY);

        String directory = "directory/to/images/";

        JPanel topImagePanel = new JPanel();
        topImagePanel.setBackground(Color.DARK_GRAY);
        topImagePanel.setBorder(new EmptyBorder(6, 6, 6, 6));
        String topImagePath = directory + "map selector - top.png";
        topImagePanel.add(new JLabel(new ImageIcon(topImagePath)));

        JPanel bottomImagePanel = new JPanel();
        bottomImagePanel.setBackground(Color.DARK_GRAY);
        bottomImagePanel.setBorder(new EmptyBorder(6, 6, 6, 6));
        String bottomImagePath = directory + "map selector - bottom.png";
        bottomImagePanel.add(new JLabel(new ImageIcon(bottomImagePath)));

        mainPanel.add(topImagePanel);
        mainPanel.add(checkboxPanel);
        mainPanel.add(buttonPanel);
        mainPanel.add(labelPanel);
        mainPanel.add(bottomImagePanel);

        // [...]

        //Buttons
        JButton startButton = new JButton("Start");
        startButton.setBackground(Color.DARK_GRAY);
        startButton.setForeground(Color.LIGHT_GRAY);
        gbc3.gridx = 0;
        gbc3.gridy = 0;
        gbc3.insets = new Insets(10, 10, 10, 10);
        startButton.setPreferredSize(new Dimension(400, 400));
        buttonPanel.add(startButton, gbc3);
        startButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        buttonPanel.add(Box.createRigidArea(new Dimension(0, 6)));

        JButton newmapButton = new JButton("New Map");
        newmapButton.setBackground(Color.DARK_GRAY);
        newmapButton.setForeground(Color.LIGHT_GRAY);
        gbc3.gridx = 0;
        gbc3.gridy = 1;
        gbc3.weightx = 1.0;
        gbc3.weighty = 1.0;
        gbc3.insets = new Insets(0,0,10,10);
        buttonPanel.add(newmapButton, gbc3);
        newmapButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        newmapButton.setEnabled(false);

        buttonPanel.add(Box.createRigidArea(new Dimension(0, 6)));

        JButton resetButton = new JButton("Reset");
        resetButton.setBackground(Color.DARK_GRAY);
        resetButton.setForeground(Color.LIGHT_GRAY);
        gbc3.gridx = 0;
        gbc3.gridy = 3;
        gbc3.insets = new Insets(0,0,10,10);
        buttonPanel.add(resetButton, gbc3);
        resetButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        resetButton.setEnabled(false);

        // [...]
    }
}

编辑:您的代码的一些一般建议

> MainFrame构造函数非常大.我会用较小的方法将其拆分,以创建不同的面板并添加监听器.
>搜索类似的代码片段,看看是否可以制作可重用的方法.例如:有三个代码块将所有复选框设置为禁用或启用.您可以使用布尔参数创建一个方法来为所有复选框调用setEnabled.
>创建复选框目前需要大量代码.如果您有游戏地图列表,则可以在循环中创建复选框. (这是在代码中创建GUI的一个很好的优点.)如果使用每个复选框的name属性,它们都可以共享相同的ItemListener.最后,如果将所有复选框存储在列表中,则可以轻松禁用或启用所有复选框.

一些代码来说明这一点:

java.util.List<String> gameMaps = Arrays.asList(
        "Dust II", "Train", "Mirage", "Inferno", "Cobblestone", "Overpass",
        "Cache", "Aztec", "Dust", "Vertigo", "Nuke", "Office", "Italy",
        "Assault", "Militia"
);

ItemListener mapCheckBoxListener = new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent itemEvent) {
        JCheckBox checkBox = (JCheckBox) itemEvent.getSource();
        if (checkBox.isSelected()) {
            map.mList.add(checkBox.getName());
        } else {
            map.removeMap(checkBox.getName());
        }
    }
};

java.util.List<JCheckBox> mapCheckBoxes = new ArrayList<>();
for (String gameMap : gameMaps) {
    JCheckBox mapCheckBox = new JCheckBox(gameMap);
    mapCheckBox.setName(gameMap);
    mapCheckBox.setBackground(Color.DARK_GRAY);
    mapCheckBox.setForeground(Color.LIGHT_GRAY);
    checkboxPanel.add(mapCheckBox);
    mapCheckBox.setSelected(false);
    mapCheckBox.addItemListener(mapCheckBoxListener);
    mapCheckBoxes.add(mapCheckBox);
}

// [....]

private void setMapsEnabled(java.util.List<JCheckBox> mapCheckBoxes, boolean enabled) {
    for (JCheckBox mapCheckBox : mapCheckBoxes) {
        mapCheckBox.setEnabled(enabled);
    }
}

标签:java,user-interface,swing,layout-manager,gridbaglayout
来源: https://codeday.me/bug/20190623/1270946.html