简体   繁体   中英

Cannot get my placement of labels right in GridBagLayout

I am trying to build a gui that uses gridbaglayout, so I can place labels and buttons at specific positions. Currently this is what my program gives me:

http://i60.tinypic.com/2dmcj0n.jpg

This is what I am aiming to get: http://i59.tinypic.com/fu5lig.jpg

Basically, I want to move the logout button to the southeast corner and the picture more towards the left and the labels a little towards right. I have tried changing the gridx and gridy of the picture and the labels but the picture doesn't move any more left and I move the labels a little to the right, the picture moves to the right as well. Here is my code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.*;
public class Library{
    private static JFrame frame;
    private GridBagConstraints padding;
    private JLabel addB;
    private JTextField aB;
    private JLabel issueB;
    private JTextField iB;
    private JLabel holdB;
    private JTextField hB;
    private JLabel renewB;
    private JTextField rB;
    private JButton logout;
    private ImageIcon logo;

    public Library(){
        frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        padding = new GridBagConstraints();
        frame.setBackground(Color.RED);
    }



    //deals with the adding of textfield and label of adding book
    private void addBLabels()
    {
        addB = new JLabel("Add Book: ");
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 0;
        frame.add(addB, padding);
        padding.gridwidth = 30;
        aB = new JTextField(30);
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 0;
        frame.add(aB, padding);
    }

    //deals with issue book labels
    private void issueBLabels(){
        issueB = new JLabel("Issue Book: ");
        padding.gridwidth = 1;
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 1;
        frame.add(issueB, padding);
        padding.gridwidth = 30;
        iB = new JTextField(30);
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 1;
        frame.add(iB, padding);
    }

    //deals with holdbook labels
    private void holdBookLabels(){
        holdB = new JLabel("Hold Book: ");
        padding.gridwidth = 1;
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 2;
        frame.add(holdB, padding);
        hB = new JTextField(30);
        padding.gridwidth = 30;
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 2;
        frame.add(hB, padding);
    }

    //deals with the renewbook labels
    private void renewBookLabels(){
        renewB = new JLabel("Renew Book: ");
        padding.gridwidth = 1;
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 3;
        frame.add(renewB, padding);
        rB = new JTextField(30);
        padding.gridwidth = 30;
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 3;
        frame.add(rB, padding);
    }

    //deals with adding the logout button
    private void logOutButton(){
        logout = new JButton("Logout");
        padding.gridwidth = 1;
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 5;
        frame.add(logout, padding);
    }

    //deals with adding the image
    private void addImage() throws IOException{
        InputStream imageStream = this.getClass().getResourceAsStream("0521-1005-0822-0024_brunette_girl_smiling_and_holding_a_stack_books.jpg");
        BufferedImage image = ImageIO.read(imageStream);
        JLabel picLabel = new JLabel(new ImageIcon(image));
        padding.fill = GridBagConstraints.NORTHEAST;
        padding.gridx = 0;
        padding.gridy = 0;
        frame.add(picLabel, padding);
        frame.pack();
    }


    public static void main(String args[]) throws IOException{
        Library gui = new Library();
        gui.addBLabels();
        gui.issueBLabels();
        gui.holdBookLabels();
        gui.renewBookLabels();
        gui.logOutButton();
        gui.addImage();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(1000,500);
        frame.setVisible(true);     
}
}

GridBagLayout requires much more than just x and y coordinates specified to do what it does best. You really should read and understand the How To Use GridBagLayout Tutorial before proceeding. As the tutorial itself declares, "GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides."

Don't use the same instance of GridBagConstraints for your components - that might lead to problems. At least one such instance should specify the weightx and weighty values, otherwise your components are going to be clumped together in the center of the container. Use gridwidth and gridheight to specify how many columns/rows your component will occupy (for example your picture should span across all your labels). Use Insets to define how much space should be left around your components. Use anchor to place components within their cells.

Here's an example (did not really understand what you wanted to do, but it should get you started - if you read the tutorial):

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Library {

    private static JFrame frame;
    private JLabel addB;
    private JTextField aB;
    private JLabel issueB;
    private JTextField iB;
    private JLabel holdB;
    private JTextField hB;
    private JLabel renewB;
    private JTextField rB;
    private JButton logout;
    private ImageIcon logo;

    public Library() {
        frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        frame.setBackground(Color.RED);
    }

    //deals with the adding of textfield and label of adding book
    private void addBLabels() {
        addB = new JLabel("Add Book: ");
        GridBagConstraints padding = new GridBagConstraints();
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 0;
        frame.add(addB, padding);
        padding = new GridBagConstraints();
        aB = new JTextField(30);
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 0;
        frame.add(aB, padding);
    }

    //deals with issue book labels
    private void issueBLabels() {
        issueB = new JLabel("Issue Book: ");
        GridBagConstraints padding = new GridBagConstraints();
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 1;
        frame.add(issueB, padding);
        padding = new GridBagConstraints();
        iB = new JTextField(30);
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 1;
        frame.add(iB, padding);
    }

    //deals with holdbook labels
    private void holdBookLabels() {
        holdB = new JLabel("Hold Book: ");
        GridBagConstraints padding = new GridBagConstraints();
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 2;
        frame.add(holdB, padding);
        padding = new GridBagConstraints();
        hB = new JTextField(30);
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 2;
        frame.add(hB, padding);
    }

    //deals with the renewbook labels
    private void renewBookLabels() {
        renewB = new JLabel("Renew Book: ");
        GridBagConstraints padding = new GridBagConstraints();
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 4;
        padding.gridy = 3;
        frame.add(renewB, padding);
        padding = new GridBagConstraints();
        rB = new JTextField(30);
        padding.fill = GridBagConstraints.HORIZONTAL;
        padding.gridx = 5;
        padding.gridy = 3;
        frame.add(rB, padding);
    }

    //deals with adding the logout button
    private void logOutButton() {
        logout = new JButton("Logout");
        GridBagConstraints padding = new GridBagConstraints();
        padding.gridx = 5;
        padding.gridy = 5;
        padding.anchor = GridBagConstraints.LAST_LINE_END;
        padding.insets = new Insets(5, 0, 5, 5);
        frame.add(logout, padding);
    }

    //deals with adding the image
    private void addImage() throws IOException {
        JLabel picLabel = new JLabel(UIManager.getIcon("OptionPane.questionIcon"));
        GridBagConstraints padding = new GridBagConstraints();
        padding.fill = GridBagConstraints.VERTICAL;
        padding.gridx = 0;
        padding.gridy = 0;
        padding.weighty = 1.0d;
        padding.weightx = 1.0d;
        padding.gridheight = 6;
        padding.anchor = GridBagConstraints.LINE_START;
        padding.insets = new Insets(10, 10, 10, 10);
        frame.add(picLabel, padding);
        frame.pack();
    }

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

            public void run() {
                Library gui = new Library();
                gui.addBLabels();
                gui.issueBLabels();
                gui.holdBookLabels();
                gui.renewBookLabels();
                gui.logOutButton();
                try {
                    gui.addImage();
                } catch (IOException ex) {
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });

    }
}

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