简体   繁体   English

JPanel周围的数字; JPanel边界?

[英]Numbers around JPanel; JPanel border?

I am creating a mini game and I'm stuck. 我正在创建一个迷你游戏而且我被卡住了。 This grid is supposed to be nxn but in this case it's 6x6 until I figure things out. 这个网格应该是nxn,但在这种情况下它是6x6,直到我解决了问题。

Anyhow, I'd like to create a transparent border 无论如何,我想创建一个透明的边框

在此输入图像描述

which will add centered numbers above each cell (on the left side and up, however, later on I must add the rights side and down). 这将在每个单元格上方添加居中数字(在左侧和上方,但是,稍后我必须添加权限侧和向下)。 What would be a good way to do this? 这样做有什么好办法? I searched around because I know that chess boards have this kind of "border" and I actually searched them up, but with no luck. 我四处搜寻,因为我知道国际象棋棋盘有这种“边界”,我实际上搜查了它们,但没有运气。

This is a code snippet, the cells are contained in a simple JPanel while everything else is in the JFrame. 这是一个代码片段,单元格包含在一个简单的JPanel中,而其他所有内容都在JFrame中。

public GameFrame() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                    | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new GamePanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            JMenuBar menubar = new JMenuBar();
            JMenu menu1 = new JMenu ("New");
            menubar.add(menu1);
            JMenu menu2 = new JMenu ("Load");
            menubar.add(menu2);
            JMenu menu3 = new JMenu ("Save");
            menubar.add(menu3);
            JMenu menu4 = new JMenu ("Size");
            menubar.add(menu4);
            JMenu menu5 = new JMenu ("Check");
            menubar.add(menu5);
            JMenu menu6 = new JMenu ("Solve");
            menubar.add(menu6);
            frame.setJMenuBar(menubar);
        }
    });

}

public class GamePanel extends JPanel {
    public GamePanel() {
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < 6; row++) {
            for (int col = 0; col < 6; col++) {
                gbc.gridx = col;
                gbc.gridy = row;

                CellPanel cellPanel = new CellPanel();
                Border border = null;
                if (row < 5) {
                    if (col < 5) {
                        border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                    } else {
                        border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                    }
                } else {
                    if (col < 5) {
                        border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                    } else {
                        border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                    }
                }
                cellPanel.setBorder(border);
                add(cellPanel, gbc);
            }
        }
    }
}
public class CellPanel extends JPanel {
    Color defaultBackground;

    public CellPanel() {
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                defaultBackground = getBackground();

                if (getBackground().equals(Color.BLUE)) { 
                    setBackground(null);
                } else {
                    setBackground(Color.BLUE);
                }
            }

        });
    }

    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }
}

图片

Maybe try to add an extra column and row to your gridbaglayout. 也许尝试在gridbaglayout中添加一个额外的列和行。 Then either control them as special cases in your for() construct or start your indexes at 1 and then go back and address them outside your for() construct. 然后在for()构造中将它们作为特殊情况控制或在1处启动索引,然后返回并在for()构造之外解决它们。

Create another inherited JPanel that you can use as CellLabels, especially if they are all going to look alike. 创建另一个可以用作CellLabel的继承JPanel,特别是如果它们看起来都相似的话。

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

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