2014-10-16 77 views
2

我的GUI一直存在很大問題。我的第一個問題是,當我按下一個按鈕導致我的JTextArea顯示一個字符串時,他的文本區域發生變化,從而推動我的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

爲什麼你這樣做'new JScrollPane(description);'_and_ this'scroll.add(description);'? – 2014-10-16 14:00:39

+0

對於'SSCCE'爲+1。 – Reimeus 2014-10-16 14:14:48

+0

可能的重複[爲什麼我的表不可見時它是在JScrollPane?](http://stackoverflow.com/questions/22593867/why-is-my-table-not-visible-when-its-in-a -jscrollpane) – 2014-10-22 12:51:04

回答

5

這是一個常見的錯誤。所以要詳細說明solution by Reimeus有點:

JScollPane是一個複雜的組件,它包含幾個子組件。它可能包含Viewport和可能的JScrollBar s,如How to use Scroll Panes教程中的this image所示。這些組件安排了一個特殊的佈局管理器(特別是,與ScrollPaneLayout)。

當您只需撥打scrollPane.add(componentThatShouldBeScrolled)時,新組件可能會替換現有組件之一,或者無法預料地擰緊整個佈局。

,而無需手動添加用於滾動窗格的內容,但通常將它傳遞給JScollPane構造:

JScrollPane scrollPane = new JScrollPane(componentThatShouldBeScrolled); 

另外,如果你取代「應滾動組件」,你可以請致電

scrollPane.setViewportView(componentThatShouldBeScrolled); 
5

刪除替換JScrollPane組件的ViewPortView的聲明。

scroll.add(description); 

JTextAreadescription已經被設置爲口的視圖。