使用filechooser加载文本文件

时间:2015-11-26 15:56:14

标签: java

文件菜单应该能够保存或打开现有的二进制文本文件进行文本编辑,现在我想使用filechooser如何加载现有的文本文件以将该文件的文本传递给我的jlabel使用文件选择器。这是我到目前为止所做的事情。

  

Blockquote / *

package mp4Test;

public class Lab4test extends JFrame implements ActionListener {

private JMenu filemenu;
private JMenu editmenu;
private JMenu aboutmenu;
private JMenuBar menubar;
private JLabel jlabel;
private JMenuItem openitem;
private JMenuItem exititem;
private JMenuItem saveitem;
private JMenuItem aboutme;
private JMenuItem abouttext;
private JMenuItem edittext;
private JMenuItem arial;
private JMenuItem setsize;
private JMenu setdesign;
private JMenu setcolor;
private JMenu submchange;
private JMenuItem tnr;
private JMenuItem cournew;
private Font fontpass;
private Font afont;
private Font tfont;
private Font cfont;
private Font defaultfont;
private float fontsize = 15;
private JMenuItem red;
private JMenuItem blue;
private JMenuItem green;
private JMenuItem yellow;
private JMenuItem orange;
private JMenuItem white;
private JMenuItem black;
private JMenuItem italic;
private JMenuItem bold;
private JMenuItem itandbold;
private String fontpassname = "Monospaced";
private String savedText;
private int boldint = 0;
private JFileChooser fc;

public Lab4test() {

    defaultfont = new Font("Monospaced", Font.PLAIN, 15);

    fc = new JFileChooser("c:");
    menubar = new JMenuBar();
    filemenu = new JMenu("File");
    editmenu = new JMenu("Edit");
    aboutmenu = new JMenu("About");
    setdesign = new JMenu("About");
    saveitem = new JMenuItem("Save");
    italic = new JMenuItem("Italic");
    itandbold = new JMenuItem("Italic Bold");
    bold = new JMenuItem("Bold");
    jlabel = new JLabel("THIS IS PRES", (int) CENTER_ALIGNMENT);
    jlabel.setFont(defaultfont);
    openitem = new JMenuItem("Open");
    exititem = new JMenuItem("Exit");
    setsize = new JMenuItem("SetFontSize");
    setcolor = new JMenu("Set Color");
    setdesign = new JMenu("Set Design");
    aboutme = new JMenuItem("Programmer");
    abouttext = new JMenuItem("Text");
    edittext = new JMenuItem("EditText");
    submchange = new JMenu("Change Font");
    arial = new JMenuItem("Arial");
    tnr = new JMenuItem("Courier New");
    cournew = new JMenuItem("Times New Roman");
    //COLORS
    red = new JMenuItem("Red");
    green = new JMenuItem("Green");
    blue = new JMenuItem("Blue");
    yellow = new JMenuItem("Yellow");
    orange = new JMenuItem("Orange");
    white = new JMenuItem("White");
    black = new JMenuItem("Black");



    setJMenuBar(menubar);
    //MENUBAR
    menubar.add(filemenu);
    menubar.add(editmenu);
    menubar.add(aboutmenu);
    add(jlabel);
    //FILEMENU ITEMS
    filemenu.add(openitem);
    filemenu.add(saveitem);
    filemenu.add(exititem);
    //EDITMENU ITEMS
    editmenu.add(edittext);
    editmenu.add(setsize);
    editmenu.add(setcolor);
    editmenu.add(setdesign);
    editmenu.addSeparator();
    editmenu.add(submchange);
    //setfonts
    submchange.add(arial);
    submchange.add(tnr);
    submchange.add(cournew);
    //setcolor
    setcolor.add(red);
    setcolor.add(blue);
    setcolor.add(green);
    setcolor.add(yellow);
    setcolor.add(orange);
    setcolor.add(white);
    setcolor.add(black);
    //setdesign
    setdesign.add(italic);
    setdesign.add(itandbold);
    setdesign.add(bold);
    //ABOUTMENU ITEMS
    aboutmenu.add(aboutme);
    aboutmenu.add(abouttext);
    //ACTIONLISTENER EDITMENU
    edittext.addActionListener(this);
    arial.addActionListener(this);
    tnr.addActionListener(this);
    cournew.addActionListener(this);
    setsize.addActionListener(this);
    red.addActionListener(this);
    blue.addActionListener(this);
    green.addActionListener(this);
    black.addActionListener(this);
    white.addActionListener(this);
    orange.addActionListener(this);
    yellow.addActionListener(this);
    italic.addActionListener(this);
    itandbold.addActionListener(this);
    bold.addActionListener(this);
    //ACTIONLISTENER FILEMENU
    openitem.addActionListener(this);
    exititem.addActionListener(this);
    saveitem.addActionListener(this);
    //ACTIONLISTENER ABOUTMENU
    aboutme.addActionListener(this);
    abouttext.addActionListener(this);

    savedText = jlabel.getText();
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == aboutme) {
        JOptionPane.showMessageDialog(jlabel, "PRESMELITO R VILLANUEVA");
    } else if (e.getSource() == abouttext) {
        JOptionPane.showMessageDialog(jlabel, "TEXT EDITOR NI PRES\nVERSION 2.2");
    } else if (e.getSource() == openitem) {
        fc.showOpenDialog(jlabel);
    } else if (e.getSource() == saveitem){
        try {
            //SAVE
            FileOutputStream fos = new FileOutputStream("c:\\file.txt");
            DataOutputStream dos = new DataOutputStream(fos);
            dos.writeBytes(savedText);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Lab4test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Lab4test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    else if (e.getSource() == exititem) {
        System.exit(1);
    } else if (e.getSource() == edittext) {
        jlabel.setText(JOptionPane.showInputDialog(jlabel, "NEW TEXT"));
        savedText = jlabel.getText();

    } else if (e.getSource() == cournew) {
        cfont = new Font("Times New Roman", boldint, (int) fontsize);
        jlabel.setFont(cfont);
        fontpassname = "Times New Roman";
    } else if (e.getSource() == tnr) {
        tfont = new Font("Courier New", boldint, (int) fontsize);
        jlabel.setFont(tfont);
        fontpassname = "Courier New";
    } else if (e.getSource() == arial) {

        afont = new Font("Arial", boldint, (int) fontsize);
        jlabel.setFont(afont);
        fontpassname = "Arial";
    } else if (e.getSource() == setsize) {
        fontsize = Integer.parseInt(JOptionPane.showInputDialog(jlabel, "Set Size"));
        jlabel.setFont(jlabel.getFont().deriveFont(fontsize));
    } else if (e.getSource() == red) {
        jlabel.setForeground(Color.red);
    } else if (e.getSource() == orange) {
        jlabel.setForeground(Color.orange);
    } else if (e.getSource() == yellow) {
        jlabel.setForeground(Color.yellow);
    } else if (e.getSource() == green) {
        jlabel.setForeground(Color.green);
    } else if (e.getSource() == blue) {
        jlabel.setForeground(Color.blue);
    } else if (e.getSource() == black) {
        jlabel.setForeground(Color.black);
    } else if (e.getSource() == white) {
        jlabel.setForeground(Color.white);
    } else if (e.getSource() == white) {
        jlabel.setForeground(Color.white);
    } else if (e.getSource() == bold) {
        fontpass = new Font(fontpassname, 1, (int) fontsize);
        jlabel.setFont(fontpass);
        boldint = 1;
    } else if (e.getSource() == italic) {
        fontpass = new Font(fontpassname, 2, (int) fontsize);
        jlabel.setFont(fontpass);
        boldint = 2;
    } else if (e.getSource() == itandbold) {
        fontpass = new Font(fontpassname, 3, (int) fontsize);
        jlabel.setFont(fontpass);
        boldint = 3;
    }

    }

  }

1 个答案:

答案 0 :(得分:0)

使用JFileChooser:

} else if (e.getSource() == openitem) {
    int answer = fc.showOpenDialog(null); // (JFrame.this) when in JFrame
    if (answer == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        byte[] content = Files.readAllBytes(file.toPath());
        String text = new String(content); // Using default encoding
        jLabel1.setText(text);
    }

你可能想在JScrollPane中使用JTextPane而不是JLabel。