2014-10-15 2 views
0

텍스트 필드, 텍스트 영역 및 단추 두 개를 만들어야하지만 내 프로그램을 실행할 때 나타나지 않기 때문에 텍스트 필드에 붙어 있습니다. 내 JFrame은 계속 비어 있습니다.텍스트 필드가 나타나지 않는 이유는 무엇입니까?

public class SentanceBuilder extends JFrame { 
    private JTextField textField = new JTextField(50); 
    private JTextArea textArea = new JTextArea(200,200); 
    private JButton Submit = new JButton(); 
    private JButton Cancel = new JButton(); 

public SentanceBuilder(){ 
    textField.setVisible(true); 
    textField.setLocation(50, 50); 
    this.textField(); 
    this.setSize(400, 300); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 
    } 

public void textField(){ 


    String textContent = textField.getText(); 

} 
} 
+0

'textField.setLocation (50, 50) '1) 자바의 GUI는, 이와 같이 등 다른 OS', 스크린 크기, 스크린 해상도에서 작동해야 그들은 픽셀 완벽한 레이아웃에 도움이되지 않습니다. 대신 레이아웃 관리자 또는 [조합] (http://stackoverflow.com/a/5630271/418556)과 [공백] 레이아웃 채우기 및 테두리 (http://stackoverflow.com/a/17874718/)를 사용하십시오. 418556). 2) 더 빨리 도움을 받으려면 [MCVE] (http://stackoverflow.com/help/mcve) (최소한의 완전하고 검증 가능한 예)를 게시하십시오. –

+0

'private JTextArea textArea = new JTextArea (200,200);'이 텍스트 영역을 200 열 200 행으로 설정 하시겠습니까? 나는 그것을 충분히 표현할만큼 큰 모니터가 없다! 나는 당신이 그것들이 픽셀 값이라고 생각했다. –

답변

3

당신은 JFrame 등의 최상위 윈도우에 의해 유지되는 JPanel과 같은 컨테이너에 텍스트 필드 변수를 추가하지 않습니다. 사실, 귀하는 코드에서 nothing을 JFrame에 추가하십시오! 이 내 GUI가 있다면

, 나는

  • 을 거라고 내 구성 요소를 잡아 내 JTextField를
  • 만들기 메인 인 JPanel를 작성합니다.
  • JPanel의 add(...) 메서드를 통해 JPanel에 기타 구성 요소를 추가합니다.
  • JFrame의 add(...) 메서드를 통해 주 JPanel을 JFrame의 contentPane에 추가합니다.
  • 전화 pack()과 JFrame의에 다음 setVisible(true),하지만 그 모든 구성 요소를 추가 후,하지 전에.
  • 매번 추측을 이기기 때문에 스윙 튜토리얼을 읽으십시오. 자습서 링크는 Swing Tag Info 링크에서 얻을 수 있습니다.

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class MyFoo extends JFrame { 
    private static final long serialVersionUID = 1L; 
    private JTextField textField = new JTextField(10); 
    private JButton button = new JButton("Foo Button"); 

    public MyFoo() { 
     super("My JFrame"); 
     // so Java will end when GUI is closed 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     // code to be called when button is pushed 
     button.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      // TODO code that runs when button is pushed. 
     } 
     }); 

     // panel to hold everything 
     JPanel mainPanel = new JPanel(); 
     // add all to the panel 
     mainPanel.add(new JLabel("Text Field:")); 
     mainPanel.add(textField); 
     mainPanel.add(button); 

     // add the panel to the main GUI 
     add(mainPanel); 
    } 

    // start up code 
    private static void createAndShowGui() { 
     JFrame mainFrame = new MyFoo(); 
     mainFrame.pack(); 
     mainFrame.setLocationRelativeTo(null); 
     mainFrame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     // call start up code in a Swing thread-safe way 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
관련 문제