2014-08-31 102 views
1

我在JPanel中使用了JEditorPane,我在下面的示例中称其为contentPane。内容窗格位于JScrollPane内。我希望编辑器窗格填充尽可能小的空间(只是包含它的文本),而内容窗格中剩余的可用空间留空到底部和任意一侧。如果文本太大,编辑器窗格会增大,最终滚动窗格将创建滚动条来浏览所有内容。但是,编辑器窗格只会占用其首选大小(即其包含的文本的大小),而不会填充整个内容窗格,该窗格将填充滚动窗格的整个视图端口。下面是一些例子代码来证明我的问题:JEditorPane填充JScrollPane中的所有可用空间

import java.awt.BorderLayout; 
import java.awt.Color; 

import javax.swing.BoxLayout; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

@SuppressWarnings("serial") 
public class ScrollPaneExample extends JFrame { 
    public static void main(String[] args) { 
     new ScrollPaneExample(); 
    } 
    public ScrollPaneExample() { 
     super("ScrollPaneExample"); 

     setSize(400, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new BorderLayout()); 

     JEditorPane editorPane = new JEditorPane("text/plain", ""); 
     editorPane.setBackground(Color.YELLOW); 

     for (int i = 0; i < 5; i++) 
      editorPane.setText(editorPane.getText() + "Hello World\n"); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 
     contentPane.setBackground(Color.BLUE); 
     contentPane.add(editorPane); 

     JScrollPane scrollPane = new JScrollPane(contentPane); 

     add(scrollPane, BorderLayout.CENTER); 

     setVisible(true); 
    } 
} 

运行时,这个窗口是什么样子:

Example

通过黄色的背景下,我们可以看到,在编辑器窗格正在所有的空间。下面是我想要的布局看起来像一个(Photoshop处理)例如:

Desired

蓝色的背景代表内容窗格中,黄色代表编辑窗格。

编辑:在我的工作程序中,contentPane不仅仅是一个编辑器窗格。这就是为什么我使用BoxLayout代替contentPaneFlowLayout;因为需要垂直页面流布局。

+0

是如何确定的大小?你想要这个确切的大小(意味着适合五个你好世界)?你想要一个特定的尺寸?你想要基于动态文本的大小吗? – 2014-08-31 20:03:46

+0

@peeskillet编辑器窗格应根据文本动态更改大小。适合输入的任何文本。 – 2014-08-31 20:12:54

+0

你想要在滚动窗格中的视图动态调整大小(基本上整个滚动窗格 - 哪一个击败滚动窗格的prupose)?或视图保持不变,但卷轴添加?在后一种情况下,什么是确定初始大小,五个hello世界的内容? – 2014-08-31 20:15:18

回答

3

仅对内容窗格使用FlowLayoutBorderLayout将不会优先考虑尺寸,并且会拉伸组件以适应。查看更多有关布局管理器在Laying out Components Withing a Container

如果你想设置为编辑窗格的初始大小,就像任何其他的组件是Scollable,您可以覆盖getPreferredScrollableViewportSize()设置为滚动窗格视图大小(当组件被添加)

此外,您应该将编辑器添加到滚动窗格,而不是面板。做后者时,编辑器将不会在滚动窗格中滚动。

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

import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 

@SuppressWarnings("serial") 
public class ScrollPaneDemo extends JFrame { 
    public static void main(String[] args) { 
     new ScrollPaneDemo(); 
    } 

    public ScrollPaneDemo() { 
     super("ScrollPaneExample"); 

     setSize(400, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 

     JEditorPane editorPane = new JEditorPane("text/plain", "") { 
      @Override 
      public Dimension getPreferredScrollableViewportSize() { 
       return new Dimension(200, 200); 
      } 
     }; 
     editorPane.setBackground(Color.YELLOW); 

     for (int i = 0; i < 50; i++) { 
      editorPane.setText(editorPane.getText() 
        + "Hello World Hello World\n"); 
     } 

     JScrollPane scrollPane = new JScrollPane(editorPane); 

     add(scrollPane); 

     setVisible(true); 
    } 
} 

UPDATE

在我工作的程序,还有比在contentPane的只是一个编辑器面板的更多。这就是为什么我使用BoxLayout而不是用于contentPane的FlowLayout;因为需要垂直页面流布局。

BoxLayout是问题所在。您需要为编辑器窗格设置最大大小。或者只是一起摆脱BoxLayout。的FlowLayout或GridBagLayout的将尊重优选尺寸


UPDATE 2

下面是使用的GridBagLayout一个例子,其可以更适合于您的“膨胀”的编辑器。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.Timer; 

@SuppressWarnings("serial") 
public class ScrollPaneExample extends JFrame { 
    public static void main(String[] args) { 
     new ScrollPaneExample(); 
    } 
    public ScrollPaneExample() { 
     super("ScrollPaneExample"); 

     setSize(400, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new BorderLayout()); 
     //getContentPane().setBackground(Color.BLUE); 

     final JEditorPane editorPane = new JEditorPane("text/plain", "") { 
     }; 

     editorPane.setBackground(Color.YELLOW); 

     for (int i = 0; i < 5; i++) 
      editorPane.setText(editorPane.getText() + "Hello World\n"); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weighty = 1; 
     contentPane.setBackground(Color.BLUE); 
     contentPane.add(editorPane, gbc); 

     gbc.gridy++; 
     contentPane.add(new JButton("Button"), gbc); 
     gbc.gridy++; 
     contentPane.add(new JButton("Button"), gbc); 

     JPanel panel = new JPanel(); 
     panel.add(contentPane); 
     JScrollPane scrollPane = new JScrollPane(panel); 

     add(scrollPane); 

     setVisible(true); 

     Timer timer = new Timer(1000, new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       editorPane.setText(editorPane.getText() + "Hello World\n"); 
       setVisible(true); 
      } 
     }); 
     timer.start(); 
    } 
} 

enter image description here

+0

你误会了我。在我的例子中,滚动窗格实际上是不相关的。我希望滚动窗格可以填充整个窗口(400x400),但是内容符合“JPanel”以适合其最小尺寸。我将JEditorPane放在'JPanel'内而不是直接将其添加到滚动窗格的原因是因为在我的工作程序中,'contentPane'内部的组件不仅仅是编辑器窗格。也许我已经弄糊涂了,我把调用窗口内的'JPanel'作为一个'contentPane',这与Window的顶层“内容窗格”模糊不清。 – 2014-08-31 20:32:56

+0

Gotcha。 BoxLayout是个问题。您需要为编辑器窗格设置最大大小。或者只是一起摆脱BoxLayout。 FlowLayout或GridBagLayout将遵循首选大小 – 2014-08-31 20:38:50

+0

将首选大小的编辑器窗格的最大大小设置为我首先想到解决问题的方式。这里的问题是,当更多的文本添加到编辑器窗格中时,其首选大小将自动更改,但其最大大小不会改变。添加一个'ComponentListener'不起作用,因为它只能监听大小改变,而不是首选大小。看看我的编辑为什么'BoxLayout'是可取的。也许'GridBagLayout'可能是解决方案,我会看看。 – 2014-08-31 20:42:22