Mr_Pang
AlstonWilliams
采纳率75%
2015-04-13 05:49 阅读 2.4k
已采纳

记事本程序的新建功能

给“新建”这个菜单项添加了如下监听器代码,可是运行时会出现错误。

newfile.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
boolean exitstext=textArea.getText().equals("");
if(!exitstext)
{
JOptionPane.showConfirmDialog(frame,"Do you want to save the contents?");
JOptionPane getuserchoose=new JOptionPane();
int userchoose=((Integer)getuserchoose.getValue()).intValue();//this sentence is wrong.But what's wrong?
if(userchoose==JOptionPane.OK_OPTION)
{
save();
textArea.setText("");
}
else if(userchoose==JOptionPane.NO_OPTION)
{
textArea.setText("");
}
}
}
catch(HeadlessException headlessexception)
{
System.out.println(headlessexception.getMessage());
}
}
}
);

其中save方法的代码为: public void save()
{
try
{
FileDialog savefiledialog=new FileDialog(frame,"save file",FileDialog.SAVE);
savefiledialog.setVisible(true);
String savefiledictory=savefiledialog.getDirectory();
String savefilename=savefiledialog.getFile()+".txt";
File savefile=new File(savefiledictory,savefilename);
FileWriter filewrite=new FileWriter(savefile);
BufferedWriter filebufferwriter=new BufferedWriter(filewrite);
filebufferwriter.write(textArea.getText(), 0,textArea.getText().length());
filebufferwriter.close();
filewrite.close();
}
catch(IOException ioexception)
{
ioexception.printStackTrace();
}
}

运行时一点击"是否保存内容"对话框里面的”是“就会出现下面的代码,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at NewNoteBook.NewNoteBook$2.actionPerformed(NewNoteBook.java:206)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:740)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:699)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:713)
at java.awt.EventQueue$4.run(EventQueue.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:710)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    danielinbiti danielinbiti 2015-04-13 05:56

    字符串转整型Integer.valueOf

     int userchoose=Integer.valueOf(getuserchoose.getValue());
    
    点赞 评论 复制链接分享

相关推荐