记事本程序,JMenu,JPanel的建议

时间:2014-05-08 12:06:08

标签: java jpanel jtextarea jmenu

我被困在构建记事本中。问题是,当用户按下清除按钮时,程序会询问用户用户是否确定要清除,如果是,则程序会询问用户是否要在清除之前保存文件,如果是,则保存文件框将打开,但如果否,那么我希望程序清除文本,但是当我按NO时,它与YES按钮相同。有什么建议?请帮忙。

public class NotePad extends JFrame implements ActionListener {

    private JTextArea txtArea;
    private JMenuBar mnuBar;
    private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp, fontSize;
    private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, font10, font12, font14, font16, font18, font20, clear, copy, cut, paste, abtNotepad;

    public NotePad() {

        setTitle("NOTEPAD");
        setSize(800, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        txtArea = new JTextArea();
        JScrollPane scrollArea = new JScrollPane(txtArea);

        //MenyBar
        mnuBar = new JMenuBar();

        mnyFile = new JMenu("File");
        mnyFormat = new JMenu("Format");
        mnyEdit = new JMenu("Edit");
        mnyHelp = new JMenu("Help");
        fontSize = new JMenu("Font Size");

        openFile = new JMenuItem("Open");
        openFile.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_O,
                java.awt.Event.CTRL_MASK));

        saveFile = new JMenuItem("Save");
        saveFile.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_S,
                java.awt.Event.CTRL_MASK));

        exit = new JMenuItem("Exit");
        exit.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_E,
                java.awt.Event.CTRL_MASK));

        textWrap = new JMenuItem("Text Wrap");
        noTextWrap = new JMenuItem("No Text Wrap");
        font10 = new JMenuItem("10");
        font12 = new JMenuItem("12");
        font14 = new JMenuItem("14");
        font16 = new JMenuItem("16");
        font18 = new JMenuItem("18");
        font20 = new JMenuItem("20");
        clear = new JMenuItem("Clear");
        abtNotepad = new JMenuItem("About Notepad");

        copy = new JMenuItem(txtArea.getActionMap().get(DefaultEditorKit.copyAction));
        copy.setText("Copy");
        copy.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_C,
                java.awt.Event.CTRL_MASK));

        cut = new JMenuItem(txtArea.getActionMap().get(DefaultEditorKit.cutAction));
        cut.setText("Cut");
        cut.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_X,
                java.awt.Event.CTRL_MASK));

        paste = new JMenuItem(txtArea.getActionMap().get(DefaultEditorKit.pasteAction));
        paste.setText("Paste");
        paste.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_V,
                java.awt.Event.CTRL_MASK));

        add(mnuBar);

        mnuBar.add(mnyFile);
        mnuBar.add(mnyFormat);
        mnuBar.add(mnyEdit);
        mnuBar.add(mnyHelp);

        mnyFile.add(openFile);
        mnyFile.add(saveFile);
        mnyFile.add(exit);

        mnyFormat.add(textWrap);
        mnyFormat.add(noTextWrap);
        mnyFormat.add(fontSize);

        mnyEdit.add(clear);
        mnyEdit.add(copy);
        mnyEdit.add(cut);
        mnyEdit.add(paste);

        mnyHelp.add(abtNotepad);

        fontSize.add(font10);
        fontSize.add(font12);
        fontSize.add(font14);
        fontSize.add(font16);
        fontSize.add(font18);
        fontSize.add(font20);

        exit.addActionListener(this);
        openFile.addActionListener(this);
        saveFile.addActionListener(this);
        clear.addActionListener(this);
        abtNotepad.addActionListener(this);
        fontSize.addActionListener(this);
        font10.addActionListener(this);
        font12.addActionListener(this);
        font14.addActionListener(this);
        font16.addActionListener(this);
        font18.addActionListener(this);
        font20.addActionListener(this);
        textWrap.addActionListener(this);
        noTextWrap.addActionListener(this);

        setJMenuBar(mnuBar);
        add(scrollArea);
        setVisible(true);

    }

//Main
    public static void main(String[] args) {

        new NotePad();

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == this.exit) {

            int svar = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to quit?", "Exit", JOptionPane.YES_NO_OPTION);
            if (svar == JOptionPane.YES_OPTION) {
                this.dispose();
            }
        } else if (e.getSource() == this.openFile) {
            JFileChooser open = new JFileChooser();
            int option = open.showOpenDialog(this);

            if (option == JFileChooser.APPROVE_OPTION) {
                this.txtArea.setText("");

                try {
                    Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
                    while (scan.hasNext()) {
                        this.txtArea.append(scan.nextLine() + "\n");
                    }

                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } else if (e.getSource() == this.saveFile) {
            JFileChooser save = new JFileChooser();
            int option = save.showSaveDialog(this);

            if (option == JFileChooser.APPROVE_OPTION) {

                try {

                    BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));

                    out.write(this.txtArea.getText());

                    out.close();

                } catch (Exception ex) {

                    System.out.println(ex.getMessage());

                }

            }
        } else if (e.getSource() == this.clear) {

            int reply = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to clear the text ?", "Clear", JOptionPane.YES_NO_OPTION);

            if (reply == JOptionPane.YES_OPTION) {
                JOptionPane.showConfirmDialog(null,
                        "Do you want to save your document?", "Save", JOptionPane.YES_NO_OPTION);

                if (reply == JOptionPane.YES_OPTION) {

                    JFileChooser lagre = new JFileChooser();
                    int options = lagre.showSaveDialog(this);

                    if (options == JFileChooser.APPROVE_OPTION) {

                        try {

                            BufferedWriter ut = new BufferedWriter(new FileWriter(lagre.getSelectedFile().getPath()));

                            ut.write(this.txtArea.getText());

                            ut.close();

                        } catch (Exception x) {

                            System.out.println(x.getMessage());

                        }

                    }

                }
            } else if (e.getSource() == this.abtNotepad) {
                JOptionPane.showMessageDialog(null, "This Application is brought to you by a 1. Year student from Norwegian School of Information Technology."
                        + "\n Hope you enjoy it.", "About Notepad", JOptionPane.INFORMATION_MESSAGE);
            } else if (e.getSource() == this.font10) {
                Font str10 = new Font("Times New Roman", Font.BOLD, 10);
                txtArea.setFont(str10);
            } else if (e.getSource() == this.font12) {
                Font str12 = new Font("Times New Roman", Font.BOLD, 12);
                txtArea.setFont(str12);
            } else if (e.getSource() == this.font14) {
                Font str14 = new Font("Times New Roman", Font.BOLD, 14);
                txtArea.setFont(str14);
            } else if (e.getSource() == this.font16) {
                Font str16 = new Font("Times New Roman", Font.BOLD, 16);
                txtArea.setFont(str16);
            } else if (e.getSource() == this.font18) {
                Font str18 = new Font("Times New Roman", Font.BOLD, 18);
                txtArea.setFont(str18);
            } else if (e.getSource() == this.font20) {
                Font str20 = new Font("Times New Roman", Font.BOLD, 20);
                txtArea.setFont(str20);
            } else if (e.getSource() == this.textWrap) {
                txtArea.setLineWrap(true);
            } else if (e.getSource() == this.noTextWrap) {
                txtArea.setLineWrap(false);
            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

JOptionPane.showConfirmDialog(null,
"Do you want to save your document?", "Save", OptionPane.YES_NO_OPTION);

此行是您的问题。在if语句中。你实际上并没有再次设置“reply”变量,而是仍在查看第一个JOptionPane中的“reply”变量。将此设置为...

reply = JOptionPane.showConfirmDialog(null,"Do you want to save your document?", 
"Save", OptionPane.YES_NO_OPTION);

就像你在第一次迭代中所做的那样。