I have a swing application, and I've written code to change the background color of the JTextArea. However, it gives me exceptions.
Here is the code:
//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);
}
}
you need to move code line frame.setVisible(true);
as last code in void execute()
because you added JTextArea
to the already visible Swing GUI, that isn't builded on Initial Thread
another important:
rename public class TextArea {
to public class MyTextArea
{, because TextArea
is reserved Java word for awt.TextArea
TextArea x=new TextArea();
and x.execute()
; should be wrapped into invokeLater
, more to se in Oracle tutorial Initial Thread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With