gpt4 book ai didi

java - 想不出一个简单的 4 框 JPanel 布局

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

几个小时以来,我一直在尝试让 JPanel 在 Java 中包含此配置中的这 4 个其他面板(见图)

  • 蓝色方框的大小永远不会改变。
  • 白框不应改变高度,但可以变宽。
  • 深灰色框不应改变宽度,但可以变高。
  • 浅灰色框可以变高或变宽。

对我来说似乎很简单,前几天我在 C# 中完成了它,这是一件轻而易举的事。设置位置、宽度、高度,以及某个边是否锚定,搞定了,我开始喜欢 Java 而不是 C,直到我遇到这个。

我尝试了无数种 GridBagLayout 组合,多个嵌套的 BoxLayout 实例。它们似乎都在做非常奇怪的事情,比如让每个面板都变成一个 4 x 4 的小正方形,或者它们周围有疯狂的填充,或者那些需要随窗口调整大小的面板,不要。

是否有某种神奇的组合可以实现这一点? null 布局是否执行锚定或百分比尺寸。

我得到的最接近的是带有 GridBagLayout 的底部图像,它在加载时看起来不错,但在您调整窗口大小时会那样。

enter image description here

enter image description here

这是获取上述图像的代码

class MainPanel extends JPanel {
public MainPanel(){
this.setBackground(new Color(216,216,216));
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

JPanel topTitle = new JPanel();
topTitle.setPreferredSize(new Dimension(140, 40));
topTitle.setMinimumSize(new Dimension(140, 40));
topTitle.setBackground(new Color(174, 216, 249));
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
this.add(topTitle,c);

JPanel mainHeader = new JPanel();
mainHeader.setPreferredSize(new Dimension(1060, 40));
mainHeader.setMinimumSize(new Dimension(1060, 40));
mainHeader.setBackground(Color.WHITE);
c.gridx = 1;
c.gridy = 0;
this.add(mainHeader,c);

JPanel sideNav = new JPanel();
sideNav.setPreferredSize(new Dimension(140, 760));
sideNav.setMinimumSize(new Dimension(140, 760));
sideNav.setBackground(new Color(110,110,110));
c.gridx = 0;
c.gridy = 1;
this.add(sideNav,c);

JPanel dataPanel = new JPanel();
dataPanel.setPreferredSize(new Dimension(1060, 760));
dataPanel.setMinimumSize(new Dimension(1060, 760));
dataPanel.setBackground(new Color(216,216,216));
c.gridx = 1;
c.gridy = 1;
this.add(dataPanel,c);
}

最佳答案

最小尺寸的 GUI

GUI at minimum size

GUI 拉伸(stretch)得更宽和更高

GUI stretched wider & taller

这一切都是为了获得适当的调整大小权重和填充值..

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FourPanelLayout {

private JComponent ui = null;

FourPanelLayout() {
initUI();
}

public void initUI() {
if (ui!=null) return;

ui = new JPanel(new GridBagLayout());
// It appears you don't want space around the panels.
// If not, commment out or remove this line.
ui.setBorder(new EmptyBorder(4,4,4,4));

// create the panels, each with a transparent image to suggest a size
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.CYAN);
bluePanel.add(new JLabel(new ImageIcon(getTransparentImage(40, 20))));

JPanel darkGrayPanel = new JPanel();
darkGrayPanel.setBackground(Color.DARK_GRAY);
darkGrayPanel.add(new JLabel(new ImageIcon(getTransparentImage(40, 20))));

JPanel whitePanel = new JPanel();
whitePanel.setBackground(Color.WHITE);
whitePanel.add(new JLabel(new ImageIcon(getTransparentImage(40, 20))));

JPanel grayPanel = new JPanel();
grayPanel.setBackground(Color.GRAY);
grayPanel.add(new JLabel(new ImageIcon(getTransparentImage(360, 80))));

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.0f;
gbc.weighty = 0.0f;
gbc.gridx = 0;
gbc.gridy = 0;

ui.add(bluePanel, gbc);

gbc.weightx = .5f;
gbc.gridx = 1;
ui.add(whitePanel, gbc);

gbc.weighty = .5f;
gbc.gridy = 1;
ui.add(grayPanel, gbc);

gbc.weightx = 0f;
gbc.gridx = 0;
//gbc.gridy
ui.add(darkGrayPanel, gbc);
}

/* We use transparent images to give panels a natural size. */
private Image getTransparentImage(int w, int h) {
return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
}

public JComponent getUI() {
return ui;
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
FourPanelLayout o = new FourPanelLayout();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 想不出一个简单的 4 框 JPanel 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39240877/

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