java编程中,创建主页面类(extends JFrame),副页面(extends 主页面),如何在副页面增加新的显示文本
在主页面中,创建了许多子页面(都是显示在主屏幕上),创建了一个副页面(extends主页面),需要在众多子页面中做一些修改,例如子页面1增加一个显示文本.但是报错Exce...
在主页面中,创建了许多子页面(都是显示在主屏幕上),创建了一个副页面(extends 主页面),需要在众多子页面中做一些修改,例如子页面1增加一个显示文本.但是报错
Exception in the thread "main" java.lang.NullPointerException应该是空指针。请大神帮忙看下哪里有错误。
public class Main{
public static void main(String[] args){
Windows mainframe=new Windows();
mainframe.setVisible(true);
Windows2 subframe=new Windows2();
subframe.setVisible(true);
}
}
public class Windows extends JFrame{
private MasterSta myMasterSta;
public Windows(){
this.setTitle("aaaaa");
this.setBounds(200,50,915,600);
this.setlayout(null);
this.setResizable(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
MasterSta myMasterSta=new MasterSta();
this.add(myMasterSta);
}
}
public class MasterSta extends JPanel{
private JLabel mystatus;
public MasterSta(){
Border Border_Info=BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder tBorder_Info=BorderFactory.creatTitledBorder(Border_Info,"",TitleBorder.CENTER,TitledBorder.TOP);
tBorder_Info.setTitleFont("宋体",Font.BOLD,11);
this.setBounds(3,4,1366,84);
this.setBorder(tBorder_Info);
this.layout(null);
mystatus=new JLabel("显示",JLabel.LEFT);
mystatus.setBounds(10,5,80,20);
this.add(mystatus);
}
public MasterSta getMasterSta(){
return getMasterSta;
}
}
Public class Windows2 extends Windows{
private JLabel xinzeng=new JLabel();
public Windows2(){
xinzeng=new JLabel("显示2",JLabel.LEFT);
xinzeng.setBounds(10,5,80,20);
this.getMaterSta().add(xinzeng);
}
}
程序就是这样子,一运行起来就报错指针错误,请大神指点,新手勿喷,在线等!!!!!! 展开
Exception in the thread "main" java.lang.NullPointerException应该是空指针。请大神帮忙看下哪里有错误。
public class Main{
public static void main(String[] args){
Windows mainframe=new Windows();
mainframe.setVisible(true);
Windows2 subframe=new Windows2();
subframe.setVisible(true);
}
}
public class Windows extends JFrame{
private MasterSta myMasterSta;
public Windows(){
this.setTitle("aaaaa");
this.setBounds(200,50,915,600);
this.setlayout(null);
this.setResizable(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
MasterSta myMasterSta=new MasterSta();
this.add(myMasterSta);
}
}
public class MasterSta extends JPanel{
private JLabel mystatus;
public MasterSta(){
Border Border_Info=BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder tBorder_Info=BorderFactory.creatTitledBorder(Border_Info,"",TitleBorder.CENTER,TitledBorder.TOP);
tBorder_Info.setTitleFont("宋体",Font.BOLD,11);
this.setBounds(3,4,1366,84);
this.setBorder(tBorder_Info);
this.layout(null);
mystatus=new JLabel("显示",JLabel.LEFT);
mystatus.setBounds(10,5,80,20);
this.add(mystatus);
}
public MasterSta getMasterSta(){
return getMasterSta;
}
}
Public class Windows2 extends Windows{
private JLabel xinzeng=new JLabel();
public Windows2(){
xinzeng=new JLabel("显示2",JLabel.LEFT);
xinzeng.setBounds(10,5,80,20);
this.getMaterSta().add(xinzeng);
}
}
程序就是这样子,一运行起来就报错指针错误,请大神指点,新手勿喷,在线等!!!!!! 展开
1个回答
2017-01-06
展开全部
MasterSta myMasterSta=new MasterSta();
--->
myMasterSta=new MasterSta();
public MasterSta getMasterSta()整个函数移到 class Windows extends JFrame{}内部,并修改使用正确的变量返回。
--->
myMasterSta=new MasterSta();
public MasterSta getMasterSta()整个函数移到 class Windows extends JFrame{}内部,并修改使用正确的变量返回。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询