我有一个swing 应用程序,我已经编写了代码来更改JTextArea 的背景颜色。但是,它给了我例外。
这是代码:
//1.JtextArea will work after maximize.
//2.on typing text,background will slowly transform to black line by line.
import java.awt.*;
import javax.swing.*;
public class TextArea {
JTextArea area;
JFrame frame;
public static void main(String args[])
{
TextArea x = new TextArea();
x.execute();
}
void execute()
{
frame = new JFrame();
frame.setVisible(true);
frame.setSize(600,600);
frame.setTitle("Temp Area");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
area = new JTextArea();
frame.add(area,BorderLayout.CENTER);
Color c = new Color(0,0,0,100);
area.setBackground(c);
}
}
Run Code Online (Sandbox Code Playgroud)
您需要将代码行移动frame.setVisible(true);为 void 中的最后一个代码execute()
因为您添加到了JTextArea已经可见的 Swing GUI,所以它不是建立在Initial Thread
另一个重要的:
重命名 public class TextArea {为public class MyTextArea{,因为TextArea这是 Java 的保留字awt.TextArea
TextArea x=new TextArea();和x.execute(); 应该包含在invokeLater, more to se 中Oracle tutorial Initial Thread
| 归档时间: |
|
| 查看次数: |
11084 次 |
| 最近记录: |