JComponent.setBackground()没有用?
java doc里面是这么写的
setBackground
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it.
代码:
然后背景颜色还是没有变
在代码中 ‘1’处加上 cmp.setOpaque(true); 也没有用 (貌似要求都符合了--opaque;override paintComponent() )
如果不是JComponet 而是JPanel 就没有问题
那么如果我想用JComponent 怎么设置背景颜色呢?
setBackground
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it.
代码:
// the main() method is ignored.
public MyFrame extends JFrame
{ //set ...
MyComponent cmp=new MyComponent();
// '1'
add(cmp);
}
class MyComponent extends JComponent
{ public void paintComponent(Graphics gr)
{ setBackground(Color.RED);
}
}
然后背景颜色还是没有变
在代码中 ‘1’处加上 cmp.setOpaque(true); 也没有用 (貌似要求都符合了--opaque;override paintComponent() )
如果不是JComponet 而是JPanel 就没有问题
那么如果我想用JComponent 怎么设置背景颜色呢?
...全文
相关推荐
JComponent与ComponentUI深入详解
JComponent与ComponentUI之间的内在关系在于,每个JComponent对象都会被赋予一个对应的ComponentUI子类实例,这个实例负责实际的绘制操作。
jComponent:一个简单的jQuery组件库
本文介绍了jComponent JavaScript库的功能,包括组件化开发、数据绑定、事件处理、表单验证、DOM操作及插件系统。该库支持多种数据绑定语法,并提供丰富的API用于数据和界面交互。同时具
可视化组件:Swing JComponent总览
### 可视化组件:Swing JComponent 总览#### (一)基本交互组件:获取用户的输入或者显示状态的简单组件1.
Java SE程序 自定义主键JComponent
Java SE程序 自定义主键JComponentJava SE程序 自定义主键JComponentJava SE程序 自定义主键JComponentJava SE程序 自定义主键JComponent
JFrame设置背景色注意的事项
`方法**:在设置透明面板时,应避免使用`setBackground()`方法来设置颜色,因为这会导致面板不再透明。


Java SE
