调整窗口大小时,JXTaskPane容器生长错误

时间:2014-02-22 14:58:54

标签: java swing swingx

我的JFrame右侧有一个JXTaskPaneContainer,其中包含超链接和标签。窗口的主要部分应该是:

  • 左侧的大面积文字
  • 右侧较小的区域,包含消息和操作

现在,问题是添加大量新元素会导致容器向左和向上增长:

enter image description here

JXTaskPaneContainer (video/yt)内有javax.swing.JScrollPane时更加怪异。

问题似乎在于JXLabel。 我怀疑它是自动换行? 任何想法如何解决这个问题?

[实施例] 根据请求自包含示例。 Full source on github.

package layoutproblemSSCEC;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.jdesktop.swingx.JXButton;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;

public class SwingWindow {

    private JFrame frame;
    String dolorem = "Sed ut perspiciatis unde omnis iste natus error sit "
            + "voluptatem accusantium doloremque laudantium, totam rem aperiam, "
            + "eaque ipsa quae ab illo inventore veritatis et quasi architecto "
            + "beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem "
            + "quia voluptas sit aspernatur aut odit aut fugit, sed quia "
            + "consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. "
            + "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, "
            + "consectetur, adipisci velit, sed quia non numquam eius modi tempora "
            + "incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut "
            + "enim ad minima veniam, quis nostrum exercitationem ullam corporis "
            + "suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? "
            + "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse "
            + "quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat "
            + "quo voluptas nulla pariatur?";

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SwingWindow window = new SwingWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SwingWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JXTaskPaneContainer panels = new JXTaskPaneContainer();
        JScrollPane scrollpanel = new JScrollPane(panels);

        Container content = frame.getContentPane();
        content.add(new JTextArea(dolorem), BorderLayout.CENTER);
        content.add(scrollpanel, BorderLayout.EAST);
        content.add(new JXButton(new AbstractAction("Click me") {

            public void actionPerformed(ActionEvent arg0) {
                JXTaskPane panel = new JXTaskPane("test");
                JXLabel lbl = new JXLabel(dolorem);
                lbl.setLineWrap(true);
                panel.add(lbl);
                panels.add(panel);
            }
        }), BorderLayout.NORTH);

        panels.add(new JXTaskPane("Click the button, then resize"));
    }

}

1 个答案:

答案 0 :(得分:0)

我希望我的问题有一个简明的答案。我担心没有。 我通过基于GridBagLayout而非BorderLayout创建新布局来解决此问题。

还存在设置最小首选尺寸的问题。没有调用setMinimumSize(new Dimension(200, 127)),容器的宽度似乎无限缩小,尽管广告的首选宽度为600.我仍然不确定它来自何处。这些数字来自组件的初始首选大小。

tutorial on oracle's site帮助了我。重要的是找到有效的重量。权重值大于0允许组件增长和缩小。

这就是布局代码现在的样子:

frmvanInput = new JFrame(); // window
// ... captions, titles, etc

frmvanInput.setContentPane(new JPanel(new GridBagLayout()));

// ... more initializing

/** 
 * LAYOUTS
 */
final Container contentPane = frmvanInput.getContentPane();

// the menu bar is on top, stretches all the way right
GridBagConstraints menuBarLayout = new GridBagConstraints();
// top left
menuBarLayout.anchor = GridBagConstraints.FIRST_LINE_START; // push to the top left
menuBarLayout.gridx = 0; 
menuBarLayout.gridy = 0;
// manage width
menuBarLayout.gridwidth = 2; // stretch
menuBarLayout.fill = GridBagConstraints.HORIZONTAL; // fill all x-space
menuBarLayout.weightx = 0.5; // a weight > 0 allows for resizing
//add
contentPane.add(menuBar, menuBarLayout);

// the text field is under the menu on the left and shares a row with the errors panel/scroll pane
GridBagConstraints textfieldLayout = new GridBagConstraints();
// top left
textfieldLayout.anchor = GridBagConstraints.FIRST_LINE_START; // push to the top left
textfieldLayout.gridx = 0; // col left
textfieldLayout.gridy = 1; // row center
// manage stretching
textfieldLayout.weightx = 0.5; // resizable
textfieldLayout.weighty = 0.5; // resizable
textfieldLayout.fill = GridBagConstraints.BOTH;
// add
contentPane.add(txtEditor, textfieldLayout);

errorScrollPane = new JScrollPane(containerTaskPanel);
// prevent indefinate shrinking
errorScrollPane.setMinimumSize(new Dimension(200, 127));

GridBagConstraints scrollpanelLayout = new GridBagConstraints();
// middle, right
scrollpanelLayout.anchor = GridBagConstraints.FIRST_LINE_END;
scrollpanelLayout.gridx = 1; // right row
scrollpanelLayout.gridy = 1; // center column
// stretch to fill
scrollpanelLayout.weightx = 0.5; // resizable
scrollpanelLayout.weighty = 0.5; // resizable
scrollpanelLayout.fill = GridBagConstraints.BOTH;
// add our controls to the visible world
contentPane.add(errorScrollPane, scrollpanelLayout);

// the emitter panel has the bottom row to itself
GridBagConstraints emitterpanelLayout = new GridBagConstraints();
// bottom left
emitterpanelLayout.anchor = GridBagConstraints.LAST_LINE_START;
emitterpanelLayout.gridx = 0; // col left
emitterpanelLayout.gridy = 2; // row bottom
// stretch width
emitterpanelLayout.weightx = 0.5;
emitterpanelLayout.gridwidth = 2;
// manage height:
emitterpanelLayout.weighty = 0.1;
emitterpanelLayout.fill = GridBagConstraints.BOTH;
// add
contentPane.add(emitterScrollPane, emitterpanelLayout);