简体   繁体   English

JTextField堆叠在JTextArea的顶部

[英]JTextField is stacking on top of JTextArea

I'm working on a small GUI project that builds a book manger to mange a book. 我正在做一个小型的GUI项目,该项目建立了一个书架来管理一本书。 In my layout, I want a textArea to display information about my book, a text JLabel underneath my textArea and a JTextArea for me to input information same column of JLabel and 1 row next to Label . 在我的布局,我希望有一个textArea显示我的书,文本信息JLabel我下面textAreaJTextArea对我的输入信息,同一列JLabel和1列旁边Label However when I run my program, textArea and Label is completely cover over my textArea . 但是,当我运行程序时, textAreaLabel完全覆盖了我的textArea

How can I fix this problem. 我该如何解决此问题。 Thanks!! 谢谢!!

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

You could apply a gridWidth constraint to the JTextArea which would allow it to span multiple columns. 您可以将gridWidth约束应用于JTextArea ,这将使其跨多列。

Remember, GridBagLayout is still based around the concept of a grid (rows and columns), but which provides a lot of flexibility to customise how components fill and span through out that grid 请记住, GridBagLayout仍然基于网格(行和列)的概念,但是它提供了很大的灵活性来自定义组件如何填充和扩展整个网格

Maybe something like... 也许像...

布局

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

See How to Use GridBagLayout for more details 有关更多详细信息,请参见如何使用GridBagLayout

Some general feedback: 一些一般性的反馈:

  • There's no point in calling setSize on a component which will be added to a container which using a layout manager, the action will simply be ignored and the layout manager will call setSize with what it wants to use 在使用布局管理器添加到容器中的组件上调用setSize毫无意义,该操作将被简单地忽略,并且布局管理器将使用其要使用的内容调用setSize
  • JTextArea really should be contained within a JScrollPane , you'll get some weird results if you don't (it will want to grow as the text occupies more space) JTextArea实际上应该包含在JScrollPane ,如果不包含,则会得到一些奇怪的结果(随着文本占用更多空间,它会不断增长)
  • Prefer JFrame#pack over JFrame#setSize , this will take into account the frame borders, wrapping the frame around the contents 优先使用JFrame#pack不是JFrame#setSize ,这将考虑框架边框,将框架环绕内容
  • You really should avoid extending directly from top level containers like JFrame , apart from locking you into a single use case, you're really not adding any new functionality to the frame. 您确实应该避免直接从诸如JFrame类的顶级容器进行扩展,除了将您锁定在单个用例之外,您实际上并没有在框架中添加任何新功能。 Instead, start with something like a JPanel , build you core UI ontop of that and then, when you need to, add that to what ever container you want 相反,从JPanel类的东西开始,在此之上构建您的核心UI,然后在需要时将其添加到所需的任何容器中

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

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