【问题标题】:Add GUI Designer JPanel to JFrame将 GUI 设计器 JPanel 添加到 JFrame
【发布时间】:2012-12-03 17:17:02
【问题描述】:

我在 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。我做错了什么?

【问题讨论】:

  • “我有一个扩展 JPanel 的类..另一个扩展 JFrame 的类..” 除非添加功能,否则不要扩展。只需使用实例并添加组件。

标签: java swing netbeans jframe jpanel


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多