已有101人关注
JAVA从入门到精通 第二版P418 程序问题
发表在Java图书答疑 2011-04-15
是否精华
版块置顶:
[font color=#0000FF][font color=#00FF00][font color=#FF0000]package Number;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.net.*;

import javax.swing.*;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;

public class ExampleFrame_12 extends JFrame
{
JDesktopPane desktopPane=null;
InternalFrame pInFrame=null;
InternalFrame rInFrame=null;
InternalFrame tInFrame=null;
public static void main(String[] args) 
{
ExampleFrame_12 frame=new ExampleFrame_12();
frame.setVisible(true);
}
public ExampleFrame_12()
{
super();
setTitle("企业人事管理系统");
setBounds(100,100,570,470);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
desktopPane=new JDesktopPane();
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
getContentPane().add(desktopPane,BorderLayout.CENTER);
final JLabel backLabel=new JLabel();
URL url = this.getClass().getResource("/back.JPG"); 
ImageIcon icon = new ImageIcon(url);
backLabel.setIcon(icon);
backLabel.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
desktopPane.add(backLabel,new Integer(Integer.MIN_VALUE));
    
final JPanel panel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
panel.setLayout(flowLayout);
getContentPane().add(panel, BorderLayout.NORTH);

final JButton personnelButton = new JButton();
personnelButton.setText("人事管理");
personnelButton
.addActionListener(new BAListener(pInFrame, "人事管理"));
panel.add(personnelButton);

final JButton reckoningButton = new JButton();
reckoningButton.setText("账套管理");
reckoningButton
.addActionListener(new BAListener(rInFrame, "账套管理"));
panel.add(reckoningButton);

final JButton treatmentButton = new JButton();
treatmentButton.setText("待遇报表");
treatmentButton
.addActionListener(new BAListener(tInFrame, "待遇报表"));
panel.add(treatmentButton);
}
private class BAListener implements ActionListener
{
InternalFrame inFrame;
String title;
public BAListener(InternalFrame inFrame,String title)
{
this.inFrame=inFrame;
this.title=title;
}
public  void actionPerformed(ActionEvent e)
{
if(inFrame==null||inFrame.isClosed())
{
JInternalFrame[] allFrames=desktopPane.getAllFrames();
int titleBarHight = 30 * allFrames.length;
int x = 10 + titleBarHight, y = x;// 设置窗体的显示位置
int width = 250, height = 180;// 设置窗体的大小
inFrame = new InternalFrame(title);// 创建指定标题的内部窗体
// 设置窗体的显示位置及大小
inFrame.setBounds(x, y, width, height);
inFrame.setVisible(true);// 设置窗体可见
desktopPane.add(inFrame);// 将窗体添加到桌面面板中
}
try
{
inFrame.setSelected(true);
}
catch (PropertyVetoException propertyVetoE)
{
propertyVetoE.printStackTrace();
}
}
}
private class  InternalFrame extends JInternalFrame
{
public InternalFrame(String title)
{
super();
setTitle(title);
setResizable(true);
setClosable(true);
setIconifiable(true);
setMaximizable(true);
URL url = this.getClass().getResource("/in_frame.PNG"); 
ImageIcon icon = new ImageIcon(url);
setFrameIcon(icon);
}
}

[font face=宋体]
程序执行后,点击人事管理这几项按钮会出现以下代码问题[/font]
[/font]
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Number.ExampleFrame_12$InternalFrame.<init>(ExampleFrame_12.java:102)
at Number.ExampleFrame_12$BAListener.actionPerformed(ExampleFrame_12.java:79)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
求解答!
分享到:
精彩评论 1
ccbwm
学分:0 LV1
TA的每日心情
开心
2022-10-30 14:26:53
2011-04-15
沙发
[FIELDSET][LEGEND]引自:楼主[/LEGEND]
[font color=#0000FF][font color=#00FF00][font color=#FF0000]package Number;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.net.*;

import javax.swing.*;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;

public class ExampleFrame_12 extends JFrame
{
JDesktopPane desktopPane=null;
InternalFrame pInFrame=null;
InternalFrame rInFrame=null;
InternalFrame tInFrame=null;
public static void main(String[] args) 
{
ExampleFrame_12 frame=new ExampleFrame_12();
frame.setVisible(true);
}
public ExampleFrame_12()
{
super();
setTitle("企业人事管理系统");
setBounds(100,100,570,470);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
desktopPane=new JDesktopPane();
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
getContentPane().add(desktopPane,BorderLayout.CENTER);
final JLabel backLabel=new JLabel();
URL url = this.getClass().getResource("/back.JPG"); 
ImageIcon icon = new ImageIcon(url);
backLabel.setIcon(icon);
backLabel.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
desktopPane.add(backLabel,new Integer(Integer.MIN_VALUE));
    
final JPanel panel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
panel.setLayout(flowLayout);
getContentPane().add(panel, BorderLayout.NORTH);

final JButton personnelButton = new JButton();
personnelButton.setText("人事管理");
personnelButton
.addActionListener(new BAListener(pInFrame, "人事管理"));
panel.add(personnelButton);

final JButton reckoningButton = new JButton();
reckoningButton.setText("账套管理");
reckoningButton
.addActionListener(new BAListener(rInFrame, "账套管理"));
panel.add(reckoningButton);

final JButton treatmentButton = new JButton();
treatmentButton.setText("待遇报表");
treatmentButton
.addActionListener(new BAListener(tInFrame, "待遇报表"));
panel.add(treatmentButton);
}
private class BAListener implements ActionListener
{
InternalFrame inFrame;
String title;
public BAListener(InternalFrame inFrame,String title)
{
this.inFrame=inFrame;
this.title=title;
}
public  void actionPerformed(ActionEvent e)
{
if(inFrame==null||inFrame.isClosed())
{
JInternalFrame[] allFrames=desktopPane.getAllFrames();
int titleBarHight = 30 * allFrames.length;
int x = 10 + titleBarHight, y = x;// 设置窗体的显示位置
int width = 250, height = 180;// 设置窗体的大小
inFrame = new InternalFrame(title);// 创建指定标题的内部窗体
// 设置窗体的显示位置及大小
inFrame.setBounds(x, y, width, height);
inFrame.setVisible(true);// 设置窗体可见
desktopPane.add(inFrame);// 将窗体添加到桌面面板中
}
try
{
inFrame.setSelected(true);
}
catch (PropertyVetoException propertyVetoE)
{
propertyVetoE.printStackTrace();
}
}
}
private class  InternalFrame extends JInternalFrame
{
public InternalFrame(String title)
{
super();
setTitle(title);
setResizable(true);
setClosable(true);
setIconifiable(true);
setMaximizable(true);
URL url = this.getClass().getResource("/in_frame.PNG"); 
ImageIcon icon = new ImageIcon(url);
setFrameIcon(icon);
}
}

[font face=宋体]
程序执行后,点击人事管理这几项按钮会出现以下代码问题[/font]
[/font]
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Number.ExampleFrame_12$InternalFrame.<init>(ExampleFrame_12.java:102)
at Number.ExampleFrame_12$BAListener.actionPerformed(ExampleFrame_12.java:79)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
求解答!
[/FIELDSET]

回复:应该是没有找到图片,所以才会报java.lang.NullPointerException异常
  
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照