如何将文件选为全局文件?

时间:2016-12-13 18:57:08

标签: java swing

我写了一个简单的GUI程序来搜索读写文本文件。

package MyGUIStuff;

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

public class multiWinDemo {

    public static void main(String[] args) {
        JLabel lbl = new JLabel ("File Name:");
        JTextField file = new JTextField (10);
        file.setEditable(false);

        JButton browse = new JButton ("Browse");
        browse.addActionListener(new ActionListener () { 
            public void actionPerformed(ActionEvent ext ) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
              File selectedFile = fileChooser.getSelectedFile();
              file.setText(selectedFile.getName());
            }
        }
        });

        JButton search = new JButton ("Search");
        JButton write = new JButton ("Write");

        JButton read = new JButton ("Read");
        read.addActionListener(new ActionListener () { 
            public void actionPerformed(ActionEvent ext ) {
                BufferedReader br = null;

                try {
                    String currentLine;
                    br = new BufferedReader(new FileReader(selectedFile.getName() ));

                    while ((currentLine=br.readLine()) != null) {
                        System.out.println(currentLine);
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }finally {
                    try { 
                        if (br != null) br.close();
                    }catch (IOException ex){
                        ex.printStackTrace();
                    }
                }
        }
        });


        JButton exit = new JButton ("Exit");
        exit.addActionListener(new ActionListener () { 
            public void actionPerformed(ActionEvent ext ) {
            System.exit(0);
        }
        });

        JPanel blank = new JPanel ();
        JPanel first = new JPanel();
        first.setLayout(new GridLayout(3,0,5,5) );
        first.add(lbl);
        first.add(file);
        first.add(browse);
        first.add(write);
        first.add(search);
        first.add(read);
        first.add(blank);
        first.add(exit);

        JPanel rPanel = new JPanel ();


        JFrame multiWin = new JFrame ("MultiWin");
        multiWin.setSize(300,130);
        multiWin.setLayout(new CardLayout() );
        multiWin.setLocationRelativeTo(null);
        multiWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        multiWin.setVisible(true);

        multiWin.add(first);

    }

}

我的问题是如何在全球第19-22行选择文件?那么我的整个程序都可以使用它吗?

非常感谢任何帮助。谢谢! :d

2 个答案:

答案 0 :(得分:0)

不要将所有代码都放在main()方法中。

而是创建一个包含GUI组件的面板。然后,您可以创建可供整个类使用的实例变量。

例如,请查看How to Use File Choosers上的Swing教程中的部分。 FileChooserDemo是一个让您入门的工作示例。

不是教程示例结构也将向您展示如何构造代码,以便在事件调度线程(EDT)上创建GUI。所有更新GUI的代码都应该在EDT上执行。

答案 1 :(得分:0)

public class multiWinDemo {
public File selectedFile;
    public static void main(String[] args) {
        JLabel lbl = new JLabel ("File Name:");
        JTextField file = new JTextField (10);
        file.setEditable(false);

        JButton browse = new JButton ("Browse");
        browse.addActionListener(new ActionListener () { 
            public void actionPerformed(ActionEvent ext ) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
              selectedFile = fileChooser.getSelectedFile();
              file.setText(selectedFile.getName());
            }
        }
        });

        JButton search = new JButton ("Search");
        JButton write = new JButton ("Write");

        JButton read = new JButton ("Read");
        read.addActionListener(new ActionListener () { 
            public void actionPerformed(ActionEvent ext ) {
                BufferedReader br = null;

                try {
                    String currentLine;
                    br = new BufferedReader(new FileReader(selectedFile.getName() ));

                    while ((currentLine=br.readLine()) != null) {
                        System.out.println(currentLine);
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }finally {
                    try { 
                        if (br != null) br.close();
                    }catch (IOException ex){
                        ex.printStackTrace();
                    }
                }
        }
        });


        JButton exit = new JButton ("Exit");
        exit.addActionListener(new ActionListener () { 
            public void actionPerformed(ActionEvent ext ) {
            System.exit(0);
        }
        });

        JPanel blank = new JPanel ();
        JPanel first = new JPanel();
        first.setLayout(new GridLayout(3,0,5,5) );
        first.add(lbl);
        first.add(file);
        first.add(browse);
        first.add(write);
        first.add(search);
        first.add(read);
        first.add(blank);
        first.add(exit);

        JPanel rPanel = new JPanel ();


        JFrame multiWin = new JFrame ("MultiWin");
        multiWin.setSize(300,130);
        multiWin.setLayout(new CardLayout() );
        multiWin.setLocationRelativeTo(null);
        multiWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        multiWin.setVisible(true);

        multiWin.add(first);

    }

}

在此代码中,File是全局的。但正如另一个答案所述,在主要方法中做所有gui代码是一个坏主意