简体   繁体   中英

How to use multiple layouts, without having blank space inbetween panels?

I'm trying to use multiple layouts (Border and Gridbag) to make a standalone visual application that shows the status of recovery rooms. I want my company logo to be in the top left corner. Directly to the right of the logo I want to have information shown in text format such as available beds, how many total occupants, ect...

Below that I am using the Grid bag layout to place bed icons as well as room numbers. There will be a lot more icons later on, I've only got two added at the moment just to show where they will start on the page.

I've provided an image of what it currently looks like with my source code.

TLDR: I need to beds shown in the image to be in the upper left part of the screen about half an inch from the bottom of the sample logo.

I tried to add an image, but I don't have enough reputation. here is a link to it if you need to see. http://imgur.com/DViRSuJ

Source code for image above:

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

}

Well for the blank around panels you probably need to set the insets

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

I am not sure how you want to handle 2 layout but you can do the effect you want with simply GridBagLayout.

You can see my test code here :

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

The part here

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

is where you can tell the number of row and column. I did not set enough column that is why you see it appear on the right when you add more.

I also did a function like i said in the comment :

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

Then i can just do

add_bed("female", 5 , 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM