2010-02-20 115 views
2

在下面的示例中,我希望能夠在最大化或最小化窗口時擴展文本區域。現在,textarea被設置爲cols/rows。如果我點擊最大化,文本區域應該隨着窗口的擴展而擴展。Java swing和擴展窗口/ textarea

注意:這是一個僞示例。我可能會添加更多的組件,這就是爲什麼我使用GridBagLayout管理器的原因。

下面的版本給我什麼,我想:

package org.berlin.pino.test.functional.jogl; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class BasicText { 

    public static JPanel buildPanel() { 

     final JPanel panel = new JPanel(new GridBagLayout()); 
     final GridBagConstraints gc = new GridBagConstraints(); 

     final JTextArea text = new JTextArea("Text"); 
     final JScrollPane scrollPane = new JScrollPane(text); 
     gc.fill = GridBagConstraints.BOTH; 
     gc.weightx = 1; 
     gc.weighty = 1;   

     // Add the textarea -> scroll pane -> to the panel -> to the jframe      
     panel.add(scrollPane, gc);   
     return panel; 
    } 

    public static void main(String[] args) { 


     JFrame frame = new JFrame("Hello World!"); 
     frame.setLayout(new GridBagLayout()); 
     final GridBagConstraints gc = new GridBagConstraints(); 
     gc.fill = GridBagConstraints.BOTH; 
     gc.weightx = 1; 
     gc.weighty = 1; 

     frame.add(buildPanel(), gc); 
     frame.setPreferredSize(new Dimension(300, 300)); 
     frame.setLocation(200, 100); 
     frame.setBackground(Color.white);    

     frame.pack(); 
     frame.setVisible(true); 
    } 

} // End of the class // 

回答

3

在您GridBagConstraints使用

gc.weightx = 1d; 
gc.weighty = 1d; 

JavaDocweightx/weighty說:

指定如何分佈額外的水平/垂直空間。

+0

謝謝,工作。 –