简体   繁体   English

创建一个可点击的JButton矩阵

[英]Creating a clickable JButton matrix

What i want to accomplish is: 我要完成的是:

*A window containing a matrix of buttons. *包含按钮矩阵的窗口。 Let's say 10x10. 假设是10x10。

*The buttons should say either "1" or "0", and change when I click them. *按钮应显示“ 1”或“ 0”,并在单击时更改。

*The values of the buttons (1 or 0) should be stored in a String[][] matrix. *按钮的值(1或0)应存储在String[][]矩阵中。

At the moment I have a String[][] 2D-array containing values. 目前,我有一个包含值的String[][] 2D数组。 I can show it in a window with clickable buttons using the following code: 我可以使用以下代码在带有可单击按钮的窗口中显示它:

//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);
}

The next step is to change the values in the matrix when clicking the buttons. 下一步是在单击按钮时更改矩阵中的值。 If i click a 0 it should change it to a 1 and vice versa. 如果我单击0,则应将其更改为1,反之亦然。 The values have to be stored in the String[][] at all times. 这些值必须始终存储在String[][]中。

How do i change things in the string matrix by clicking a button in the graphical matrix? 如何通过单击图形矩阵中的按钮来更改字符串矩阵中的内容? If i click the button at position [5][2] , how should the program know that I want to change the string matrix as position [5][2] ? 如果单击位置[5][2]处的按钮,程序应如何知道我要将字符串矩阵更改为位置[5][2]

Best regards Goatcat 最好的问候山羊猫

GridButtonPanel illustrates the basic principle. GridButtonPanel说明了基本原理。 Substitute JToggleButton to get the effect of a binary selected/not-selected state. 替代JToggleButton以获得二进制选择/未选择状态的效果。

图片

what you need is an ActionListener. 您需要的是一个ActionListener。

this Tutorial give you an idea how to use it. 本教程为您提供了一个使用方法的想法。

http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

I give you an example of a JButton extension called ChangingButton that has matrix position and matrix assigned to it. 我给你的例子JButton扩展名为ChangingButton具有分配给它的矩阵位置和矩阵。 Also, it creates an ActionListener that will change the name on the click. 此外,它还创建一个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]));
    }

}

This is your main test class. 这是您的主要测试课程。

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);

}

Hope it helps. 希望能帮助到你。 If you don't understand something, please ask. 如果您听不懂,请询问。

Try it 试试吧

        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);

and override the actionPerformed method. 并覆盖actionPerformed方法。 Implementing your code :) 实现您的代码:)

@Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
 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);
       }
   }

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

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