在循环中动态创建JTextField

问题描述 投票:2回答:2

我是Java GUI的初学者。我正在编写一个计算给定矩阵的逆的程序。要做到这一点,首先我必须扫描矩阵。我决定用这种方式扫描矩阵:

首先,我要求用户计算方阵的行数或列数。当用户输入一个数字并单击Tamam按钮时,我想创建一个小的NxN JTextField

因此用户将能够轻松地输入矩阵的每个元素。我想问的是:我无法创建NxN JTextField。我的代码:

private void jButtonRowCntMouseClicked(java.awt.event.MouseEvent evt) {                                           
        int i,j;
        if(jTextFieldRowCnt.getText() != null){
            String cnt = jTextFieldRowCnt.getText();
            Integer rowCnt = Integer.parseInt(cnt);                      
            for(i=0;i<rowCnt;i++){
                for(j=0;j<rowCnt;j++){
                      JTextField textField = new JTextField();
                      this.add(textField);
                      pack();
                }
            }
        }
    } 

但不幸的是,我做不到。我哪里错了?

我的另一个问题是,如何设置NxN JTextFields的位置?

java swing user-interface jtextfield
2个回答
1
投票
import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;

    public class Test 
    {
        // Field members
        static JPanel panel = new JPanel();
        static Integer indexer = 1;
        static List<JTextField> listOfTextFields = new ArrayList<JTextField>();

        public static void main(String[] args)
        {       
            // Construct frame
            JFrame frame = new JFrame();
            frame.setLayout(new GridBagLayout());
            frame.setPreferredSize(new Dimension(990, 990));
            frame.setTitle("My Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // Frame constraints
            GridBagConstraints frameConstraints = new GridBagConstraints();

            // Construct button
            JButton addButton = new JButton("test");
            addButton.addActionListener(new ButtonListener());

            // Add button to frame
            frameConstraints.gridx = 0;
            frameConstraints.gridy = 0;
            frame.add(addButton, frameConstraints);

            // Construct panel
            panel.setPreferredSize(new Dimension(600, 600));
            panel.setLayout(new GridBagLayout());
            panel.setBorder(LineBorder.createBlackLineBorder());

            // Add panel to frame
            frameConstraints.gridx = 0;
            frameConstraints.gridy = 1;
            frameConstraints.weighty = 1;
            frame.add(panel, frameConstraints);

            // Pack frame
            frame.pack();

            // Make frame visible
            frame.setVisible(true);
        }

        static class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {       

                panel.removeAll();
                GridBagConstraints textFieldConstraints = new GridBagConstraints();

                int rowCnt=4,i,j;

                for(i=0;i<rowCnt;i++){
                    for(j=0;j<rowCnt;j++){
                        JTextField g=new JTextField();
                        g.setText("7");
                        textFieldConstraints.gridx = i;
                        textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                        textFieldConstraints.weightx = 0.5;
                        textFieldConstraints.insets = new Insets(10, 10, 10, 10);
                        textFieldConstraints.gridy = j;
                        panel.add(g, textFieldConstraints);
                    }
                }

                panel.updateUI();
            }


        }
    }

试试这个,得到你想要的


0
投票

具体问题是什么?什么是对象的类?这似乎是容器组件布局的问题。

我建议你将矩阵文本字段添加到带有JPanel的专用GridLayout中。

例:

    // ...
    matrixPanel.setLayout(new GridLayout(rowCnt, rowCnt)); // matrixPanel is the dedicated JPanel
    for(i=0;i<rowCnt;i++){
        for(j=0;j<rowCnt;j++){
              JTextField textField = new JTextField();
              matrixPanel.add(textField); // add the fields into the panel
              //pack(); I think it wouldn't be needed
        }
    }

有关Qazxswpoi中GridPanel的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.