gpt4 book ai didi

java - 如何正确设置 jpanels 的大小?

转载 作者:行者123 更新时间:2023-12-01 18:01:50 25 4
gpt4 key购买 nike

我有两个 jpanel:jpanel1 和 jpanel2,它们必须根据其内容具有正确的最小尺寸。 jpanel0 是这两个面板的容器,它必须位于框架的左侧。这里是 jpanel3,它应该占据右侧剩余的可用空间。

如何将 jpanel 的大小设置为所有可用空间?

我想要的输出: enter image description here

我当前的输出: enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Panels {

public static void main(String[] args) {

JFrame myFrame = new JFrame();
myFrame.setLayout(new BorderLayout());

JTabbedPane jtp = new JTabbedPane();

JPanel jPaneTab1 = new JPanel();
jPaneTab1.setLayout(new FlowLayout(FlowLayout.LEFT));

JPanel jpanel0 = new JPanel();
jpanel0.setLayout(new BoxLayout(jpanel0, BoxLayout.Y_AXIS));
jpanel0.setBorder(BorderFactory.createTitledBorder("jpanel0"));
jpanel0.setBackground(Color.RED);


JPanel jpanel1 = new JPanel();
jpanel1.setLayout(new GridBagLayout());
jpanel1.setBorder(BorderFactory.createTitledBorder("jpanel1"));
GridBagConstraints gc = new GridBagConstraints();
jpanel1.setBackground(Color.BLUE);

JLabel jlabel1 = new JLabel("jlabel1");
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.NORTHWEST;
gc.insets = new Insets(0, 0, 0, 2);
jpanel1.add(jlabel1, gc);
JLabel jlabel2 = new JLabel("jlabel2");
gc.gridx = 0;
gc.gridy = 1;
gc.anchor = GridBagConstraints.NORTHWEST;
gc.insets = new Insets(0, 0, 0, 2);
jpanel1.add(jlabel2, gc);


JPanel jpanel2 = new JPanel();
jpanel2.setLayout(new GridBagLayout());
jpanel2.setBorder(BorderFactory.createTitledBorder("jpanel2"));
GridBagConstraints gc2 = new GridBagConstraints();
jpanel1.setBackground(Color.BLUE);

JLabel jlabel3 = new JLabel("jlabel3");
gc2.gridx = 0;
gc2.gridy = 0;
gc2.anchor = GridBagConstraints.NORTHWEST;
gc2.insets = new Insets(0, 0, 0, 2);
jpanel2.add(jlabel3, gc2);
JLabel jlabel4 = new JLabel("jlabel4");
gc2.gridx = 0;
gc2.gridy = 1;
gc2.anchor = GridBagConstraints.NORTHWEST;
gc2.insets = new Insets(0, 0, 0, 2);
jpanel2.add(jlabel4, gc2);






JPanel jpanel3 = new JPanel();
jpanel3.setBackground(Color.YELLOW);
JLabel jlabel5 = new JLabel("jpanel3");
jpanel3.add(jlabel5);

jpanel0.add(jpanel1);
jpanel0.add(jpanel2);
jPaneTab1.add(jpanel0, BorderLayout.WEST);
jPaneTab1.add(jpanel3, BorderLayout.CENTER);

JPanel jPaneTab2 = new JPanel();


jtp.addTab("tab1", jPaneTab1);
jtp.addTab("tab2", jPaneTab2);

myFrame.add(jtp);
myFrame.setSize(800, 600);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}

}

添加:

当我将 BorderLayout 用于主(选项卡面板)时,我遇到了另一个问题: enter image description here

最佳答案

当您将布局分解为逻辑面板,然后使用不同的布局管理器嵌套面板时,布局会更容易工作。

例如,使用带有 BorderLayout 的面板作为选项卡式 Pane 的内容。

然后创建一个“骑行侧面板”并将其添加到此面板和一个“中心面板”

JPanel main = new JPanel( new BorderLayout() );
JPanel rightSide = new JPanel( ... );
JPanel center = new JPanel(...);
main.add(rightSide, BorderLayout.LINE_START);
main.add(center, BorderLayout.CENTER);

然后设置“rightSide”和“center”面板的布局,并向每个面板添加组件。

关于java - 如何正确设置 jpanels 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com