我对挥杆还很陌生,所以我相信这可能很简单。希望你们中的一些人能帮我一把。这是一个更复杂场景的一部分,但我设法把它放在一个简单的例子中。
我正在使用 Netbeans,我正在尝试创建一个 Frame,其中包含一个带有一些复选框的面板(由框架动态添加)。
如果我使用可视化编辑器来创建带有原生组件的整个结构,它运行良好,但问题是我需要一个自定义面板,而这就是它中断的地方。
当我执行应用程序时,我只看到一个没有内容的空白框。
代码如下
框架:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package contacorrente;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JCheckBox;
/**
*
* @author afsilva
*/
public class Test extends javax.swing.JFrame {
/**
* Creates new form Test
*/
public Test() {
initComponents();
this.testPanel1.add(new JCheckBox("ergcdf"));
this.testPanel1.add(new JCheckBox("ergcdf1"));
this.testPanel1.add(new JCheckBox("ergcdf2"));
this.testPanel1.add(new JCheckBox("ergcdf3"));
this.testPanel1.add(new JCheckBox("ergcdf4"));
this.testPanel1.validate();
this.validate();
}
/**
* 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() {
testPanel1 = new contacorrente.TestPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(51, 204, 255));
testPanel1.setBackground(new java.awt.Color(255, 51, 204));
testPanel1.setName("testPanel1"); // NOI18N
javax.swing.GroupLayout testPanel1Layout = new javax.swing.GroupLayout(testPanel1);
testPanel1.setLayout(testPanel1Layout);
testPanel1Layout.setHorizontalGroup(
testPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
testPanel1Layout.setVerticalGroup(
testPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(testPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(testPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private contacorrente.TestPanel testPanel1;
// End of variables declaration
}
我不会粘贴面板代码,因为它只是一个空面板(没有自定义代码)
请帮忙..
提前致谢