Results 1 to 8 of 8
Like Tree4Likes
  • 1 Post By KevinWorkman
  • 2 Post By Tolls
  • 1 Post By Tolls

Thread: A problem about pushing a exit button to exit my java swing program

  1. #1
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default 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:
    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)
    And here's my code :
    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();
    			}
    		}
    	}
    }
    Can somebody help me to find what's wrong?
    Thank you,Best regards!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default 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.
    blackdiz likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default 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?

  4. #4
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default 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?
    blackdiz and Fubarable like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default 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
    Thank you. Best Regards!
    Last edited by blackdiz; 06-27-2012 at 04:06 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default 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.
    blackdiz likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: A problem about pushing a exit button to exit my java swing program

    Quote Originally Posted by Tolls View Post
    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.
    Oh, OK , I 've got it , thank you for teaching me that~

Similar Threads

  1. Netbeans GUI EXIT button?
    By blackz in forum AWT / Swing
    Replies: 4
    Last Post: 04-04-2012, 04:28 AM
  2. Could not find the main class: java. Program will exit
    By thewonkits in forum New To Java
    Replies: 3
    Last Post: 01-11-2012, 11:56 AM
  3. Replies: 4
    Last Post: 08-22-2011, 03:42 PM
  4. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 PM
  5. Adding EXIT button on MIDlet form
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •