程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> javase-記事本程序的新建功能

javase-記事本程序的新建功能

編輯:編程綜合問答
記事本程序的新建功能

給“新建”這個菜單項添加了如下監聽器代碼,可是運行時會出現錯誤。

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)

最佳回答:


字符串轉整型Integer.valueOf

 int userchoose=Integer.valueOf(getuserchoose.getValue());
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved