Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

java - How to save data from JEditorPane?

So first of all I want to use my JEditorPane as a "note field" and I want it to save the text, so I still can see it the next time I either reload the data or when I next open the window (frame).

I tried this code, but it wont function and I don't get any errors? And at the same time the buttons texts "save" and "load" disappears from my frame (visually) when I run it?

Code:

//imports

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import numerologi.common.RessourceManager;
import numerologi.common.SaveData;

@SuppressWarnings("serial")
public class Noter extends JFrame {

//variables
    private JPanel contentPane;
    private JMenuBar menuBar;
    private JMenu ForsideMenu;
    private JMenuItem menuopret, menuKliste, menuNF, menuNliste;
    private JLabel LabelNoter;
    private JButton BButton,BSave,BLoad;
    private JEditorPane editorPane;
    
// classes/sites
    private static Klient Klientside = new Klient();
    
    
    
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Noter frame = new Noter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace(); }
}
});
}

public Noter() {
    
    initComponents();
    createEvents();
        
        
}

////////////////////////////////////////////////////////////////////////////////
//This method contains all codes for creating and initializing components.  //
//////////////////////////////////////////////////////////////////////////////
private void initComponents() {
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1000, 600);
    setIconImage(Toolkit.getDefaultToolkit().getImage(Program.class.getResource("/numerologi/resources/Diamond_512.png")));
    setTitle("Numerologi program");
    
    contentPane = new JPanel();
    contentPane.setBackground(SystemColor.menu);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    menuBar = new JMenuBar();
    menuBar.setBounds(5, 5, 976, 22);
    contentPane.add(menuBar);
    
    ForsideMenu = new JMenu("Menu");
    ForsideMenu.setHorizontalAlignment(SwingConstants.CENTER);
    menuBar.add(ForsideMenu);
    
    menuopret = new JMenuItem("Opret ny klient");
    menuopret.setBackground(SystemColor.inactiveCaption);
    
    ForsideMenu.add(menuopret);
    
    menuKliste = new JMenuItem("Klientliste");
    menuKliste.setBackground(SystemColor.inactiveCaption);
    ForsideMenu.add(menuKliste);
    
    ForsideMenu.addSeparator();
    
    menuNF = new JMenuItem("Navne forandring");
    menuNF.setBackground(SystemColor.inactiveCaption);
    ForsideMenu.add(menuNF);
    
    menuNliste = new JMenuItem("Navneliste");
    menuNliste.setBackground(SystemColor.inactiveCaption);
    ForsideMenu.add(menuNliste);
    
    LabelNoter = new JLabel("Noter");
    LabelNoter.setBounds(468, 32, 49, 25);
    LabelNoter.setFont(new Font("Tahoma", Font.PLAIN, 20));
    contentPane.add(LabelNoter);
    
    BButton = new JButton("Tilbage");
    BButton.setBounds(20, 38, 85, 21);
    contentPane.add(BButton);
    
    BSave = new JButton("Save");
    BSave.setBounds(453, 430, 85, 21);
    contentPane.add(BSave);
    
    editorPane = new JEditorPane();
    editorPane.setBounds(20, 114, 945, 293);
    contentPane.add(editorPane);
    
    BLoad = new JButton("Load");
    BLoad.setBounds(553, 430, 85, 21);
    contentPane.add(BLoad);
    
    
}
        
//////////////////////////////////////////////////////////////////////////////
//This method contains all codes for creating events.                      //
////////////////////////////////////////////////////////////////////////////
private void createEvents() {
    
    BButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    Klientside.show();
    dispose(); }
});     
    
    
    BSave.setAction(event); {
        SaveData data = new SaveData();
        data.name = editorPane.getText();
        try {
        RessourceManager.save(data, "1.save");  
        }
        catch (Exception e) {
            System.out.println("Couldn't save data: "+ e.getMessage()); }
        }
    
     BLoad.setAction(event); {
        try {
            SaveData data = (SaveData) RessourceManager.load("1.save");
            editorPane.setText(data.name);} 
        catch (Exception e) {
            System.out.println("couldn't load saved data: "+ e.getMessage()); }
        } 
    }

}

SaveData Class:

public class SaveData implements java.io.Serializable {

    private static final long serialVersionUID =1L;
    
    public String name;
    public int hp;
    
}

RessourceManager Class:

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;


public class RessourceManager {

    public static void save (Serializable data, String fileName) throws Exception {
        try (ObjectOutputStream oos = new ObjectOutputStream (Files.newOutputStream(Paths.get(fileName)))) {
                oos.writeObject(data); }
    }
    public static Object load(String fileName) throws Exception {
        try (ObjectInputStream ois = new ObjectInputStream (Files.newInputStream(Paths.get(fileName)))) {
            return ois.readObject(); }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...