0

我在 Java 和 Swing GUI 中迈出了第一步。我的计划是制作一个带有 JTextArea 的小 GUI 来填充一些文本和两个选项卡式 JPanel。但问题是,当我在 JTextArea 中填写一些文本并单击选项卡“包含”(或其他选项卡“功能”)时,它会调整选项卡菜单的大小(它应该具有固定的 50:50 视图)。

下面是一些图片以便更好地理解:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

这是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

4

1 回答 1

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);
    }
}
于 2012-11-08T17:52:57.767 回答