JLabel only shows if initComponents() is deleted

后端 未结 1 1137
盖世英雄少女心
盖世英雄少女心 2021-01-27 13:11
MainFrame.java -JFrame

public MainFrame() {
        initComponents();
        Letters pl = new Letters(this);
        this.setContentPane(pl);
        this.setTitle(\"P         


        
相关标签:
1条回答
  • 2021-01-27 13:40

    The problem is that when using GroupLayout all components on the container must be added to both the horizontal & vertical parallel groups of the layout:

    In Letters.java, you have added the JLabel to the container but have not attached it to the 2 groups of the layout, so nothing will be displayed as a result.

    To fix, you could move the label creation to the initComponents method and add to the groups:

    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))
    );
    
    0 讨论(0)
提交回复
热议问题