首页上一页 1 下一页尾页 1 条记录 1/1页
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)
求解答!
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)
求解答!