简体   繁体   English

无法在GridBagLayout中正确放置标签

[英]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. 我正在尝试构建使用gridbaglayout的gui,因此可以将标签和按钮放置在特定位置。 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. GridBagLayout需要做更多的工作,而不仅仅是指定x和y坐标。 You really should read and understand the How To Use GridBagLayout Tutorial before proceeding. 在继续之前,您确实应该阅读并理解“ 如何使用GridBagLayout教程” As the tutorial itself declares, "GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides." 正如教程本身所言, “ GridBagLayout是Java平台提供的最灵活,最复杂的布局管理器之一。”

Don't use the same instance of GridBagConstraints for your components - that might lead to problems. 不要为您的组件使用相同的GridBagConstraints实例-这可能会导致问题。 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. 至少应有一个这样的实例指定weightxweighty值,否则您的组件将被聚集在容器的中央。 Use gridwidth and gridheight to specify how many columns/rows your component will occupy (for example your picture should span across all your labels). 使用gridwidthgridheight指定组件将占用多少列/行(例如,图片应跨越所有标签)。 Use Insets to define how much space should be left around your components. 使用Insets来定义组件周围应保留多少空间。 Use anchor to place components within their cells. 使用anchor将组件放置在其单元格中。

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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