有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java首次使用JFileChooser需要帮助

坦率地说,我正在寻求有关如何实际使用它的帮助。我们刚刚开始在我的课程中使用这个,但我们的新老师不教,我真的很难做到这一点。因此,我使用windows builder设置了一个基本的JFrame,目标是能够以字符串形式打开文本文件并将其放入文本空间,然后能够在文本中找到字符串并对其进行更改。我将粘贴下面的代码,如果有人可以帮助解释如何执行此操作,我将非常感激,谢谢。:)

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.TextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.io.File;
import javax.swing.filechooser.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class task1 extends JFrame {

    private JPanel contentPane;
    private JTextField findTxtBox;
    private JButton findBtn;
    private JTextField replaceTxtBox;
    private JTextField fileTxtBox;
    private JButton openBtn;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    task1 frame = new task1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public task1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 312);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        TextArea textArea = new TextArea();
        textArea.setBounds(10, 45, 380, 160);
        contentPane.add(textArea);

        findTxtBox = new JTextField();
        findTxtBox.setBounds(80, 211, 236, 20);
        contentPane.add(findTxtBox);
        findTxtBox.setColumns(10);

        findBtn = new JButton("Find");
        findBtn.setBounds(326, 210, 89, 23);
        contentPane.add(findBtn);

        JButton btnReplace = new JButton(" Replace");
        btnReplace.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnReplace.setBounds(326, 240, 89, 23);
        contentPane.add(btnReplace);

        replaceTxtBox = new JTextField();
        replaceTxtBox.setColumns(10);
        replaceTxtBox.setBounds(80, 242, 236, 20);
        contentPane.add(replaceTxtBox);

        fileTxtBox = new JTextField();
        fileTxtBox.setColumns(10);
        fileTxtBox.setBounds(80, 11, 236, 20);
        contentPane.add(fileTxtBox);

        final JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
        fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());

        openBtn = new JButton("Open File");
        openBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                fc.showOpenDialog(null);
            }
        });
        openBtn.setBounds(326, 10, 89, 23);
        contentPane.add(openBtn);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    the object is to be able to open a text file as a string and put it into the text space, and then be able to find strings in the text and change them.

    这不仅仅是使用文件选择器。我建议您首先阅读Swing教程中关于How to Use File Choosers的部分,以获得一个工作示例

    文件选择器仅用于获取文件名,而不是读取文件。因此,接下来我建议您使用JTextArea(不是文本区域)来显示您读取的文件中的文本。您可以使用JTextArea的read(...)方法来实现这一点

    所有文本组件都有一个getText()方法,您可以使用获取文本。然后,您可以在字符串中搜索所需内容,并使用JTextArea的replace()方法替换文本

    最后,您不应该使用setBounds()方法来设置组件的大小/位置。你应该使用Layout Managers,让他们完成他们的工作。Swing教程还有一节介绍如何使用布局管理器