-1

可能重复:
JLabel 仅显示是否删除了 initComponents()

JavaApplication1.java

这就是我打电话给我的单独小组的地方。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import javax.swing.JFrame;

/**
 *
 * @author Aires
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       JFrame f = new JFrame();
       NewJPanel n1 = new NewJPanel();
       f.add(n1);
       f.pack();
       f.show();
    }
}

newJPanel.java 我正在创建新的 JLabel 以添加到我当前的面板中,但是当我运行该程序时,它没有显示出来。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Nov 10, 2012, 3:07:31 PM
 */
package javaapplication1;

import javax.swing.JLabel;


/**
 *
 * @author Aires
 */
public class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        //pag kinoment ko ito, tsaka lang lamalabas yung jlabel.
        initComponents();

        //dito Sir, di siya nalalagay sa panel.
        JLabel n = new JLabel();
        n.setText("asdasdasd");
        this.add(n);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        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(175, 175, 175)
                .addComponent(jLabel1)
                .addContainerGap(191, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jLabel1)
                .addContainerGap(159, Short.MAX_VALUE))
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}

我编码创建的 JLabel 没有显示,它仅在我注释掉 //initComponents(); 时显示 有什么解决办法吗?

4

2 回答 2

5
  • JLabel n = new JLabel(); 不可见,因为使用GroupLayout了所需的一堆代码verticalhorizontal坐标JPanel

  • JLabel n = new JLabel();缺少这些用于jLabel1 = new javax.swing.JLabel();into private void initComponents() {then的坐标JLabel n = new JLabel();在 中不可见JPanel,您只能看到jLabel1

  • 删除代码块private void initComponents() {并继续为您的 GUI 手动编码

  • JPanelFlowLayout在 API中实现

  • JFrameBorderLayout在 API中实现

在此处输入图像描述

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JavaApplication1 {

    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();
    private JLabel label = new JLabel();

    public JavaApplication1() {
        label.setText("asdasdasd");
        panel.add(label);
        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JavaApplication1 j1 = new JavaApplication1();
            }
        });
    }
}
于 2012-11-10T07:46:28.087 回答
3

IMO,您必须将自己的 JPanel 添加到 JFrame 的 ContentPane 中。

此外,使用 SwingUtilities 显示 GUI。

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {    
            public void run() {
                JFrame f = new JFrame();
                NewJPanel n1 = new NewJPanel();
                f.getContentPane().add(n1, BorderLayout.CENTER);
                f.pack();
                f.show();
            }
        });
    }

类 NewJPanel 代码。

class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        //pag kinoment ko ito, tsaka lang lamalabas yung jlabel.
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        JLabel n = new JLabel();
        n.setText("asdasdasd");

        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(175, 175, 175)
                .addComponent(jLabel1)
                .addComponent(n)
                .addContainerGap(191, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jLabel1)
                .addComponent(n)
                .addContainerGap(159, Short.MAX_VALUE))
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}
于 2012-11-10T08:04:56.810 回答