I would like to know what setOpaque() method do...
here is a part of small program:
public class Buttons extends JFrame implements ActionListener
{
private JButton button;
private JLabel label;
private JTextArea text;
private String t;
public Buttons()
{
super("TESTING");
label = new JLabel("Hello!!!!");
button = new JButton("Color Change");
text = new JTextArea("Test");
setLayout(new FlowLayout());
label.setOpaque(true);
add(button);
add(label);
add(text);
LabelHandler labelHandler = new LabelHandler();
button.addActionListener(this);
label.addMouseListener(labelHandler);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button)
{
label.setBackground(Color.red);
}
if (e.getSource()==text)
{
if (t == "\n")
{
setText(t);
label.getText();
}
}
}
class LabelHandler extends MouseAdapter
{
public void mouseEntered(MouseEvent e)
{
label.setBackground(Color.GREEN);
}
}
Without the setOpaque it wont paint the label. Why? thanks in advance.
The opaque flag is used by the Swing ComponentUI
to test whether they should paint their background or whether they should not. If you set your background color, but fail to setOpaque(true)
, you will not see that bg color.
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