JLabel的,才会显示的initComponents()被删除(JLabel only shows

2019-08-02 13:19发布

MainFrame.java -JFrame

public MainFrame() {
        initComponents();
        Letters pl = new Letters(this);
        this.setContentPane(pl);
        this.setTitle("Preset Lessons");
        this.pack();
}

Letters.java  -JPanel

public Letters(JFrame frame) {
        initComponents(); 
        JLabel label = new JLabel();
        label.setText("Sample");
        this.add(label);
}

如果的initComponents()在Letters.java是多数民众赞成在唯一一次删除的JLabel会显示出来。 我怎样才能把新的JLabel以我现有的JPanel?

Letters.java的的initComponents()的内容; 如果我在构造函数中删除iniComponents它会创建一个JLabel的。

private void initComponents() {

        jLabelLetters = new javax.swing.JLabel();
        jLabelNumbers = new javax.swing.JLabel();
        jLabelFlashcards = new javax.swing.JLabel();
        jLabelStories = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setMaximumSize(new java.awt.Dimension(333333, 333333));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));

        jLabelLetters.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabelLetters.setText("Letters");

        jLabelNumbers.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabelNumbers.setText("Numbers");
        jLabelNumbers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelNumbersMouseClicked(evt);
            }
        });

        jLabelFlashcards.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabelFlashcards.setText("Flashcards");
        jLabelFlashcards.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelFlashcardsMouseClicked(evt);
            }
        });

        jLabelStories.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabelStories.setText("Stories");
        jLabelStories.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelStoriesMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabelLetters)
                .addGap(75, 75, 75)
                .addComponent(jLabelNumbers)
                .addGap(76, 76, 76)
                .addComponent(jLabelFlashcards)
                .addGap(75, 75, 75)
                .addComponent(jLabelStories)
                .addGap(89, 89, 89)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelFlashcards)
                        .addComponent(jLabelStories))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelLetters)
                        .addComponent(jLabelNumbers)))
                .addGap(565, 565, 565))
        );
    }// </editor-fold>

Answer 1:

的问题是,当使用的GroupLayout容器上的所有组件必须被添加到该布局的两个水平和垂直并行组:

Letters.java ,您已经添加了JLabel到容器,但没有它连接到2组的布局,所以什么都不会被显示为结果。

要解决,你可以在标签创建移动到initComponents方法和添加到组:

layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(25, 25, 25)
    .addComponent(jLabelLetters)
    .addGap(75, 75, 75)
    .addComponent(jLabelNumbers)
    .addGap(76, 76, 76)
    .addComponent(jLabelFlashcards)
    .addGap(75, 75, 75)
    .addComponent(jLabelStories)
    .addGap(89, 89, 89)
    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(32, Short.MAX_VALUE))
    .addComponent(label)
);

layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabelFlashcards)
    .addComponent(jLabelStories))
    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
    .addGap(4, 4, 4)
    .addComponent(jComboBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabelLetters)
    .addComponent(jLabelNumbers)))
    .addComponent(label)
    .addGap(565, 565, 565))
);


文章来源: JLabel only shows if initComponents() is deleted