当前位置: 代码迷 >> Java相关 >> 一个网格包的问题
  详细解决方案

一个网格包的问题

热度:249   发布时间:2006-05-24 16:58:00.0
一个网格包的问题


每次做界面都会出问题。看来练习的太少了。
这次是抛出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编辑过]

搜索更多相关的解决方案: 网格  

----------------解决方案--------------------------------------------------------
JLabel yearLabel;
JLabel monthLabel;
没有初始化
----------------解决方案--------------------------------------------------------

谢谢版主,居然存在这么低级的错误,汗一个。
可是
初始化以后还是有
异常。
在container.add(c);
那一行……


----------------解决方案--------------------------------------------------------
还是有东西没有初始化
----------------解决方案--------------------------------------------------------
我自己再看看。
----------------解决方案--------------------------------------------------------

改过一次,又跑到
AccountFrame mainframe = new AccountFrame("记帐");
了,愁……
我知道这种事不该老麻烦人。

可是希望,有人能费心帮我看一眼……
改后的代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
//public class AccountBook {

//}
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 = 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();
monthField = new JTextField();
dayField = new JTextField();
accountLabel = new JLabel("当日开销");
accountField = new JTextField("请输入开销");

//向容器中舔加组件
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(dateLabel,0,0,1,1);

gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(yearLabel,1,0,1,1);

gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(yearField,1,1,1,1);

gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(monthLabel,1,2,1,1);

gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(monthField,1,3,1,1);

gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(dayLabel,1,4,1,1);

gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(dayField,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.setVisible(true);
mainframe.setSize(400,400);
}

}


----------------解决方案--------------------------------------------------------
你把所有的都成员变量都初始化后是没有异常的,我这里现在可以运行了
还有你要把你的mainframe.setVisible(true);
mainframe.setSize(400,400);
两个位置互相换一下
否则就会一片白
----------------解决方案--------------------------------------------------------
好,我再找找。嘿嘿~
惭愧啊,谢谢版主了。
----------------解决方案--------------------------------------------------------
JLabel dateLabel;
JLabel yearLabel;
JLabel monthLabel;
JLabel dayLabel;
这四个都初始化都没事了
然后再把那两个位置对调一下

----------------解决方案--------------------------------------------------------
我确定都初始化了。还是不行……
和编译环境有关系么……?
我用的netbeans
----------------解决方案--------------------------------------------------------
  相关解决方案