gpt4 book ai didi

java - 摆脱JPanels之间的差距

转载 作者:搜寻专家 更新时间:2023-11-01 01:29:06 26 4
gpt4 key购买 nike

这是我的问题

enter image description here

如何消除内部矩形框的间隙(它实际上是中间 JPanel 的边框)?

外部矩形是一个扩展的 JCompoment。它内部包含三个 JPanel。他们每个人都使用 GridLayout。我什至尝试在大矩形中将 setHgap 设置为负值,但它没有任何改变。

编辑:抱歉,问题不清楚。我确实想要边框,但我不想要内边框和外边框之间的间隙。如果中间没有空隙,则整个矩形代表一个类图类。

最佳答案

你的意思可能是这样的:

enter image description here

import java.awt.*;
import javax.swing.*;

public class GridBadFrame {

private JFrame frame;
private JPanel pnlCenter;
private JPanel pnl1;
private JPanel pnl2;
private JPanel pnl3;

public GridBadFrame() {
pnl1 = new JPanel();
pnl1.setBackground(Color.red);
pnl2 = new JPanel();
pnl2.setBackground(Color.blue);
pnl3 = new JPanel();
pnl3.setBackground(Color.red);
JLabel lblWest = new JLabel();
lblWest.setPreferredSize(new Dimension(50, 150));
JLabel lblEast = new JLabel();
lblEast.setPreferredSize(new Dimension(50, 150));
JLabel lblNorth = new JLabel();
lblNorth.setPreferredSize(new Dimension(600, 50));
JLabel lblSouth = new JLabel();
lblSouth.setPreferredSize(new Dimension(600, 50));
pnlCenter = new JPanel();
pnlCenter.setBackground(Color.black);
pnlCenter.setLayout(new java.awt.GridLayout(3, 0, 10, 10));
pnlCenter.setPreferredSize(new Dimension(600, 400));
pnlCenter.add(pnl1);
pnlCenter.add(pnl2);
pnlCenter.add(pnl3);
frame = new JFrame();
frame.add(pnlCenter, BorderLayout.CENTER);
frame.add(lblNorth, BorderLayout.NORTH);
frame.add(lblSouth, BorderLayout.SOUTH);
frame.add(lblWest, BorderLayout.WEST);
frame.add(lblEast, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
GridBadFrame gridBadFrame = new GridBadFrame();
}
});
}
}

关于java - 摆脱JPanels之间的差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7161430/

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