JTextField堆叠在JTextArea之上

时间:2016-01-12 22:43:55

标签: java swing gridbaglayout

我正在开发一个小型GUI项目,该项目可以构建一个书籍管理器来管理一本书。在我的布局中,我希望textArea显示有关我的图书的信息,JLabel下方的文字textAreaJTextArea,以便我输入同一列JLabel的信息1}}和Label旁边的1行。但是,当我运行我的程序时,textAreaLabel完全覆盖了我的textArea

如何解决此问题。谢谢!

import java.awt.*;
import javax.swing.*;

public class Design extends JFrame {

    JButton button1;
    JTextArea outputArea;
    JLabel stockLabel, bookPriceLabel;
    JTextField stockTextField, priceTextField;
    JPanel panel;
    GridBagConstraints gc;

    public Design(){
        super("Book Info");

        gc = new GridBagConstraints ();
        panel = new JPanel();
        outputArea = new JTextArea(20, 50);
        stockLabel = new JLabel ("Books In Stock");
        bookPriceLabel = new JLabel("Book Price");
        stockTextField = new JTextField(10);
        button1 = new JButton ("button1");

        panel.setLayout(new GridBagLayout());
        panel.setSize(600, 600);

        gc.anchor = GridBagConstraints.NORTHWEST;
        gc.weightx = 1;
        gc.weighty = 1;
        panel.add(outputArea, gc);

        gc.gridx = 2;
        gc.gridy = 2;
        panel.add(stockLabel, gc);

        gc.gridx = 3;
        gc.gridy = 2;
        panel.add(stockTextField, gc);

        this.getContentPane().add(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,600);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    public static void main (String args[]){
        Design frame = new Design();
    }
}

1 个答案:

答案 0 :(得分:3)

您可以对gridWidth应用JTextArea约束,以允许它跨越多列。

请记住,GridBagLayout仍然基于网格(行和列)的概念,但它提供了很大的灵活性来自定义组件填充和跨越网格的方式

也许像......

Layout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Design extends JFrame {

    JButton button1;
    JTextArea outputArea;
    JLabel stockLabel, bookPriceLabel;
    JTextField stockTextField, priceTextField;
    JPanel panel;
    GridBagConstraints gc;

    public Design() {
        super("Book Info");

        gc = new GridBagConstraints();
        panel = new JPanel();
        outputArea = new JTextArea(20, 50);
        stockLabel = new JLabel("Books In Stock");
        bookPriceLabel = new JLabel("Book Price");
        stockTextField = new JTextField(10);
        button1 = new JButton("button1");

        panel.setLayout(new GridBagLayout());

        gc.anchor = GridBagConstraints.NORTHWEST;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.weightx = 1;
        gc.weighty = 1;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(new JScrollPane(outputArea), gc);

        gc.weightx = 0;
        gc.weighty = 0;
        gc.anchor = GridBagConstraints.WEST;
        gc.gridwidth = 1;
        gc.gridx = 0;
        gc.gridy = 2;
        panel.add(stockLabel, gc);

        gc.gridx++;
        gc.gridy = 2;
        panel.add(stockTextField, gc);

        this.getContentPane().add(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Design frame = new Design();
            }
        });
    }
}

有关详细信息,请参阅How to Use GridBagLayout

一些一般性反馈:

  • 在一个组件上调用setSize没有意义,该组件将被添加到使用布局管理器的容器中,操作将被忽略,布局管理器将根据需要调用setSize使用
  • JTextArea确实应该包含在JScrollPane中,如果你不这样做,你会得到一些奇怪的结果(它会随着文本占用更多空间而增长)
  • 首选JFrame#pack超过JFrame#setSize,这将考虑框架边框,围绕内容包装框架
  • 你真的应该避免直接从顶级容器扩展,如JFrame,除了将你锁定到一个用例,你真的没有添加任何新的功能到框架。相反,从像JPanel这样的东西开始,构建你的核心UI,然后,当你需要时,将它添加到你想要的容器中