简体   繁体   English

JTextArea 不显示

[英]JTextArea doesn't show up

public class Server extends JFrame{
    private JLabel ipLabel;
    private JTextField ipTextField;
    private JLabel portLabel;
    private JTextField portTextField;

    private JTextArea messagesTextArea;

    public Server(String title){
        super(title);

        setLayout(new GridBagLayout());
        Container container = getContentPane();
        GridBagConstraints gc = new GridBagConstraints();

        ipLabel = new JLabel("IP Address: ");
        gc.gridx = 0;
        gc.gridy = 0;
        container.add(ipLabel, gc);

        ipTextField = new JTextField();
        ipTextField.setColumns(15);
        gc.gridx = 1;
        gc.gridy = 0;
        container.add(ipTextField, gc);

        portLabel = new JLabel("Port: ");
        gc.gridx = 2;
        gc.gridy = 0;
        container.add(portLabel, gc);

        portTextField = new JTextField();
        portTextField.setColumns(4);
        gc.gridx = 3;
        gc.gridy = 0;
        container.add(portTextField, gc);

            // I think this is the problem
        messagesTextArea = new JTextArea();
        gc.gridx = 0;
        gc.gridy = 1;
        container.add(messagesTextArea, gc);
    }
}

JTextArea doesn't show up. JTextArea 不显示。 I am using GridBagLayout.我正在使用 GridBagLayout。 This is my code.这是我的代码。 What's the problem?有什么问题?

Here's my main method.这是我的主要方法。

public class MainFrame {

    private static Server server;

    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                server = new Server("Server");
                server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                server.setBounds(dimension.width/4, dimension.height/4, dimension.width/2, dimension.height/2);
                server.setVisible(true);
            }
        });
    }
}

Always add JTextArea to JScrollPane otherwise it will create a problem when the no of rows in text area is greater than frame's height.始终将JTextArea添加到JScrollPane否则当文本区域中的行数大于框架的高度时会产生问题。

    JScrollPane jScrollPane=new JScrollPane(messagesTextArea);
    container.add(jScrollPane, gc);

and try并尝试

    gc.fill=GridBagConstraints.BOTH;
    gc.weighty=0.9;
    gc.weightx=0.8;

use weighty and weightx to set the size in percent of the component.使用weightyweightx以组件的百分比设置大小。

use fill to fill all the available space either horizontally or vertically or both.使用fill水平或垂直或同时填充所有可用空间。

For more info have a look at How to Use GridBagLayout有关更多信息,请查看如何使用 GridBagLayout

Instead of calling the empty constructor use JTextArea(int, int) to set the row and column size like this:不是调用空构造函数,而是使用JTextArea(int, int)来设置行和列的大小,如下所示:

// JTextArea with 1 row and 50 columns
messagesTextArea = new JTextArea(1,50);

the constructor without arguments creates a JTextArea with 0 rows and 0 columns hence you see nothing.没有参数的构造函数创建了一个具有 0 行和 0 列的 JTextArea,因此您什么也看不到。

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

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