簡體   English   中英

循環動態創建JTextField

[英]Create JTextField dynamically in a loop

我是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 JTextField的位置?

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


        }
    }

試試這個,得到你想要的

具體問題是什么? 什么是物體的類別? 容器組件的布局似乎有問題。

我建議您將矩陣文本字段添加到帶有GridLayout的專用JPanel

例:

    // ...
    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
        }
    }

有關GridPanel的詳細信息,請參見http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM