JLabel仅显示是否删除了initComponents()

时间:2012-11-09 19:06:22

标签: java swing jframe jpanel jlabel

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

如果删除了Letters.java中的initComponents(),那么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>

1 个答案:

答案 0 :(得分:3)

问题在于,当使用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))
);