2016-12-13 46 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代碼是一個糟糕的主意