java程序——awt
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
class myFrame extends Frame //implements ActionListener
{ Box baseBox,boxV1,boxV2;
Button bt1,bt2,bt3;
TextField t1,t2,t3,t4;
Label lb1,lb2,lb3,lb4,lb5;
myFrame(String s)
{
super(s);
lb1=new Label("请输入三角形的第一条边:");
lb2=new Label("请输入三角形的第二条边:");
lb3=new Label("请输入三角形的第三条边:");
lb4=new Label("三角形的面积是:");
lb5=new Label("");
boxV1=Box.createVerticalBox();
boxV1.add(lb1);
boxV1.add(Box.createVerticalStrut(10));
boxV1.add(lb2);
boxV1.add(Box.createVerticalStrut(10));
boxV1.add(lb3);
boxV1.add(Box.createVerticalStrut(10));
boxV1.add(lb4);
boxV1.add(Box.createVerticalStrut(10));
boxV1.add(lb5);
boxV1.add(Box.createVerticalStrut(10));
boxV1.add(bt3);
boxV1.add(Box.createVerticalStrut(10));
t1=new TextField(12);
t2=new TextField(12);
t3=new TextField(12);
t4=new TextField(12);
bt1=new Button("判断三边");
bt2=new Button("退出程序");
bt3=new Button("计算面积");
// bt1.addActionListener(this);
//bt2.addActionListener(this);
//bt3.addActionListener(this);
boxV2=Box.createVerticalBox();
boxV2.add(t1);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t2);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t3);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t4);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(bt1);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(bt2);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(10));
setLayout(new FlowLayout());
add(baseBox);
setBounds(120,125,250,150);
setResizable(false);
setVisible(true);
}
}
public class Area
{
public static void main(String[] args)
{
myFrame m=new myFrame("求三角形的面积");
}
}
编译时没出错,运行时出现:
Exception in thread "main"
java.lang.NullPointerException------解决方案--------------------
你上面的程序在对象没初始化就添加到box上面当然显示
空指针 一定要先new出来 在add