-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();intoprivate void initComponents() {に使用されたこれらの座標が欠落していると、ではJLabel n = new JLabel();表示できず、表示できるのはJPaneljLabel1

  • コードブロックprivate void initComponents() {を削除し、GUIのハンドコーディングを続行します

  • JPanelFlowLayoutAPIに実装されています

  • JFrameBorderLayoutAPIに実装されています

ここに画像の説明を入力してください

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 に答える