保存文件/打开文件对话框,使用Swing& Netbeans GUI编辑器 [英] Save file/open file dialog box, using Swing & Netbeans GUI editor

查看:250
本文介绍了保存文件/打开文件对话框,使用Swing& Netbeans GUI编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者。我正在使用GUI编辑器在netbeans 7(.3)IDE中创建一个简单的文本编辑器。我面临的主要问题是我无法保存/打开文件。我创建了保存按钮。当我删除文件选择器时,它作为一个普通的打开文件对话框嵌入在java窗口中,根本没有任何功能。我还尝试在单击保存按钮时(在源视图中)创建新的jFileChooser,但它不起作用。

I am a beginner to Java. I am making a simple text editor in netbeans 7(.3) IDE, using its GUI editor. The main problem I face in it is that I can't make it to save/open the file. I have created the "save" button. When I drop the file chooser, it comes as a normal open file dialog box embedded in the java window with no functionality at all. I have also tried creating a new jFileChooser when the save button is clicked (in the Source view), but it doesn't work.

简而言之,我需要一个简单的打开/保存对话框。按下保存按钮后,将打开保存对话框,并将文件保存在用户选择的任何名称和.rtf或.txt扩展名的任何位置。 (PS:是否可以在Java中保存.docx或.doc中的文件?)

按下打开btn时,它会打开.rtf或.txt中的文件(同样,是可以通过文件选择器在Java中打开.docx或.doc吗?

In a nutshell, I need a simple open/save dialog box. When the "Save" button is pressed, the save dialog box opens and saves the file wherever the user chooses with whatever name and .rtf or .txt extension. (P.S.: is it possible to save a file in .docx or .doc in Java?)
When the "Open" btn is pressed, it opens a file in .rtf or .txt (again, is it possible to open .docx or .doc in Java?) through the file chooser.

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    JFileChooser saveFile = new JFileChooser();
    if saveFile.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION {
        File xyz = saveFile.getSelectedFile();
    }
}

代码在这里:https://docs.google.com/file/d/0B766zz1iJ1LRN2lGRjNtM29vN2M/edit?usp=sharing

推荐答案

我创建了一个示例UI,显示了保存和打开文件对话框。单击保存按钮打开保存对话框,然后单击打开按钮打开文件对话框。

I have created a sample UI which shows the save and open file dialog. Click on save button to open save dialog and click on open button to open file dialog.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FileChooserEx {
    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new FileChooserEx().createUI();
            }
        };

        EventQueue.invokeLater(r);
    }

    private void createUI() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JButton saveBtn = new JButton("Save");
        JButton openBtn = new JButton("Open");

        saveBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser saveFile = new JFileChooser();
                saveFile.showSaveDialog(null);
            }
        });

        openBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser openFile = new JFileChooser();
                openFile.showOpenDialog(null);
            }
        });

        frame.add(new JLabel("File Chooser"), BorderLayout.NORTH);
        frame.add(saveBtn, BorderLayout.CENTER);
        frame.add(openBtn, BorderLayout.SOUTH);
        frame.setTitle("File Chooser");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

这篇关于保存文件/打开文件对话框,使用Swing& Netbeans GUI编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆