2016-12-13 9 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コードを実行するのは悪い考えです。

関連する問題