1

私が達成したいことは次のとおりです。

*ボタンのマトリックスを含むウィンドウ。10x10としましょう。

※ボタンは「1」か「0」のどちらかで、クリックすると変化します。

String[][]*ボタンの値 (1 または 0) は、マトリックスに格納する必要があります。

現時点では、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[][]値は常にに保存する必要があります。

グラフィック マトリックスのボタンをクリックして文字列マトリックスを変更するにはどうすればよいですか? position のボタンをクリックした場合[5][2]、文字列行列を position として変更したいことをプログラムはどのように知る必要があります[5][2]か?

宜しくお願いします

4

5 に答える 5

4

GridButtonPanel基本原理を示します。JToggleButtonバイナリの選択/非選択状態の効果を得るために代用します。

画像

于 2013-06-17T08:36:35.007 に答える
2

マトリックス位置とマトリックスが割り当てられたJButton拡張機能の例を示します。ChangingButtonまた、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);

}

それが役に立てば幸い。わからないことがあれば、質問してください。

于 2013-06-17T08:49:04.247 に答える
2

それを試してみてください

        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

    }
于 2013-06-17T08:59:24.720 に答える
2

必要なのは ActionListener です。

このチュートリアルでは、その使用方法について説明します。

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

于 2013-06-17T08:39:03.263 に答える
1
 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);
       }
   }
于 2013-06-17T08:47:17.620 に答える