【发布时间】:2015-08-10 16:26:40
【问题描述】:
我正在创建一个由全窗口 (J)TextArea 组成的简单 GUI。我创建了一个 JFrame 窗口和一个 JTextArea 文本区域并设置了两者。此外,我创建了一些颜色和我想在文本区域中使用的字体。
运行该类时,窗口按预期弹出,但文本区域不存在。
我已将文本区域设置为可见,所以这不是问题。
代码:
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");
}
}
结果是一个名为“控制台”的空白窗口。
如何修复 JTextArea 以使其显示?
【问题讨论】: