自动调整大小(JTextArea和JTabbedPane)而不是固定布局

时间:2012-11-08 17:33:51

标签: java jtextarea jtabbedpane

我在Java和Swing GUI中完成了我的第一步。我的计划是使用JTextArea创建一个小GUI来填充一些文本和两个选项卡式JPanel。但问题是,当我在JTextArea中填写一些文本并单击“包含”选项卡(或其他选项卡“函数”)时,它会调整tabmenu的大小(它应该有一个固定的50:50视图)。

以下是一些可以更好理解的图片:

enter image description here enter image description here enter image description here

这是Java代码:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class Editor extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        Editor editor = new Editor();
    }

    private JTextArea guiEdit = new JTextArea();
    private JScrollPane utilScrollbar = new JScrollPane(guiEdit);
    private JTabbedPane guiOverviewTabPanel = new JTabbedPane();
    private JPanel guiOverviewTabIncludes = new JPanel();
    private JPanel guiOverviewTabFunctions = new JPanel();

    public Editor() {
        // Set layout
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gridbag);

        // Add the textfield
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.WEST;
        add(utilScrollbar, c);

        // Add tabs to the JTabbedPane
        guiOverviewTabPanel.add("Includes", guiOverviewTabIncludes);
        guiOverviewTabPanel.add("Functions", guiOverviewTabFunctions);

        // Add the JTabbedPane
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 1;
        c.gridy = 0;
        c.gridheight = 1;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.EAST;
        add(guiOverviewTabPanel, c);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        this.setMinimumSize(new Dimension(800, 400));

        setVisible(true);
    }
}

有没有人知道解决这个问题的方法/解决方案。实际比例是5:2(5份JTextArea和2份JTabbedPane - 这只是为了更好地理解,所以我不能使用GridLayout

1 个答案:

答案 0 :(得分:1)

JSplitPane适用于此类GUI显示。

我修改了你的代码以使用JSplitPane。

import java.awt.Dimension;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

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

public class Editor extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new Editor();
    }

    private JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    private JTextArea guiEdit = new JTextArea();
    private JScrollPane utilScrollbar = new JScrollPane(guiEdit);
    private JTabbedPane guiOverviewTabPanel = new JTabbedPane();
    private JPanel guiOverviewTabIncludes = new JPanel();
    private JPanel guiOverviewTabFunctions = new JPanel();

    public Editor() {
        HierarchyListener hierarchyListener = new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent event) {
                long flags = event.getChangeFlags();
                if ((flags & HierarchyEvent.SHOWING_CHANGED) == 
                        HierarchyEvent.SHOWING_CHANGED) {
                    splitPane.setDividerLocation(0.50D);
                }
            }

        };

//      guiEdit.setLineWrap(true);
//      guiEdit.setWrapStyleWord(true);

        splitPane.setLeftComponent(utilScrollbar);

        // Add tabs to the JTabbedPane
        guiOverviewTabPanel.add("Includes", guiOverviewTabIncludes);
        guiOverviewTabPanel.add("Functions", guiOverviewTabFunctions);

        splitPane.setRightComponent(guiOverviewTabPanel);

        splitPane.addHierarchyListener(hierarchyListener);
        add(splitPane);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        this.setMinimumSize(new Dimension(800, 400));

        setVisible(true);
    }
}