如何使用多个布局,面板之间没有空白?

时间:2015-08-05 16:23:02

标签: java layout layout-manager

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

在下面我使用Grid包布局来放置床图标和房间号码。之后会有更多的图标,我现在只添加了两个图标,只是为了显示它们将从页面开始的位置。

我提供了一张目前使用源代码的图片。

TLDR:我需要将图片中显示的床位于屏幕左上角,距离样本徽标底部约半英寸。

我试图添加图片,但我没有足够的声誉。如果你需要看,这里有一个链接。 http://imgur.com/DViRSuJ

上图中的源代码:

    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);

}

1 个答案:

答案 0 :(得分:1)

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

例如: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);