我在 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