2016-12-13 68 views
0

我写了一个简单的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

全局通常是一个坏主意。你应该把它传递给需要它的类。 – SLaks

+0

你是什么意思传递它? – shrillhook

回答

0

请勿将所有代码放在main()方法中。

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

例如查看How to Use File Choosers上Swing教程的部分。 FileChooserDemo是一个可以开始使用的示例。

不是教程示例结构还会告诉您如何构造代码,以便在事件派发线程(EDT)上创建GUI。所有更新GUI的代码都应该在EDT上执行。

+0

我会如何去做这件事?我仍然在学习Java。 – shrillhook

+0

我告诉过你如何做到这一点。从教程中的演示代码开始。了解它如何工作,然后进行更改。如果你不尝试,你不会学习。 – camickr

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代码是一个糟糕的主意