Java - 创建常用 JPanel 时出现问题

Java - Problem during the creation of a stock JPanel

我正在尝试创建一个 JPanel,它在 GridLayout 左侧 JTextField 右侧的多个 JLabel 中放置。遗憾的是,即使报告 Component 已正确添加,也不会显示任何内容。问题出在哪里?

public class AlternateGL_JPanel extends JPanel {
    protected JPanel layoutPanel;
    protected int rows, columns;
    
    public AlternateGL_JPanel(int rows, int columns) { 
        this.rows       = rows;
        this.columns    = columns;
        
        // ===== MAIN FRAME DEFINITION =====
        setBorder(new EmptyBorder(0, 0, 0, 0));
        setLayout(new GridLayout(this.rows, this.columns));
    //  setBounds(10, 10, 500, 500);
        
        // ===== INNER PANEL =====
        this.layoutPanel = new JPanel();    //This is the nested panel
        layoutPanel.setLayout(new GridLayout(this.rows, this.columns));
        super.add(layoutPanel, BorderLayout.PAGE_START);
    }
    // =========================================================            
        // TODO | Superclass: JPanel
    
    public void add(JComponent component) {
        layoutPanel.add(component);
    }
    
}


/**  A <b>ManyTextAndInsertText</b> is a {@link JPanel} that houses a number of {@link JLabel}s to the left 
 * and {@link JTextField}s on the right.
 * 
 */
public class ManyTextAndInsertText extends AlternateGL_JPanel {
    private JLabel[] texts;
    private JTextField[] insertTexts;
    
    public ManyTextAndInsertText(String[] descriptions) {
        super(descriptions.length, 2);
        
        this.texts          = new JLabel[descriptions.length];
        this.insertTexts    = new JTextField[descriptions.length];
        
        for(int i=0 ; i<descriptions.length ; i++)
            {
            texts[i]        = new JLabel(descriptions[i]);
            insertTexts[i]  = new JTextField();

            for(int j=0 ; j<this.getComponentCount() ; j++)
                System.out.println("\t" + this.getComponent(j).toString());
            this.add(texts[i]);

            for(int j=0 ; j<this.getComponentCount() ; j++)
                System.out.println("\t" + this.getComponent(j).toString());
            this.add(insertTexts[i]);
            }
    }


public class TestManyTextes extends JFrame {
    

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestManyTextes frame = new TestManyTextes();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestManyTextes() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new ManyTextAndInsertText(new String[] { "First text: " , "Second text: "});
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }



我大大简化了您的代码并创建了这个 GUI。

我没有扩展 Swing 组件,而是使用了 Swing 组件。

使用 GridBagLayoutGridLayout 更美观。

这是可运行的示例。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class TestManyTexts {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new TestManyTexts();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestManyTexts() {
        JFrame frame = new JFrame("Test Many Texts");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ManyTexts panel = new ManyTexts(new String[] { "First text: ", 
                "Second text: ", "Third Text:", "Forth Text" });
        frame.add(panel.getPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class ManyTexts {

        private JPanel panel;

        private JTextField[] fields;

        public ManyTexts(String[] labels) {
            createPanel(labels);
        }

        private void createPanel(String[] labels) {
            panel = new JPanel(new GridLayout(0, 2));
            panel.setBorder(new EmptyBorder(5, 5, 5, 5));

            fields = new JTextField[labels.length];

            for (int i = 0; i < labels.length; i++) {
                JLabel label = new JLabel(labels[i]);
                panel.add(label);

                fields[i] = new JTextField(15);
                panel.add(fields[i]);
            }
        }

        public JPanel getPanel() {
            return panel;
        }

        public JTextField[] getFields() {
            return fields;
        }

    }

}