java setOpaque() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3144207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setOpaque() method
提问by firestruq
I would like to know what setOpaque() method do...
我想知道 setOpaque() 方法是做什么的...
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.
没有 setOpaque 它不会绘制标签。为什么?提前致谢。
回答by akf
The opaque flag is used by the Swing ComponentUIto 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.
Swing 使用不透明标志ComponentUI来测试他们是否应该绘制背景。如果您设置了背景颜色,但没有设置setOpaque(true),您将看不到该背景颜色。
回答by cristis
Here's some info: http://mindprod.com/jgloss/setopaque.html

