2013-02-16 2878 views
0

我正在尝试使用Java Swing将文本区域放到对话框中。我有一个设置此JTextArea大小的问题。如果我调整它的大小,文本区域的宽度总是等于窗口的整个宽度并与窗口一起延伸。我该如何调整Java Swing JTextArea的大小

private void arrangeComponents() { 
    JTextArea textArea = new JTextArea(); 
    JPanel outerPanel = new JPanel(); 
    outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.PAGE_AXIS)); 

    JScrollPane scrollPane = new JScrollPane(textArea); 
    outerPanel.add(scrollPane, BorderLayout.CENTER); 

    Container contentPane = getContentPane(); 
    contentPane.add(outerPanel, BorderLayout.CENTER); 
} 

我想让JTextArea水平对齐窗口的中心并且不会改变它的大小。

我做错了什么?

回答

1

我发现这是从一个简单的编码网站。此代码示例可能对您有用。

import java.awt.Dimension; 
import java.awt.FlowLayout; 

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

public class JTextAreaTest { 
    public static void main(String[] args) { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("JTextArea Test"); 
     frame.setLayout(new FlowLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     String text = "A JTextArea object represents a multiline area for displaying text. " 
       + "You can change the number of lines that can be displayed at a time, " 
       + "as well as the number of columns. You can wrap lines and words too. " 
       + "You can also put your JTextArea in a JScrollPane to make it scrollable."; 
     JTextArea textAreal = new JTextArea(text, 5, 10); 
     textAreal.setPreferredSize(new Dimension(100, 100)); 
     JTextArea textArea2 = new JTextArea(text, 5, 10); 
     textArea2.setPreferredSize(new Dimension(100, 100)); 
     JScrollPane scrollPane = new JScrollPane(textArea2, 
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
       JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     textAreal.setLineWrap(true); 
     textArea2.setLineWrap(true); 
     frame.add(textAreal); 
     frame.add(scrollPane); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+0

+1并排比较[sscce](http://sscce.org/)。我的例子需要一个滚动窗格,你可以使用一个'invokeLater()'。 :-) – trashgod 2013-02-16 19:24:40

+2

-1对于使用setPreferredSize(),它只是一堆随机数字,它们没有考虑用于确定每行行高的字体。 setPreferredSize()方法会覆盖您在创建文本区域时尝试指定的行,列值。 – camickr 2013-02-16 19:30:01

+0

@camickr提出了一个我忽视的关键点;更多[这里](http://stackoverflow.com/q/7229226/230513)。此外,请注意[*不可调整大小的容器*](http://stackoverflow.com/a/12532237/230513),另请参阅[*初始线程*](http://docs.oracle.org/)。COM/JavaSE的/教程/ uiswing /并发/ initial.html)。 – trashgod 2013-02-16 19:35:10

2
outerPanel.add(scrollPane, BorderLayout.CENTER); 

甲BoxLayout的不采取约束,所以BorderLayout.CENTER是不必要的。

问题是,BoxLayout尊重组件的最大尺寸,这对于滚动窗格来说设置得非常大。

不是使用BoxLayout,而是使用带有FlowLayout的面板。

运行下面的示例以查看您当前正在执行的操作。然后注释掉setLayout(...)语句并再次运行。默认情况下,面板使用FlowLayout,这样你就可以得到你想要的东西。

import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class SSCCE extends JPanel 
{ 
    public SSCCE() 
    { 
      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 

      JTextArea textArea = new JTextArea(5, 30); 
      JScrollPane scrollPane = new JScrollPane(textArea); 
      //scrollPane.setMaximumSize(scrollPane.getPreferredSize()); 

      add(scrollPane); 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new SSCCE()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 

或者,如果你真的想保持的BoxLayout然后离开保持setLayout的(...)语句,然后设置最大尺寸等于优先停留大小。很多人会说你不应该直接调用“setXXX()”方法,而应该重写scrollpane的setMaximumSize()方法以返回首选大小。

请注意,在测试这两个解决方案时,请确保您使窗口小于滚动窗口以查看每个布局的工作方式。

1

只是调用该方法对UR文本区域:setLineWrap(true);

0

如果JTextArea是initializated JTextArea text = new JTextArea(int rows, int columns)

你只需要调用该方法text.setLineWrap(true)

然后text'size是固定的。