Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:使用GridLayout输入矩阵_Java_Swing_Input_Matrix_Grid Layout - Fatal编程技术网

Java:使用GridLayout输入矩阵

Java:使用GridLayout输入矩阵,java,swing,input,matrix,grid-layout,Java,Swing,Input,Matrix,Grid Layout,我正在尝试编写一个函数,它可以使用GridLayout输入任意大小的矩阵,但由于找不到合适的方法来提取JTextField值以填充“mat”变量,因此我陷入了困境,请参见下面的FIXME /** * @mat: matrix declared in main (e.g: mat[][] = new int[3][3];) * @rows: number of matrix rows (e.g: int rows = 3;) * @columns: numbe

我正在尝试编写一个函数,它可以使用GridLayout输入任意大小的矩阵,但由于找不到合适的方法来提取JTextField值以填充“mat”变量,因此我陷入了困境,请参见下面的FIXME

    /**
     * @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;
    }
提前感谢您的帮助

使用而不是GridLayout放置的一堆JTextField 或

添加putClientProperty和要添加的

将JTextField放入HashMap

我更愿意使用putClientProperty,而不是多个数字或附加信息。单独的putClientProperty的数量并没有以某种方式减少

根据不明确的设计,您可以将ActionListener添加到JTextField加速器中,即ENTER键或DocumentListener

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

循环中

buttons[i][j].putClientProperty("column", i);
buttons[i][j].putClientProperty("row", j);
buttons[i][j].addActionListener(new MyActionListener());
例如,从ActionListener获取

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"));
}
使用而不是GridLayout放置的一堆JTextField 或

添加putClientProperty和要添加的

将JTextField放入HashMap

我更愿意使用putClientProperty,而不是多个数字或附加信息。单独的putClientProperty的数量并没有以某种方式减少

根据不明确的设计,您可以将ActionListener添加到JTextField加速器中,即ENTER键或DocumentListener

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

循环中

buttons[i][j].putClientProperty("column", i);
buttons[i][j].putClientProperty("row", j);
buttons[i][j].addActionListener(new MyActionListener());
例如,从ActionListener获取

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

假设您有一个3行x 3列的网格。由于GridLayout是按行添加的,因此第二行的第一项将是添加到网格中的第四项。您可以通过调用panel.getComponent3零索引来检索此项,因此第4项位于索引3处


因此,您可以使用getComponent,根据矩阵中的列数和i,j坐标计算出正确的索引。

假设您有一个3行x 3列的网格。由于GridLayout是按行添加的,因此第二行的第一项将是添加到网格中的第四项。您可以通过调用panel.getComponent3零索引来检索此项,因此第4项位于索引3处


因此,您可以使用getComponent,根据列数和矩阵中的i,j坐标计算出正确的索引。

Hi,我尝试了mat[b][c]=Integer.parseIntpanel.getComponenta;但它说不能从组件转换为int。我是不是遗漏了什么?ty@user1847810组件本身不是int,但可能是JTextField。您可以尝试这样做:mat[b][c]=Integer.parseIntJTextFieldpanel.getComponenta.getText;。但我的建议是遵循mKorbel解决方案,使用一个实际上是用于您的目的的JTable。@GuillaumePolet对我来说很有用,但看起来很脏。嗨,我试过mat[b][c]=Integer.parseIntpanel.getComponenta;但它说不能从组件转换为int。我是不是遗漏了什么?ty@user1847810组件本身不是int,但可能是JTextField。您可以尝试这样做:mat[b][c]=Integer.parseIntJTextFieldpanel.getComponenta.getText;。但我的建议是遵循mKorbel解决方案,使用一个JTable,它实际上是为您的目的而设计的。@GuillaumePolet对我来说很有用,但看起来很脏。嗨,JTable的方式看起来不错,您能再详细一点吗?我也会调查另一个。ty@user1847810从…开始。所有Swing组件在Oracle/Sun上都有相应的教程,这几乎都是一个很好的起点。您好,JTable方式看起来不错,您能详细介绍一下吗?我也会调查另一个。ty@user1847810从…开始。所有Swing组件在Oracle/Sun上都有相应的教程,这几乎都是一个很好的起点。