2017-11-24 11 views
-1

현재 Java GUI를 배울 수 있습니다. Yam 클래스의 j1 ('open'버튼)과 Open_Button 클래스의 기능을 연결하고 싶습니다. Yam 클래스의 전역 변수 인 "textField"에 파일 이름을 쓰고 'open'을 누르면 "TextArea"가 열린 파일의 내용을 출력해야합니다. 하지만 어떻게 든 작동하지 않습니다. open_button 클래스의 함수는 Yam 클래스의 "textField"와 연결되어 있지 않습니다. 그러나 나는 틀린 것을 정말로 발견 할 수 없다. 불쌍한 내 Engish에 대한 (죄송합니다 ..;. (난 당신이 내가, 최소 완료하고 검증 가능한 rull을 따르도록 전체 코드를 원하지만 잘못된 부분입니다 정말 모르는 보여주지 ..)open_button 클래스의 함수를 사용하여 텍스트 필드를 연결하는 방법

Yam class) 

    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.io.File.*; 
    import java.io.*; 
    import java.nio.file.*; 

    public class Yam extends JFrame { 
    Container contentPane; 
    JButton j1, j2, j3, j4, j5, j6; 
    JTextField textField; 
    JTextField textField_1; 
    TextArea textArea, textArea_1; 

    public Yam() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    contentPane = getContentPane(); 
    contentPane.setLayout(null); 

    textField = new JTextField(); 
    textField.setBounds(14, 12, 240, 24); 
    contentPane.add(textField); 
    textField.setColumns(20); 

    j1 = new JButton("Open"); 
    j1.setBounds(294, 11, 105, 27); 
    j1.addActionListener(new AB()); 
    contentPane.add(j1); 

    textField_1 = new JTextField(); 
    textField_1.setBounds(14, 48, 240, 24); 
    contentPane.add(textField_1); 
    textField_1.setColumns(20); 

    j2 = new JButton("Save"); 
    j2.setBounds(294, 47, 105, 27); 
    contentPane.add(j2); 

    textArea = new TextArea(); 
    textArea.setBounds(10, 105, 405, 190); 
    contentPane.add(textArea); 

    j3 = new JButton("Compile"); 
    j3.setBounds(10, 319, 87, 25); 
    j3.addActionListener(new AB()); 
    contentPane.add(j3); 

    j4 = new JButton("Save Errors"); 
    j4.setBounds(111, 319, 87, 25); 
    contentPane.add(j4); 

    j5 = new JButton("Delete"); 
    j5.setBounds(217, 319, 87, 25); 
    contentPane.add(j5); 

    j6 = new JButton("Clear"); 
    j6.setBounds(321, 319, 87, 25); 
    j6.addActionListener(new AB()); 
    contentPane.add(j6); 

    textArea_1 = new TextArea(); 
    textArea_1.setBounds(10, 391, 405, 253); 
    contentPane.add(textArea_1); 

    setSize(506, 698); 
    setVisible(true); } 



class AB implements ActionListener { 
private TextArea args; 

    public void actionPerformed(ActionEvent e) { 
    Object source = e.getSource(); 
    CmdClass cmd = new CmdClass(); 
    Open_button open = new Open_button(); 
    JFrame f = new JFrame("what"); 

    if(source == j1) { 
      } 
    else if(source == j2) { 
     } 

    else if(source == j3) { 
     if(cmd.main(args)) 
      textArea_1.append("Compile Success\n"); 
    } 
    else if(source == j4) { } 
    else if(source == j5) { } 
    else { 
     textField.setText(" "); 
     textField_1.setText(" "); 
     textArea.setText(" "); 
     textArea_1.setText(" "); }}} 



    public static void main(String[] args) { 
     new Yam(); 
     } 




     public static String getFileName() { 
    // TODO Auto-generated method stub 
    return null;}} 

 Open_button class) 

import java.io.*; 
import java.nio.file.*; 
import java.io.File; 

public class Open_button { 
    Path Path; 

    public void main(String Str_paths){ 
     Yam y = new Yam(); 

     Str_paths = y.textField.getText(); 
     Path = FileSystems.getDefault().getPath(Str_paths); 
     File file = Path.toFile(); 

     if(Path.toFile() != null) 
      y.textArea.append("file exist!");  
     else { 
      y.textArea_1.append("file not exist"); 
      BufferedReader br = null; 
      try { 
       br = new BufferedReader(new FileReader(Str_paths)); 
       String line; 
       while ((line = br.readLine()) != null) { 
        y.textArea.append(line); 
       } 
      } catch (FileNotFoundException e1) { 
       e1.printStackTrace(); 
      } catch (IOException e2) { 
       e2.printStackTrace(); 
      } 
     } 
    } 
} 
+0

"JTextField 및 JButton 연결"?? action.properted 내에서 field.getText 또는 이와 비슷한 것을 호출하십시오. – Stultuske

+0

어떻게 물건들이 서로 연결되어 있는지 보려면 유효한 [mcve]를 게시해야합니다. 문제는 잘못된 참조를 사용하여 두 클래스를 올바르게 연결하지 못하는 문제이지만이 게시물에서 알기는 어렵습니다. 다시 말하지만 MCVE를 작성하여 컴파일하고 실행할 수 있도록 코드를 개선하십시오 (자세한 내용은 [mcve] 링크 참조). –

답변

1

나는 어떤 이유로 열고 파일 경로가 JTextField를에 지정된 파일을 읽을 당신이 원하는 생각, 가장 간단한 방법은 다음과 같습니다

JButton openButton = new JButton("Open File");    
openButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     String thePath = textfield.getText(); 
     File theFile = new File(thePath); 
     ...read..write.. 
    } 
}); 

나는 참마 클래스에 직접 선언 할 ;

코드에서 두 가지 문제가 있습니다. 첫째, 두 개의 Yam (y) 인스턴스가 있습니다 (OpenButton 클래스에서 선언 된 인스턴스는 위에있는 인스턴스와 다릅니다). 둘째, 파일 읽기 쉽고 항상 파일 시스템보기에 위치합니다 ... 또한 JFileChooser 대화 상자를 고려해야한다고 생각합니다. 안부,

+0

너무 많이 감사합니다! 나는 시도 할 것이다 –

+0

OMG 그것은 작동한다! !!!!!! @ #! @ # 당신은 나의 인생을 구한다 haha ​​고맙다 –