无法在GridBagLayout中正确放置标签

时间:2014-10-26 23:04:01

标签: java user-interface gridbaglayout

我正在尝试构建一个使用gridbaglayout的gui,因此我可以在特定位置放置标签和按钮。目前这是我的计划给我的:

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

这是我的目标: http://i59.tinypic.com/fu5lig.jpg

基本上,我想将退出按钮移动到东南角,图片向左移动,标签稍微向右移动。我已经尝试更改图片和标签的gridx和gridy,但图片不再向左移动,我将标签向右移动一点,图片也向右移动。这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

GridBagLayout需要的不仅仅是指定的x和y坐标,以做它最擅长的事情。在继续之前,你真的应该阅读并理解How To Use GridBagLayout Tutorial。正如教程本身所声明的那样," GridBagLayout是Java平台提供的最灵活,最复杂的布局管理器之一。"

不要为您的组件使用相同的GridBagConstraints实例 - 这可能会导致问题。至少有一个这样的实例应该指定weightxweighty值,否则您的组件将在容器的中心聚集在一起。使用gridwidthgridheight指定您的组件将占用的列数/行数(例如,您的图片应跨越所有标签)。使用Insets定义组件周围应留出多少空间。使用anchor将组件放置在其单元格中。

这是一个例子(我真的不明白你想做什么,但它应该让你开始 - 如果你阅读教程):

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

    }
}