java里边图形界面编程
我写了一个图形界面的程序没有报错,但是运行的时候出错了。请问这是哪里的问题Exceptioninthread"main"java.lang.NullPointerExce...
我写了一个图形界面的程序没有报错,但是运行的时候出错了。请问这是哪里的问题
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at test2.<init>(test2.java:21)
at test2.main(test2.java:8)
下边是源代码
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
public class test2 extends JFrame{
JButton a1,a2,a3,a4,a5;
public static void main(String[] args){
test2 a=new test2();
}
public test2(){
a1=new JButton("东");
a2=new JButton("西");
a2=new JButton("南");
a2=new JButton("北");
a2=new JButton("中");
this.add(a1,BorderLayout.EAST);
this.add(a2,BorderLayout.WEST);
this.add(a3,BorderLayout.SOUTH);
this.add(a4,BorderLayout.NORTH);
this.add(a5,BorderLayout.CENTER);
this.setTitle("边界布局BorderLayout");
this.setSize(500,400);
this.setLocation(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
} 展开
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at test2.<init>(test2.java:21)
at test2.main(test2.java:8)
下边是源代码
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
public class test2 extends JFrame{
JButton a1,a2,a3,a4,a5;
public static void main(String[] args){
test2 a=new test2();
}
public test2(){
a1=new JButton("东");
a2=new JButton("西");
a2=new JButton("南");
a2=new JButton("北");
a2=new JButton("中");
this.add(a1,BorderLayout.EAST);
this.add(a2,BorderLayout.WEST);
this.add(a3,BorderLayout.SOUTH);
this.add(a4,BorderLayout.NORTH);
this.add(a5,BorderLayout.CENTER);
this.setTitle("边界布局BorderLayout");
this.setSize(500,400);
this.setLocation(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
} 展开
2个回答
展开全部
修改成这样:
import java.awt.*;
import javax.swing.*;
public class test2 extends JFrame
{
public static void main(String[] args)
{
test2 a = new test2();
}
public test2()
{
JButton a1 = new JButton("东");
JButton a2 = new JButton("西");
JButton a3 = new JButton("南");
JButton a4 = new JButton("北");
JButton a5 = new JButton("中");
this.add(a1, BorderLayout.EAST);
this.add(a2, BorderLayout.WEST);
this.add(a3, BorderLayout.SOUTH);
this.add(a4, BorderLayout.NORTH);
this.add(a5, BorderLayout.CENTER);
this.setTitle("边界布局BorderLayout");
this.setSize(500, 400);
this.setLocation(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class test2 extends JFrame
{
public static void main(String[] args)
{
test2 a = new test2();
}
public test2()
{
JButton a1 = new JButton("东");
JButton a2 = new JButton("西");
JButton a3 = new JButton("南");
JButton a4 = new JButton("北");
JButton a5 = new JButton("中");
this.add(a1, BorderLayout.EAST);
this.add(a2, BorderLayout.WEST);
this.add(a3, BorderLayout.SOUTH);
this.add(a4, BorderLayout.NORTH);
this.add(a5, BorderLayout.CENTER);
this.setTitle("边界布局BorderLayout");
this.setSize(500, 400);
this.setLocation(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询