gpt4 book ai didi

java - 如何使用多种布局,而面板之间没有空白?

转载 作者:行者123 更新时间:2023-12-01 11:15:44 29 4
gpt4 key购买 nike

我正在尝试使用多种布局(Border 和 Gridbag)来制作一个独立的可视化应用程序来显示恢复室的状态。我希望我的公司 Logo 位于左上角。在 Logo 的右侧,我希望以文本格式显示信息,例如可用床位、总入住人数等...

下面我使用网格袋布局来放置床图标以及房间号。稍后会有更多图标,我目前只添加了两个图标,只是为了显示它们在页面上的起始位置。

我已经提供了源代码当前外观的图像。

TLDR:我需要将图像中显示的床放置在屏幕的左上方,距示例 Logo 底部约半英寸。

我尝试添加图像,但我没有足够的声誉。如果您需要查看,这里有一个链接。 /image/xsInF.jpg

上图的源代码:

    public static void showUI() {

int bedCount = getBedCount();
bedCount = bedCount - 5;

Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());

ImageIcon maleBed = new ImageIcon("Images/Male.png");
ImageIcon femaleBed = new ImageIcon("Images/Female.png");
ImageIcon emptyBed = new ImageIcon("Images/empty.png");
ImageIcon logo = new ImageIcon("Images/logo-sample.png");
ImageIcon mlogo = new ImageIcon("Images/Medicalistics_Logo.png");

JFrame window = new JFrame("Ft Lauderdale");
window.setVisible(true);
window.setSize(xSize, ySize);

JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setBounds(900, 900, 900, 900);
mainPanel.setLayout(new BorderLayout());

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(mainPanel);

JPanel logoPane = new JPanel();
logoPane.setLayout(new BorderLayout());
logoPane.setBackground(Color.white);

mainPanel.add(logoPane, BorderLayout.NORTH);

JPanel bedsPane = new JPanel();
bedsPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

JLabel logoLabel = new JLabel(logo);

logoLabel.setHorizontalAlignment(JLabel.LEFT);
logoPane.add(logoLabel, BorderLayout.NORTH);
mainPanel.add(bedsPane, BorderLayout.CENTER);

JLabel bedLabel = new JLabel(emptyBed);

c.gridheight = 5;
c.gridwidth = 5;

c.gridx = 50;
c.gridy = 0;
bedsPane.add(bedLabel, c);

JLabel bedLabel2 = new JLabel(femaleBed);

c.gridx = 0;
c.gridy = 0;
bedsPane.add(bedLabel2, c);

}

最佳答案

对于面板周围的空白,您可能需要设置插图

例如:c.insets = new Insets(0, 0, 0, 0);

我不确定你想如何处理 2 布局,但你可以简单地使用 GridBagLayout 来实现你想要的效果。

您可以在这里看到我的测试代码:

package Main;

import java.awt.*;

import javax.swing.*;

public class dummyGUI {
private static JPanel infopane, bedsPane, logopane, mainPanel;
private static JLabel lmalebed, lfemalebed, logoLabel;

public dummyGUI() {
showUI();
}

public static void showUI() {

int bedCount = 7; // getBedCount();
bedCount = bedCount - 5;

Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());

ImageIcon maleBed = new ImageIcon("Images/Male.png");
ImageIcon femaleBed = new ImageIcon("Images/Female.png");
ImageIcon emptyBed = new ImageIcon("Images/empty.png");
ImageIcon logo = new ImageIcon("Images/logo-sample.png");
ImageIcon mlogo = new ImageIcon("Images/Medicalistics_Logo.png");

JFrame window = new JFrame("Ft Lauderdale");
window.setVisible(true);
window.setSize(xSize, ySize);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 897, 0 };
gridBagLayout.rowHeights = new int[] { 504, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
window.getContentPane().setLayout(gridBagLayout);

mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
GridBagConstraints gbc_mainPanel = new GridBagConstraints();
gbc_mainPanel.insets = new Insets(0, 0, 5, 0);
gbc_mainPanel.fill = GridBagConstraints.BOTH;
gbc_mainPanel.gridx = 0;
gbc_mainPanel.gridy = 0;
window.getContentPane().add(mainPanel, gbc_mainPanel);
GridBagLayout gbl_mainPanel = new GridBagLayout();
gbl_mainPanel.columnWidths = new int[] { 286, 518, 0 };
gbl_mainPanel.rowHeights = new int[] { 101, 367, 0 };
gbl_mainPanel.columnWeights = new double[] { 1.0, 2.0, Double.MIN_VALUE };
gbl_mainPanel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
mainPanel.setLayout(gbl_mainPanel);

logopane = new JPanel();
logopane.setBackground(Color.WHITE);
GridBagConstraints gbc_logopane = new GridBagConstraints();
gbc_logopane.insets = new Insets(0, 0, 0, 0);
gbc_logopane.fill = GridBagConstraints.BOTH;
gbc_logopane.gridx = 0;
gbc_logopane.gridy = 0;
mainPanel.add(logopane, gbc_logopane);
logopane.setLayout(new BorderLayout());

logoLabel = new JLabel(logo);
logopane.add(logoLabel, BorderLayout.NORTH);

logoLabel.setHorizontalAlignment(JLabel.LEFT);

infopane = new JPanel();
infopane.setBackground(Color.WHITE);
GridBagConstraints gbc_infopane = new GridBagConstraints();
gbc_infopane.insets = new Insets(0, 0, 0, 0);
gbc_infopane.fill = GridBagConstraints.BOTH;
gbc_infopane.gridx = 1;
gbc_infopane.gridy = 0;
mainPanel.add(infopane, gbc_infopane);
infopane.setLayout(new BorderLayout());

bedsPane = new JPanel();
GridBagConstraints gbc_bedsPane = new GridBagConstraints();
gbc_bedsPane.gridwidth = 2;
gbc_bedsPane.fill = GridBagConstraints.BOTH;
gbc_bedsPane.gridx = 0;
gbc_bedsPane.gridy = 1;
mainPanel.add(bedsPane, gbc_bedsPane);
GridBagLayout gbl_bedsPane = new GridBagLayout();
gbl_bedsPane.columnWidths = new int[] { 1, 0, 0, 0, 0 };
gbl_bedsPane.rowHeights = new int[] { 1, 0, 0, 0 };
gbl_bedsPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_bedsPane.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
bedsPane.setLayout(gbl_bedsPane);

lmalebed = new JLabel(maleBed);
GridBagConstraints gbc_lmalebed = new GridBagConstraints();
gbc_lmalebed.insets = new Insets(0, 0, 0, 0);
gbc_lmalebed.gridx = 1;
gbc_lmalebed.gridy = 1;
bedsPane.add(lmalebed, gbc_lmalebed);

lfemalebed = new JLabel(femaleBed);
GridBagConstraints gbc_lfemalebed = new GridBagConstraints();
gbc_lfemalebed.gridx = 3;
gbc_lfemalebed.gridy = 1;
bedsPane.add(lfemalebed, gbc_lfemalebed);

}

public static void main(String[] args) {
new dummyGUI();
}
}

这里的部分

    gbl_bedsPane.columnWidths = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
gbl_bedsPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_bedsPane.rowHeights = new int[] { 1, 0, 0, 0, 0 };
gbl_bedsPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };

是你可以知道行数和列数的地方。我没有设置足够的列,这就是为什么当您添加更多列时您会看到它出现在右侧。

我还做了一个像我在评论中所说的功能:

private static JLabel add_bed(String sex, int x, int y){
JLabel bed = null;

if (sex == "female"){
bed = new JLabel(femaleBed);
GridBagConstraints gbc_lfemalebed = new GridBagConstraints();
gbc_lfemalebed.insets = new Insets(0, 0, 0, 0);
gbc_lfemalebed.gridx = x;
gbc_lfemalebed.gridy = y;
bedsPane.add(lfemalebed, gbc_lfemalebed);
}else if (sex == "male"){
lmalebed = new JLabel(maleBed);
GridBagConstraints gbc_lmalebed = new GridBagConstraints();
gbc_lmalebed.insets = new Insets(0, 0, 0, 0);
gbc_lmalebed.gridx = x;
gbc_lmalebed.gridy = y;
bedsPane.add(lmalebed, gbc_lmalebed);
}


return bed;
}

那我就可以做

add_bed("female", 5 , 1);

关于java - 如何使用多种布局,而面板之间没有空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837922/

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