Java:使用GridLayout输入矩阵 [英] Java: input a matrix using GridLayout

查看:137
本文介绍了Java:使用GridLayout输入矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个可以使用GridLayout输入任意大小的矩阵的函数,但是因为我找不到一个合适的方法来提取JTextField值来填充'mat'var,所以我卡住了(参见下面的FIXME)。

I am trying to write a function that can input a matrix of any size using a GridLayout, but I'm stuck since I can't find an appropriate way of extracting the JTextField values to fill the 'mat' var (see FIXME below).

    /**
     * @mat: matrix declared in main (e.g: mat[][] = new int[3][3];)
     * @rows: number of matrix rows (e.g: int rows = 3;)
     * @columns: number of matrix columns (e.g: int columns = 3;)
     */
    public static int[][] inputMatrix(int[][] mat, int rows, int columns)
    {
        JPanel panel = new JPanel();     
        panel.setLayout(new GridLayout(rows,columns));

        for (int a=0; a<(rows*columns); a++)
        {
            panel.add(new JTextField(a));
        }

        if (JOptionPane.showConfirmDialog(null, panel, "Enter the matrix", JOptionPane.OK_CANCEL_OPTION)
                                        == JOptionPane.OK_OPTION)
        {
            for(int a=0; a<(rows*columns); a++){
                for(int b=0; b<rows; b++){
                    for(int c=0; c<columns; c++){
                        /* FIXME: find how to extract JTextField values. */
                        mat[b][c] = JTextField.a.getText();
                    }
                }
            }
        }

        return mat;
    }

预先感谢您的帮助!

推荐答案


  • 使用 JTable 而不是一堆 JTextField GridLayout

    • use JTable instead of bunch of JTextField layed by GridLayout
      • add there putClientProperty and to add identifier Row a Column from GridLayout

      JTextField 放入 HashMap

      我更喜欢 putClientProperty (你可以多重播放数字或额外的信息..,单独的 putClientProperty isn'以某种方式减少了)

      I would be preferring putClientProperty (you can to multiplay number or additional infos.., number of separate putClientProperty isn't somehow reduced)

      取决于(不清楚)设计,你可以将 ActionListener 添加到 JTextField (accelera tor是 ENTER键)或 DocumentListener

      depends of (not clear) desing, you can to add ActionListener to JTextField (accelerator is ENTER key) or DocumentListener

      虚拟示例, JButton ActionListener 的代码示例, putClientProperty 可从所有方法访问或监听器添加到 JTextField

      virtual example, code example for JButton and ActionListener, putClientProperty is accesible from all methods or Listeners added to JTextField

      buttons[i][j].putClientProperty("column", i);
      buttons[i][j].putClientProperty("row", j);
      buttons[i][j].addActionListener(new MyActionListener());
      

      从ActionListener获取(例如)

      and get from ActionListener (for example)

      public class MyActionListener implements ActionListener {
      
          @Override
          public void actionPerformed(ActionEvent e) {
              JButton btn = (JButton) e.getSource();
              System.out.println("clicked column " + btn.getClientProperty("column")
                      + ", row " + btn.getClientProperty("row"));
      }
      

      这篇关于Java:使用GridLayout输入矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆