使用Graphics出现.NullPointerException初五

ljs34101130 2013-06-18 10:27:14
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class StafffingWindow extends JFrame{
private static JTextArea jtaDescription = new JTextArea();
OperatingToSQL op = new OperatingToSQL();
private static Graphics g;

public StafffingWindow() {
add(new paint());
}

public static void main(String[] args) {
JFrame frame = new StafffingWindow();
frame.pack();
frame.setTitle("操作初始界面Vol 0.1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);

}

class paint extends JPanel{
JButton select = new JButton("查询数据");
JButton insert = new JButton("插入数据");
JButton update = new JButton("更改数据");
JButton delete = new JButton("删除数据");

public paint() {
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame win = new JFrame();
//对jtaDescription进行设置
jtaDescription.setLineWrap(true);
jtaDescription.setWrapStyleWord(true);
jtaDescription.setEditable(false);
jtaDescription.setText(op.selectTable());
win.setSize(200, 200);
win.setTitle("Infomation");
win.setVisible(true);
win.setLayout(new BorderLayout());
//将文本域放入win框架中
win.add(jtaDescription, BorderLayout.CENTER);
// win.add(setLabel(), BorderLayout.NORTH);
}
});

insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op.insert();
}
});

update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
op.Update();
}
});

delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op.Delete();
}
});
setLayout(new GridLayout(1,4));
add(select);
add(insert);
add(update);
add(delete);

}
}
/** 有问题 */
protected static JPanel setLabel() {
JPanel jlbl = new JPanel();
jlbl.paintComponents(g);
g.drawString("姓名", 0, 0);//这里报错
g.drawString("性别", 200 / 3, 0);
g.drawString("身份证号码", 200 / 3 * 2, 0);

return jlbl;
}
}
...全文
75 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljs34101130 2013-06-18
  • 打赏
  • 举报
回复
好吧,是错误。打错了
gloomyfish 2013-06-18
  • 打赏
  • 举报
回复
你在哪里初始化了
private static Graphics g;
在JAVA Swing中获取图像设备Graphics有两种途径 1.通过override paint(Graphics g)方法或者paintComponent(Graphics g)方法 2.创建BufferedImage对象,从BufferedImage对象中获取图像设备Graphics 显然你没有这么做。所以你无法获取Graphics。

50,531

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧