import java.awt.BorderLayout;/** * * @author Administrator */ public class ClassDelPanel extends javax.swing.JPanel {
/** Creates new form ClassDelPanel */ public ClassDelPanel() { 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. */ // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents private void initComponents() { //... }// </editor-fold>//GEN-END:initComponents
getParent()也是空指针异常。
当然是父容器组件,怎么能是父类
看API说明:getParent
public Container getParent()获得此组件的父级。
返回:
此组件的父级容器
从以下版本开始:
JDK1.0 你是怎么用的?
如果你的组件你还没有add到其他组件中去就调用了
getParent()
那肯定是返回null嘛
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ClassDelPanel1 delclass = new ClassDelPanel1();
this.getParent().removeAll();
this.getParent().add(delclass, BorderLayout.CENTER);
this.getParent().validate();
}当前面板已加入JFrame里了
你这个是一个自定义的JPanel类嘛?
能不能贴完整代码
import java.awt.BorderLayout;/**
*
* @author Administrator
*/
public class ClassDelPanel extends javax.swing.JPanel {
/** Creates new form ClassDelPanel */
public ClassDelPanel() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents private void initComponents() {
//... }// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ClassDelPanel1 delclass = new ClassDelPanel1();
this.getParent().removeAll();
this.getParent().add(delclass, BorderLayout.CENTER);
this.getParent().validate();
}
static String grade, dept;
}
你先
this.getParent().removeAll();
这样就已经把你的这个组件从其父组件中移除了
之后再
this.getParent().add(delclass, BorderLayout.CENTER);
this.getParent().validate();
肯定找不到父组件了嘛你应该这样:
Container cont = this.getParent();
cont.removeAll();
cont.add(delclass, BorderLayout.CENTER);
cont.validate();
我还有个问题:我在当前面板用静态变量保存了年级和系别信息,想在下一个面板调用变量当作参数。java没有报错,但始终输出结果没有根据输入改变。//面板1代码:
grade = jComboBox1.getSelectedItem().toString();
dept = jComboBox2.getSelectedItem().toString();
//面板2代码:
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(Sgms.dataAccess.classOut(ClassDelPanel.grade, ClassDelPanel.dept)));
之后才执行了改变grade和dept的值的方法吧?
我把改变值的方法放到LISENER外面了,所以改变不了了。