每次做界面都会出问题。看来练习的太少了。
这次是抛出NULLpointexception。
希望,明白的人帮我看看。谢谢了~
版主提示以后改过,可是还不行。麻烦大家了,帮着看看。
下边是改过以后的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class AccountFrame extends JFrame{
Container container;
GridBagLayout gb;
GridBagConstraints gbc;
JButton ok;
JButton check;
JLabel dateLabel;
JLabel yearLabel;
JLabel monthLabel;
JLabel dayLabel;
JLabel accountLabel;
JTextField yearField;
JTextField monthField;
JTextField dayField;
JTextField accountField;
public AccountFrame(String title){
super("title");
Container container = getContentPane();
gb = new GridBagLayout();
gbc = new GridBagConstraints();
container.setLayout(gb);
ok = new JButton("存储");
check = new JButton("查询");
dateLabel = new JLabel("日期");
yearLabel = new JLabel("年");
monthLabel = new JLabel("月");
dayLabel = new JLabel("日");
yearField = new JTextField(4);
monthField = new JTextField(2);
dayField = new JTextField(2);
accountLabel = new JLabel("当日开销");
accountField = new JTextField("请输入开销");
//向容器中舔加组件
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(dateLabel,0,0,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(yearField,1,0,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(yearLabel,1,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(monthField,1,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(monthLabel,1,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(dayField,1,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(dayLabel,1,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(accountLabel,2,0,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(accountField,2,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(ok,2,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(check,2,6,1,1);
}
public void addComponent(Component c,int row,int col,int nrow,int ncol) {
gbc.gridx = col;
gbc.gridy = row;
gbc.gridwidth = ncol;
gbc.gridheight = nrow;
gb.setConstraints(c,gbc);
container.add(c);
}
public static void main(String[] args){
AccountFrame mainframe = new AccountFrame("记帐");
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(400,400);
mainframe.setVisible(true);
}
}
[此贴子已经被作者于2006-5-26 3:36:08编辑过]