【问题标题】:Numbers around JPanel; JPanel border?JPanel 周围的数字; JPanel边框?
【发布时间】:2016-04-18 13:02:08
【问题描述】:

我正在创建一个迷你游戏,但我被卡住了。这个网格应该是 nxn,但在这种情况下它是 6x6,直到我弄清楚为止。

无论如何,我想创建一个透明边框

这将在每个单元格上方添加居中的数字(在左侧和上方,但是,稍后我必须添加右侧和下方)。什么是这样做的好方法?我四处搜索,因为我知道棋盘有这种“边界”,我实际上搜索了它们,但没有运气。

这是一个代码 sn-p,单元格包含在一个简单的 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);
    }
}

【问题讨论】:

  • 您可以将一些JLabels 添加到网格内的相应位置。
  • 在源代码中缺少CellPanels 的类。如果你可以添加这个类,那么这个程序就可以被 SO 用户测试了。
  • @pzaenger 好的。我去看看,谢谢。
  • @Intai'sei 你能用主要方法添加部分吗?我认为 pzaenger 是对的,简单地添加一些标签可能是完成任务的最简单方法。
  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。 3) 另见Making a robust, resizable Swing Chess GUI

标签: java swing jpanel border


【解决方案1】:

也许尝试在您的 gridbaglayout 中添加额外的列和行。然后要么在你的 for() 构造中将它们控制为特殊情况,要么从 1 开始索引,然后返回并在你的 for() 构造之外处理它们。

创建另一个继承的 JPanel,您可以将其用作 CellLabels,尤其是当它们看起来都一样时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2018-02-22
    • 1970-01-01
    相关资源
    最近更新 更多