Results 1 to 8 of 8
- 06-27-2012, 03:31 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
A problem about pushing a exit button to exit my java swing program
I try to write a simple text editor.
When I click the exit button ,I get a exception as blow:
And here's my code :Java Code:Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: at com.black.System.exit(System.java:959) at com.black.HomeWork09$4.actionPerformed(HomeWork09.java:98) 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.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$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)
Can somebody help me to find what's wrong?Java Code:package com.black; import javax.swing.*; import java.awt.event.*; import java.io.*; public class HomeWork09 { private boolean fileSaved = false; private JFrame frame; private JTextArea textArea; private JMenuItem newFile; private JMenuItem openFile; private JMenuItem saveFile; private JMenuItem exit; public HomeWork09() { frame = new JFrame("Simple Text Editor"); textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JMenuBar bar = new JMenuBar(); JMenu file = new JMenu("File"); newFile = new JMenuItem("Open New File"); openFile = new JMenuItem("Open File"); saveFile = new JMenuItem("Save File"); exit = new JMenuItem("Exit"); newFile.addActionListener(newFileListener); openFile.addActionListener(OpenFileListener); saveFile.addActionListener(SaveFileListener); exit.addActionListener(ExitListener); file.add(newFile); file.add(openFile); file.add(saveFile); file.add(exit); bar.add(file); frame.setJMenuBar(bar); frame.add(textArea); frame.setSize(500, 600); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new HomeWork09(); } ActionListener newFileListener = new ActionListener() { public void actionPerformed(ActionEvent event) { int n = 0; if (fileSaved == false && !textArea.getText().isEmpty()) { n = JOptionPane.showConfirmDialog(frame, "You haven't saved a file,sure to open a new one?", "Simple Text Editor", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { textArea.setText(null); fileSaved = false; } } else { textArea.setText(null); fileSaved = false; } } }; ActionListener OpenFileListener = new ActionListener() { public void actionPerformed(ActionEvent event) { JFileChooser openFileDialog = new JFileChooser(); openFileDialog.setDialogTitle("Open File"); openFileDialog.showOpenDialog(frame); open(openFileDialog.getSelectedFile()); } }; ActionListener SaveFileListener = new ActionListener() { public void actionPerformed(ActionEvent event) { JFileChooser saveFile = new JFileChooser(); saveFile.setDialogTitle("Save New File"); saveFile.showSaveDialog(frame); save(saveFile.getSelectedFile()); } }; ActionListener ExitListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }; private void open(File file) { if (file != null) { try { String str; StringBuilder s = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(file)); while ((str = br.readLine()) != null) { s.append(str); s.append("\n"); } textArea.setText(s.toString()); } catch (IOException e) { e.printStackTrace(); } } } private void save(File file) { if (file != null) { try { String s = textArea.getText(); BufferedWriter bf = new BufferedWriter(new FileWriter(file)); bf.write(s); bf.close(); fileSaved = true; } catch (IOException ex) { ex.printStackTrace(); } } } }
Thank you,Best regards!
- 06-27-2012, 03:33 PM #2
Re: A problem about pushing a exit button to exit my java swing program
That means you have a compiler error. Track down what that is before trying to run your program.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 06-27-2012, 03:36 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: A problem about pushing a exit button to exit my java swing program
Thank you for your answering
But I can't see any warning icon in my Eclipse.
Is there any other way to find that compiler error?
- 06-27-2012, 03:48 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: A problem about pushing a exit button to exit my java swing program
Well....That's really weird...
I change the workspace to another directory.
Then this works just perfectly fine.
So the problem solved.
I think the reason may because I put my src file in dropbox , and the file's synchronization has problem.So after I copy the file to another directory , it will work. Just guessing.
Anyway , thank you for answering again.Last edited by blackdiz; 06-27-2012 at 03:56 PM.
- 06-27-2012, 03:53 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: A problem about pushing a exit button to exit my java swing program
com.black.System.exit(System.java:959)
You haven't got a System class have you by any chance?Please do not ask for code as refusal often offends.
- 06-27-2012, 04:04 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: A problem about pushing a exit button to exit my java swing program
Oh, Yes~ I put the System.java in my package(just curious what's inside that, so I copy it into my package and use eclipse to look) , the error happens.
After I delete it , all works!!
So the problem is I put the System.java in my package, when I call System.exit(0) , it will look into the System.java in my package rather than the System.java in the Java src.zip , right?
I really do something silly.gif)
Thank you. Best Regards!Last edited by blackdiz; 06-27-2012 at 04:06 PM.
- 06-27-2012, 04:24 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: A problem about pushing a exit button to exit my java swing program
Yep, or at least that's when it notices the System.java is different (and non-compilable as its package declaration and location don't match).
If you want Eclipse to bring up the source code then CTRL-left click on the classname in your code (or method name) and it'll either bring up the source code or ask you where the source code is located.Please do not ask for code as refusal often offends.
- 06-27-2012, 05:30 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Netbeans GUI EXIT button?
By blackz in forum AWT / SwingReplies: 4Last Post: 04-04-2012, 04:28 AM -
Could not find the main class: java. Program will exit
By thewonkits in forum New To JavaReplies: 3Last Post: 01-11-2012, 11:56 AM -
Java virtual machine:Could not find Main Class: d. Program will exit.
By xHolyMariox in forum New To JavaReplies: 4Last Post: 08-22-2011, 03:42 PM -
How to exit the program by typing "exit"?
By Laythe in forum New To JavaReplies: 6Last Post: 08-19-2009, 08:32 PM -
Adding EXIT button on MIDlet form
By Java Tip in forum Java TipReplies: 0Last Post: 11-22-2007, 10:13 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks