Frage

I am relatively new to java(started coding about a month ago),and after doing basic examples of gui programs,i decided to create a very simple text editor(something like Notepad). I've made a menu bar,with menu File,with menu items Open,Save,Exit.Everything works perfectly but the Exit menu item. So here is the whole code.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileNameExtensionFilter;


public class Editor {
    JTextArea TextAr = new JTextArea();
    JFileChooser chooser = new JFileChooser();
    private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Editor window = new Editor();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Editor() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    TextAr.setBounds(10, 11, 414, 218);
    frame.getContentPane().add(TextAr);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmOpen = new JMenuItem("Open");
    mnFile.add(mntmOpen);
    performer performance = new performer();
    mntmOpen.addActionListener(performance);

    JMenuItem mntmSaveAs = new JMenuItem("Save as");
    mnFile.add(mntmSaveAs);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mnFile.add(mntmExit);
    mntmSaveAs.addActionListener(performance);
}
public class performer implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(event.getActionCommand().equals("Save as")){
            chooser.setDialogTitle("Choose where to save the file");
            FileNameExtensionFilter txtfilter = new FileNameExtensionFilter("Text File", "txt");
            chooser.addChoosableFileFilter(txtfilter);
            int val = chooser.showSaveDialog(null);
            if(val == JFileChooser.APPROVE_OPTION){
                try(FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt")){
                    fw.write(TextAr.getText());
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }else if (event.getActionCommand().equals("Open")){
            chooser.setDialogTitle("Choose a text file");
            FileNameExtensionFilter textfilter = new FileNameExtensionFilter("Text Files", "txt");
            chooser.addChoosableFileFilter(textfilter);
            chooser.setAcceptAllFileFilterUsed(false);
            int returnval = chooser.showOpenDialog(null);
            if(returnval == JFileChooser.APPROVE_OPTION){
                try(BufferedReader a = new BufferedReader(new FileReader(chooser.getSelectedFile().getPath()))){
                    StringBuilder str = new StringBuilder();
                    String line;
                    while((line = a.readLine()) != null){
                        str.append(line);
                        str.append("\n");
                    }
                    TextAr.append(str.toString());
                }catch (IOException e){
                    e.printStackTrace();
                }


            }else if(event.getActionCommand().equals("Exit")){
                System.exit(0);
            }


        }

    }
}
}

I have to mention that I use the ECLIPSE IDE with WindowBuilder.I tried to fix the problem by repositioning the

     else if(event.getActionCommand().equals("Exit")){
        System.exit(0);
     }

to different locations around the code.

War es hilfreich?

Lösung

On your mntmExit you should attach action listener in order to get action.

You can do that on several ways, but here is the one:

/** exit menu event */
mntmExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        System.exit(0); //close the application
    }
});

Andere Tipps

You don't have

mntmExit.addActionListener(performance);

anywhere.

Edit: that was problem one. There appears to be another one.

The other problem is the last else if statement currently is in the wrong place. You need to move it out one more block.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top