1

我有一个JPanel在 NetBeans 中扩展的类。该类正在使用“表单设计器”,您可以在其中将 GUI 元素拖放到屏幕上。我有另一个扩展类,JFrame它正在创建此类的一个实例并将其添加到其内容框架中。

JPanel班级:

/**
 * Creates new form OpenPanel
 */
public OpenPanel() {
    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() {

    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();

    jLabel2.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
    jLabel2.setText("Jungle Tracks");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(87, Short.MAX_VALUE)
            .addComponent(jLabel2)
            .addGap(86, 86, 86))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(21, Short.MAX_VALUE)
            .addComponent(jLabel2)
            .addGap(20, 20, 20))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 225, Short.MAX_VALUE))
    );
}// </editor-fold>

// Variables declaration - do not modify
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

JFrame类(我试图将 JPanel 添加到其中)如下:

public class MainClass extends JFrame {

static final int WIDTH = 800;
static final int HEIGHT = 600;

public MainClass() throws FileNotFoundException, IOException, UnsupportedAudioFileException, LineUnavailableException, BackingStoreException {
    super("MainClass");

    setSize(WIDTH, HEIGHT);
    setBackground(Color.WHITE);

    OpenPanel open = new OpenPanel();
    ((OpenPanel)open).setFocusable(true);

            getContentPane().add(open);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);              
    setVisible(true);
}

public static void main(String[] args) throws FileNotFoundException, IOException, UnsupportedAudioFileException, LineUnavailableException, BackingStoreException {
    MainClass mc = new MainClass();
}

}

但是没有显示在 NetBean 的设计视图中创建的 JPanel 及其 UI 元素,或者据我所知——甚至添加到JFrame. 我究竟做错了什么?

4

1 回答 1

3

我猜您的paint(...)方法没有调用其中的super.paint(...)方法,因此 JPanel 没有很好地绘制自己的组件。如果是这样,那么我建议:

  • 不要覆盖paint(...),因为这很少需要,如果完成,必须小心完成,因为此方法不仅负责绘制组件,还负责绘制其边框及其子组件——您似乎遇到的正是这个问题。
  • 取而代之的是覆盖paintComponent(...),但一定要super.paintComponent(...)在这个方法中调用,通常作为方法的第一行。
于 2012-12-03T18:10:56.647 回答