【问题标题】:Creating Custom Lines for Tic Tac Toe Board NetBeans为井字游戏 NetBeans 创建自定义行
【发布时间】:2015-11-26 01:12:33
【问题描述】:

我想开始学习 Java 并使用 NetBeans 作为我的 GUI 编辑器。我可以在调色板中看到如何添加按钮、标签、文本框等,但是如何添加可以使用 NetBeans 自定义宽度和长度的线条?我知道我可以通过查看 Java 代码示例来做到这一点,但我希望能够通过 NetBeans 插入,而不是插入代码来创建 3x3 网格。我是使用 NetBeans 的新手,我查看了谷歌,但找不到任何东西。在此先感谢您的帮助。

【问题讨论】:

    标签: java swing netbeans tic-tac-toe


    【解决方案1】:

    更好地使用代码,但您可以使用 NetBeans 做到这一点。创建一个 GridLayout,但为其水平和垂直间隙属性赋予非零值。那么如果使用该布局的容器有Color.black背景,并且你将不透明的组件放入网格中,就会出现黑线。

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    @SuppressWarnings("serial")
    public class TicTacToePanel extends JPanel {
        private static final int ROWS = 3;
        private static final int MY_C = 240;
        private static final Color BG = new Color(MY_C, MY_C, MY_C);
        private static final int PTS = 60;
        private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, PTS);
        public static final Color X_COLOR = Color.BLUE;
        public static final Color O_COLOR = Color.RED;
        private JLabel[][] labels = new JLabel[ROWS][ROWS];
        private boolean xTurn = true;
    
        public TicTacToePanel() {
            setLayout(new GridLayout(ROWS, ROWS, 2, 2));
            setBackground(Color.black);
    
            MyMouse myMouse = new MyMouse();
            for (int row = 0; row < labels.length; row++) {
                for (int col = 0; col < labels[row].length; col++) {
                    JLabel label = new JLabel("     ", SwingConstants.CENTER);
                    label.setOpaque(true);
                    label.setBackground(BG);
                    label.setFont(FONT);
                    add(label);
                    label.addMouseListener(myMouse);
                }
            }
        }
    
        private class MyMouse extends MouseAdapter {
            @Override // override mousePressed not mouseClicked
            public void mousePressed(MouseEvent e) {
                JLabel label = (JLabel) e.getSource();
                String text = label.getText().trim();
                if (!text.isEmpty()) {
                    return;
                }
                if (xTurn) {
                    label.setForeground(X_COLOR);
                    label.setText("X");
                } else {
                    label.setForeground(O_COLOR);
                    label.setText("O");
                }
    
                // information to help check for win
                int chosenX = -1;
                int chosenY = -1;
                for (int x = 0; x < labels.length; x++) {
                    for (int y = 0; y < labels[x].length; y++) {
                        if (labels[x][y] == label) {
                            chosenX = x;
                            chosenY = y;
                        }
                    }
                }
                // TODO: check for win here
                xTurn = !xTurn;
            }
        }
    
        private static void createAndShowGui() {
            TicTacToePanel mainPanel = new TicTacToePanel();
    
            JFrame frame = new JFrame("Tic Tac Toe");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(mainPanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGui();
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2015-01-08
      相关资源
      最近更新 更多