我有一个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
. 我究竟做错了什么?