将GUI Designer JPanel添加到JFrame

时间:2012-12-03 17:17:03

标签: java swing netbeans jframe jpanel

我在NetBeans中有一个扩展JPanel的类。该类正在使用“表单设计器”,您只需在屏幕上拖放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。我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

  • 不要覆盖paint(...),因为这很少需要,如果完成,必须小心,因为此方法不仅负责绘制组件,还绘制其边框和其子组件< / em> - 你似乎遇到的问题。
  • 而是覆盖paintComponent(...),但请务必在此方法中调用super.paintComponent(...),通常作为方法的第一行。