簡體   English   中英

Java新手保存和加載:記事本

[英]Java Novice Saving and loading: Notepad

感謝您花時間閱讀本文,我會明白這一點。 我遇到的問題是,由於某種原因,我的練習記事本沒有按照我的意願去做。 它可以正常工作,但問題是我的save方法只加載而不是保存。 它們都加載,我想要其中一個保存。 我不確定我是否在某個地方犯了錯誤或者我做錯了但是這里是代碼:

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;

public class Notepad extends JFrame {

    ImageIcon im = new ImageIcon("Water.bmp");

    private JFrame f = new JFrame("Notepad"); // Create Frame
    private JPanel pnlNorth = new JPanel(); //North Quadrant
    private JPanel pnlSouth = new JPanel(); //South Quadrant


    private JMenuBar mb = new JMenuBar(); // Menu Bar
    private JMenu mnuFile = new JMenu("File"); // File Entry of Menu Bar
    private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit Sub item

    private JMenuItem mnuItemSave = new JMenuItem("Save");

    private JMenuItem mnuItemOpen = new JMenuItem("Open");

    private JMenu mnuHelp = new JMenu("Help"); //Help Menu Entry
    private JMenuItem mnuItemAbout = new JMenuItem("About"); //About Entry

    private JTextArea Ta = new JTextArea(10, 30);


    int fileSave;
    int fileOpen;
    JFileChooser sFile;
    JFileChooser oFile;


    public Notepad(){



        pnlSouth.add(Ta);

        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(pnlNorth, BorderLayout.NORTH);
        f.getContentPane().add(pnlSouth,BorderLayout.SOUTH );
        //f.add(button);
f.addWindowListener(new ListenCloseWdw());

f.setJMenuBar(mb);

mnuItemQuit.addActionListener(new ListenMenuQuit());

mnuItemSave.addActionListener(new ListenMenuSave());

mnuItemOpen.addActionListener(new ListenMenuOpen());

//Build Menus
mnuFile.add(mnuItemQuit);//Create Quit Line
mnuFile.add(mnuItemSave);
mnuFile.add(mnuItemOpen);
mnuHelp.add(mnuItemAbout); //Create About Line
mb.add(mnuFile);        // Add Menu Items to form
mb.add(mnuHelp);




    }




    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);

        }
    }

    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }   
    }

    public class ListenMenuSave implements ActionListener{
        public void actionPerformed(ActionEvent e){
            sFile();

            if (fileSave == JFileChooser.APPROVE_OPTION) {
                Ta.setText("");
                try {
                    Scanner scan = new Scanner(new FileReader(sFile.getSelectedFile().getPath()));
                    while(scan.hasNext())
                        Ta.append(scan.nextLine() + "\n");
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }


        }
    }

    public class ListenMenuOpen implements ActionListener{
        public void actionPerformed(ActionEvent e){
            oFile();
            if (fileOpen == JFileChooser.APPROVE_OPTION) {
                Ta.setText("");
                try {
                    Scanner scan = new Scanner(new FileReader(oFile.getSelectedFile().getPath()));
                    while(scan.hasNext())
                        Ta.append(scan.nextLine() + "\n");
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }

        }
    }

    public void sFile(){
        JFileChooser save = new JFileChooser();// Open up file chooser
        int option = save.showSaveDialog(this);
        fileSave= option;
        sFile = save;
    }

    public void oFile(){
        JFileChooser open = new JFileChooser();// Open up file chooser
        int option = open.showOpenDialog(this);
        fileOpen= option;
        oFile = open;

    }

    public void launchFrame(){
        //Display Frame
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack(); // Adjusting panel to components for Display
        f.setVisible(true);
    }
    public static void main(String args[]){
        Notepad gui = new Notepad();
        gui.launchFrame();
    }
}

您正在ListenMenuSave ActionListenermnuItemSave JMenuItem使用Scanner ,該ListenMenuSave正在嘗試讀取保存JFileChooser中的所選文件。

使用JTextComponent#write來編寫JTextArea的內容。

PrintWriter writer = new PrintWriter(sFile.getSelectedFile());
ta.write(writer);

請記住在finally塊中調用writer.close()以將任何未寫入的緩沖數據寫入磁盤。

你在兩個聽眾中做的幾乎完全相同。

Scanner scan = new Scanner(new FileReader(oFile.getSelectedFile().getPath()));
while(scan.hasNext())
  Ta.append(scan.nextLine() + "\n");

您掃描給定的文件並將其放入文本字​​段。 這是您想要加載的行為,但是對於保存,您將要從文本字段讀取行並將其寫入新文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM