2014-10-16 7 views
2

GUI에 큰 문제가 있습니다. 내 첫 번째 문제는 내가 JTextArea가 문자열을 표시하게하는 버튼을 누르면 텍스트 영역이 변경되어 모든 GUI와 모든 GUI를 푸시한다는 점입니다. 나는 많은 해결책을 시도했지만 아무것도 효과가 없었다.JScrollPane의 JTextArea가 표시되지 않습니다.

필자는 스크롤 영역에 텍스트 영역을 넣었습니다. 스크롤 패널에 텍스트가 표시되지 않기 때문에 잘 모릅니다. 누군가 내 코드를보고 내가 뭘 잘못하고 있는지 말해 줄 수 있습니까?

감사

왼쪽 하단에있는 작은 버튼을 누르면
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class myClass extends JFrame implements ActionListener{ 
    public JButton picture = new JButton(); 
    public JTextArea description = new JTextArea(10,30); 
    public JButton B1 = new JButton(); 
    public JTextArea C1 = new JTextArea(); 

    public myClass (String STP){ 

     super(STP); 
     makeGUI(); 
    } 

    public static void main(String[] args){ 
     myClass test = new myClass("story"); 
    } 

    public void actionPerformed(ActionEvent event){ 
     Object source = event.getSource(); 
     if (source==B1){ 
      description.setText("hello world"); 
      C1.setText("hello world"); 
     } 
    } 

    public void makeGUI(){ 

     JScrollPane scroll = new JScrollPane(description); 
     JPanel pane = new JPanel(new GridBagLayout()); 
     Container con = this.getContentPane(); 
     setBounds(50,50,600,600); //(x,-y,w,h) from north west 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // picture 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill=GridBagConstraints.NONE; 
     gbc.ipadx=260; 
     gbc.ipady=310; 
     gbc.insets=new Insets(5,5,5,5); 
     gbc.gridx=0; 
     gbc.gridy=0; 
     gbc.gridwidth=2; 
     gbc.gridheight=3; 
     pane.add(picture,gbc); 

     // button 1 
     B1.addActionListener(this); 
     gbc.fill=GridBagConstraints.NONE; 
     gbc.ipadx=0; 
     gbc.ipady=10; 
     gbc.insets=new Insets(5,5,5,5); 
     gbc.gridx=0; 
     gbc.gridy=3; 
     gbc.gridwidth=1; 
     gbc.gridheight=1; 
     pane.add(B1,gbc); 

     //caption 1 
     gbc.fill=GridBagConstraints.HORIZONTAL; 
     gbc.ipadx=0; 
     gbc.ipady=30; 
     gbc.insets=new Insets(5,5,5,5); 
     gbc.gridx=1; 
     gbc.gridy=3; 
     gbc.gridwidth=3; 
     gbc.gridheight=1; 
     C1.setEditable(false); 
     C1.setLineWrap(true); 
     C1.setWrapStyleWord(true); 
     pane.add(C1,gbc); 

     // description !this is the part im having a problem with! 
     gbc.fill=GridBagConstraints.BOTH; 
     gbc.ipadx=100; 
     gbc.ipady=170; 
     gbc.insets=new Insets(10,10,10,0); 
     gbc.gridx=2; 
     gbc.gridy=0; 
     gbc.gridwidth=2; 
     gbc.gridheight=1; 
     description.setEditable(false); 
     description.setLineWrap(true); 
     description.setWrapStyleWord(true); 

     scroll.add(description); 
     pane.add(scroll,gbc); 

     con.add(pane); 

     setVisible(true); 

    } 
} 

시도. 두 텍스트 영역 모두에서 텍스트를 볼 수 있지만 하나만 작동합니다.

+1

왜이 새로운 JScrollPane (설명)을 사용합니까? 그리고이 scroll.add (설명); –

+0

+1은 'SSCCE'입니다. – Reimeus

+0

[JScrollPane에있을 때 테이블이 보이지 않는 이유는 무엇입니까?] (http://stackoverflow.com/questions/22593867/why-is-my-table-not-visible-when-its-in-a) -jscrollpane) –

답변

5

이것은 일반적인 실수입니다. 따라서 solution by Reimeus 비트 설명 :

JScollPane은 여러 하위 구성 요소가 포함 된 복잡한 구성 요소입니다. How to use Scroll Panes 자습서의 this image에 표시된대로 Viewport과 가능하게는 JScrollBar을 포함 할 수 있습니다. 이러한 구성 요소는 특수 레이아웃 관리자 (특히 ScrollPaneLayout)와 함께 정렬됩니다.

scrollPane.add(componentThatShouldBeScrolled)을 호출하면 새로운 구성 요소가 기존 구성 요소 중 하나를 대체하거나 예기치 않은 전체 레이아웃을 망칠 수 있습니다.

대신 수동으로 스크롤에 대한 내용을 추가하는 것은, 당신은 일반적으로 JScollPane 생성자에 전달 : 당신이 "스크롤되는 구성 요소를"대체이 경우,

JScrollPane scrollPane = new JScrollPane(componentThatShouldBeScrolled); 

또는, 당신은 할 수 호출

scrollPane.setViewportView(componentThatShouldBeScrolled); 
5

JScrollPane 구성 요소의 ViewPortView을 대체하는이 설명문을 제거하십시오.

scroll.add(description); 

JTextAreadescription

이미 뷰포트보기로 설정되어있다.

관련 문제