2016-12-13 2 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의 스윙 튜토리얼 섹션을 확인하십시오. FileChooserDemo은 시작하기위한 실례입니다.

구조체 예제 구조에서는 GUI가 EDT (Event Dispatch Thread)에서 작성되도록 코드를 구조화하는 방법을 보여주지 않습니다. GUI를 업데이트하는 모든 코드는 EDT에서 실행해야합니다.

+0

어떻게해야할까요? 나는 아직도 자바를 배우는 중이다. – 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 코드를 수행하는 것은 좋지 않습니다.

관련 문제