2017-10-14 8 views
2

프로그램의 현재 상태에 대한 사용자 정보를 제공하는 그래픽 콘솔을 만들고 싶습니다. JTextArea를 사용할 계획 이었지만 append() 메서드에 문제가 있습니다. 메인 클래스에서 사용했다하더라도 여전히 비어있는 JTextArea가 있습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 여기 Append()가 JTextArea 요소에서 작동하지 않습니다.

콘솔의 GUI의 코드입니다 :

package com.meh; 

import javax.swing.*; 

public class Controller extends JFrame { 
    public JPanel ControlPanel; 
    public JTextArea Log; 

    static void setView() { 
     JFrame frame = new JFrame("Controller"); 
     frame.setContentPane(new Controller().ControlPanel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

을 그리고 이것은 메인 클래스의 코드입니다 : 당신이 jTextArea APPEND 방법을 보면

package com.meh; 

public class Main { 
    public static void main(String[] args) { 
    Controller controller = new Controller(); 
     controller.setView(); 
     controller.Log.append("Hello"); 
    } 
} 

답변

2

, 당신은 그것을 볼 수 있습니다 :

Appends the given text to the end of the document 

그러나 String이 emp 인 경우 아무 작업도 수행하지 않습니다. ty 또는 null입니다.

귀하의 경우에는 setText()을 사용할 수 있습니다.

+0

흠 ... append()를 setText()로 변경했지만 여전히 동일한 문제가 있습니다. – Halep

+0

내 전화기에 있지만 jframe에 텍스트 영역을 추가하지 않았을 수 있습니다. – notyou

+0

또는 jTextArea를 초기화 할 수 있습니다. – notyou

1

getText()으로 전화하면 새 문자열 값이 반환됩니까? 그렇다면 문자를 변경 한 후에 controller 및/또는 controller.Logrepaint() 및/또는 revalidate()으로 전화해야 할 수 있습니다.

1

'ControlPanel'을 초기화하지 않으므로 항상 null이되므로 아무 것도 할 수 없습니다.