简体   繁体   English

JTextArea未出现在JFrame中-Java

[英]JTextArea not appearing in JFrame - Java

I'm creating a simple GUI consisting of a full-window (J)TextArea. 我正在创建一个由全窗口(J)TextArea组成的简单GUI。 I've created a JFrame window and a JTextArea text area and set up both. 我创建了一个JFrame窗口和一个JTextArea文本区域,并设置了两者。 Additionally I created some colors and the font I want to use with the text area. 另外,我创建了一些颜色和要用于文本区域的字体。

Upon running the class, the window pops up as expected, however the text area is not there. 运行课程后,窗口将按预期方式弹出,但是文本区域不存在。

I have set the text area to visible, so that can't be a problem. 我已将文本区域设置为可见,所以这不是问题。

Code: 码:

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class GUI {

    public static void main(String[] args) {

        //create and set up window
        JFrame window = new JFrame("Console");

        window.setSize(800, 500);
        window.setVisible(true);
        window.setLocationRelativeTo(null);

        //create fonts and colors
        Color gray = new Color(34, 34, 34);
        Color lightGray = new Color(207, 191, 173);
        Font consolas = new Font("Consolas", Font.PLAIN, 15);

        //create and set up text area
        JTextArea text = new JTextArea();

        text.setSize(800, 500);
        text.setVisible(true);

        //text area font and colors
        text.setBackground(gray);
        text.setForeground(lightGray);
        text.setFont(consolas);

        text.append("Text");

    }   
}

And what results is a blank window named 'Console'. 结果是一个名为“控制台”的空白窗口。

How do I fix the JTextArea so that it shows? 如何修复JTextArea以使其显示?

You should add your JTextArea to the JFrame with the add- Method window.add(text); 您应该使用add-方法window.add(text);将JTextArea添加到JFrame window.add(text);

Another point make the frame visible after adding. 另外一点是添加后使框架可见。 window.setVisible(true); in the last line. 在最后一行。 Because sometimes there are strange layouterrors. 因为有时会出现奇怪的layouterrors。

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

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