2

私は仕事をしていました。基本的には割り当ては完了ですが、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);
        }
}
}
}

上記のコードは私の割り当てですが、入力ファイル名のファイルチューザーを追加し、出力ファイル名のファイルを保存したいのですが、どうすればよいですか?または、変更が必要な部分を変更して表示することもできます。

4

2 に答える 2

1

公式ドキュメントから始めるべきだと思います。の使用JFileChooserは非常に簡単です。次のことを行う必要があります。

  • 実行内容に応じて、開くダイアログまたは保存ダイアログを表示します
  • show関数の戻り値をチェックして、ユーザーがファイルを効果的に選択したことを確認します
  • FilethroughgetSelectedFile()メソッドを取得する
  • オプションでカスタムFileFilterを使用して、ユーザーが必要なファイルタイプだけを表示できるようにします
于 2012-11-22T15:08:01.077 に答える
1

いいえ、ボタンにactionlistenerを追加し、filechooserを定義します。

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() );                                    
    }
};
于 2012-11-22T15:39:26.147 に答える