java - JFrame FileChooser 获取文件目录

标签 java swing filechooser

我正在做作业。基本上任务已经完成,但我试图通过添加 GUI 来使其更好。

但是我在 FileChooser 上遇到了一些问题,因为我不太明白它是如何工作的。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class CaesarCipherGui extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
private static JButton Encrypt  = new JButton("Encrypttion");
private static JButton Decrypt  = new JButton("Decrypttion");
private static JPanel  panel    = new JPanel();

public static void main(String[] args) {
    new CaesarCipherGui();
}
private void addComponent(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
    GridBagConstraints gc = new GridBagConstraints();
    gc.gridx = x;
    gc.gridy = y;
    gc.gridwidth = width;
    gc.gridheight = height;
    gc.weightx = 100.0;
    gc.weighty = 100.0;
    gc.insets = new Insets(5, 5, 5, 5);
    gc.anchor = align;
    gc.fill = GridBagConstraints.NONE;
    p.add(c, gc);
}

public CaesarCipherGui() {
    this.setSize(310, 192);
    this.setTitle("Caesar Cipher");
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setResizable(false);
    panel.setLayout(new GridBagLayout());

    addComponent(panel, Encrypt, 1, 4, 1, 1, GridBagConstraints.WEST);
    Encrypt.addActionListener(this);

    addComponent(panel, Decrypt, 1, 4, 1, 1, GridBagConstraints.EAST);
    Decrypt.addActionListener(this);


    this.add(panel);
    this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == Encrypt){
            Scanner console = new Scanner(System.in);
            System.out.print("Input file: ");
            String inputFileName = console.next();
            System.out.print("Output file: ");
            String outputFileName = console.next();

            try{ 
                FileReader reader = new FileReader("C:/"+inputFileName+".txt");
                Scanner in = new Scanner(reader);
                PrintWriter out = new PrintWriter("C:/"+outputFileName+".txt");

                    while (in.hasNextLine()){
                        String line = in.nextLine();
                        String outPutText = "";
                        int key = 3;

                        for (int i = 0; i < line.length(); i++) {
                            char c = line.charAt(i);
                            if (c >= 'a' && c <= 'z') {
                                c += key % 26;
                                if (c < 'a')
                                    c += 26;
                                if (c > 'z')
                                    c -= 26;
                            }
                            if (c >= 'A' && c <= 'Z') {
                                c += key % 26;
                                if (c < 'A')
                                    c += 26;
                                if (c > 'Z')
                                    c -= 26;
                            }
                            if (c == ' '){
                                c = '#';
                            }
                            outPutText += c;
                        }
                        out.println(outPutText);

                    }
                    out.close();
                    }
            catch (IOException exception){
                System.out.println("Error processing file:" + exception);
            }
    }
    if (e.getSource() == Decrypt){
        Scanner console = new Scanner(System.in);
        System.out.print("Input file: ");
        String inputFileName = console.next();
        System.out.print("Output file: ");
        String outputFileName = console.next();

        try{ 
            FileReader reader = new FileReader("C:/"+inputFileName+".txt");
            Scanner in = new Scanner(reader);
            PrintWriter out = new PrintWriter("C:/"+outputFileName+".txt");

                while (in.hasNextLine()){
                    String line = in.nextLine();
                    String outPutText = "";
                    int key = -3;

                    for (int i = 0; i < line.length(); i++) {
                        char c = line.charAt(i);
                        if (c >= 'a' && c <= 'z') {
                            c += key % 26;
                            if (c < 'a')
                                c += 26;
                            if (c > 'z')
                                c -= 26;
                        }
                        if (c >= 'A' && c <= 'Z') {
                            c += key % 26;
                            if (c < 'A')
                                c += 26;
                            if (c > 'Z')
                                c -= 26;
                        }
                        if (c == '#'){
                            c = ' ';
                        }
                        outPutText += c;
                    }
                    out.println(outPutText);

                }
                out.close();
                }
        catch (IOException exception){
            System.out.println("Error processing file:" + exception);
        }
}
}
}

由于上面的代码是我的作业,但我希望为输入文件名添加文件选择器并为输出文件名添加保存文件,我该怎么做?或者您可以只修改需要更改的部分并显示给我。

最佳答案

不,您向按钮添加一个 Action 监听器并定义文件选择器:

cmdSearch = new AbstractAction("Search", null) {
    public void actionPerformed(ActionEvent evt) {
        final JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        txtSearch.setText( (fc.showOpenDialog(YOURCLASSNAME.this) == JFileChooser.APPROVE_OPTION) ? fc.getSelectedFile().toString() : txtSearch.getText() );                                    
    }
};

关于java - JFrame FileChooser 获取文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13515417/

相关文章:

Java JCombobox 和 JButton 问题

django - 如何在 Django 中使用文件选择器?

c - GTK中静态编译如何处理FileChooser

java - 如何检索未知子项中未知键的值?

java - 如何将 JAR 文件添加到 Eclipse 中的 web-inf/lib 文件夹?

java - 制作 Swing Logo 测验

r - 为什么 select.files() 不在当前工作目录中打开?

java - java中如何比较Json responedata和sql数据

java - 在 Java 中将 png 文件作为二维整数数组输入

java - JFileChooser 选择空文件