使用不同的变量名称复制相同的 JLabel

Replicate the same JLabel with diferent variable names

我需要使相同的 JLabel 在同一个网格布局中多次出现,但我在实现生成 label00label01 的方法时遇到了问题label02, ..., label10。这些标签必须具有相同的内容。有人可以给我一个代码示例吗?

public class Grid {
    private JFrame f;

    public Grid(String fname, int row, int column, int d) {
        f = new JFrame(fname);
        f.setLayout(new GridLayout(row,column));
        f.setSize(row*d,column*d);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addContent(row,column,d);
    }

    public void open() {
        f.setVisible(true);
    }

    private void addContent(int r, int c, int d) {
        JLabel label=new JLabel(" ");
        Border border = BorderFactory.createLineBorder(Color.black, 2);
        label.setBorder(border);
        label.setPreferredSize(new Dimension(d,d));
        f.add(label);
        JLabel label1= new JLabel("1 ");
        f.add(label1);
    }


    public static void main(String[] args) {
         Grid grid = new Grid("Test", 5, 4, 50);
         grid.open();
    }
}

我不想手动创建每个标签,而是想实现一种使用原始标签的文本自动生成 label1label2、... labeli 的方法。

您有一个二维数组网格。要用 JLabel 填充它,遍历两个维度并像这样添加您的自定义 JLabel。如果你有特定的属性,比如值或样式的计算,我建议你创建一个自定义 JLabel.

private JFrame f;
public Grid(String fname, int row, int column, int d) {
    f = new JFrame(fname);
    f.setLayout(new GridLayout(row,column));
    f.setSize(row*d,column*d);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    addContent(row,column,d);
}

public void open() {
    f.setVisible(true);
}

private void addContent(int r, int c, int d) {
    Border border = BorderFactory.createLineBorder(Color.black, 2);
    for (int i = 0 ; i < r ; i++) {
        for (int j = 0 ; j < c ; j++) {
            f.add(new JLabel(i+","+j) {{
                 setBorder(border);
                 setPreferredSize(new Dimension(d,d));
            }});
        }
    }
}
public static void main(String[] args) { 
    Grid grid = new Grid("Test", 5, 4, 50);
    grid.open();
}

结果:

编辑:

因为这是一个井字游戏,我建议你创建一个自定义的 JLabel,它可以在点击时实现 mouselistener(例如),你将能够根据事件修改颜色等。

public class TicTacToeCell extends JLabel {
    private static final long serialVersionUID = 1L;
    public TicTacToeCell(Border border, int height, int width) {
        setText("");
        setBorder(border);
        setPreferredSize(new Dimension(height, width));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (getText().isEmpty()) {
                    setText(getClickedValue());
                }
            }
        });
    }
} 

您需要知道最后一次点击是什么,所以每次点击一个单元格时将静态方法设置为 return O 或 X。

public static int clickValue = 0;
public static String getClickedValue() {
    clickValue = clickValue == 0 ? 1 : 0;
    return clickValue == 0 ? "X" : "O";
}

在构建 GUI 时将其添加到网格中

private void addContent(int r, int c, int d) {
    Border border = BorderFactory.createLineBorder(Color.black, 2);
    for (int i = 0 ; i < r ; i++) {
        for (int j = 0 ; j < c ; j++) {
            f.add(new TicTacToeCell(border, d, d));
        }
    }
}

结果(将网格更改为 3,3):

完整示例:

public class Grid {
    private JFrame f;
    public Grid(String fname, int row, int column, int d) {
        f = new JFrame(fname);
        f.setLayout(new GridLayout(row,column));
        f.setSize(row*d,column*d);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addContent(row,column,d);
    }
    public void open() {
        f.setVisible(true);
    }
    private void addContent(int r, int c, int d) {
        Border border = BorderFactory.createLineBorder(Color.black, 2);
        for (int i = 0 ; i < r ; i++) {
            for (int j = 0 ; j < c ; j++) {
                f.add(new TicTacToeCell(border, d, d));
            }
        }
    }
    public static void main(String[] args) {
         Grid grid = new Grid("Test", 3, 3, 50);
         grid.open();
    }
    public static int clickValue = 0;
    public static String getClickedValue() {
        clickValue = clickValue == 0 ? 1 : 0;
        return clickValue == 0 ? "X" : "O";
    }
    public class TicTacToeCell extends JLabel {
        private static final long serialVersionUID = 1L;
        public TicTacToeCell(Border border, int height, int width) {
            setText("");
            setBorder(border);
            setPreferredSize(new Dimension(height, width));
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (getText().isEmpty()) {
                        setText(getClickedValue());
                    }
                }
            });
        }
    }
}