有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建可单击的JButton矩阵

我想完成的是:

*包含按钮矩阵的窗口。比如说10x10

*按钮应显示“1”或“0”,并在我单击时更改

*按钮(1或0)的值应存储在String[][]矩阵中

目前,我有一个包含值的String[][]2D数组。我可以使用以下代码在带有可单击按钮的窗口中显示它:

//dim = 10
//matrix is the 10x10 String[][] matrix containing 1s or 0s

private static void convertMatrixToGUI() {
    JFrame f = new JFrame("Window containing a matrix");
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(dim, dim));

    for(int r = 0; r < dim; r++){
        for(int c = 0; c < dim; c++){
            p.add(new JButton(matrix[r][c]));
        }
    }
    f.add(p);
    f.pack();
    f.setVisible(true);
}

下一步是在单击按钮时更改矩阵中的值。如果单击0,则应将其更改为1,反之亦然。这些值必须始终存储在String[][]

如何通过单击图形矩阵中的按钮来更改字符串矩阵中的内容?如果单击位置[5][2]处的按钮,程序如何知道我要将字符串矩阵更改为位置[5][2]

向Goatcat致意


共 (5) 个答案

  1. # 1 楼答案

    我给你举一个名为JButtonChangingButton扩展的例子,它有矩阵位置和分配给它的矩阵。此外,它还创建了一个ActionListener,将在单击时更改名称

    public static class ChangingButton extends JButton {
    
        private final int[][] fModel;
        private final int fX;
        private final int fY;
    
        public ChangingButton(final int x, final int y, final int[][] model) {
            fX= x;
            fY= y;
            fModel= model;
    
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    fModel[fX][fY] = fModel[fX][fY] == 1 ? 0 : 1;
                    updateNameFromModel();
                }
            });
            updateNameFromModel();
        }
    
        private void updateNameFromModel() {
            setText(String.valueOf(fModel[fX][fY]));
        }
    
    }
    

    这是你的主要测试课程

    public static void main(String[] args) {
    
        int dim=10;
        int matrix[][] = new int[10][10];
    
        JFrame f = new JFrame("Window containing a matrix");
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(dim, dim));
    
        for(int r = 0; r < dim; r++){
            for(int c = 0; c < dim; c++){
                ChangingButton button= new ChangingButton(r, c, matrix);
                p.add(button);
            }
        }
        f.add(p);
        f.pack();
        f.setVisible(true);
    
    }
    

    希望能有帮助。如果你不明白什么,请问

  2. # 2 楼答案

    ^{}说明了基本原则。替换JToggleButton以获得二进制选择/未选择状态的效果

    image

  3. # 3 楼答案

    试试看

            int DIM = 10;
        String [][]matrix = new String[DIM][DIM];
        JButton [][]butt = new JButton[DIM][DIM];
    
        JFrame f = new JFrame("Window containing a matrix");
        JPanel p = new JPanel();
    
        for(int r=0; r<DIM; r++){
            for(int c=0; c<DIM; c++){
                butt[r][c] = new JButton(matrix[r][c]);
                p.add(butt[r][c]);
                butt[r][c].addActionListener(this); //if your class extends ActionListener
            }
        }
    
        f.add(p);
        f.pack();
        f.setVisible(true);
    

    并重写actionPerformed方法。 实现您的代码:)

    @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
  4. # 4 楼答案

     for(int r = 0; r < dim; r++) {
           for(int c = 0; c < dim; c++){
               JButton temp = new JButton(matrix[r][c])
               temp.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                    matrix[r][c] = (matrix[r][c].equals("0")) ? "1" : "0"; //or for changing text of the button this.setText("1") or "0"
                  }
               })
              p.add(temp);
           }
       }