Component的setBackGround方法无效果

wsy85 2010-08-24 01:13:55

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

/**
*
* @author wusy
*/
public class FrameTest {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class SimpleFrame extends JFrame {

public SimpleFrame() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

setSize(screenWidth / 2, screenHeight / 2);
setLocationByPlatform(true);

DrawComponent component= new DrawComponent();
component.setBackground(Color.ORANGE);//设置背景色
component.setForeground(Color.PINK);
add(component);

}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}


class DrawComponent extends JComponent
{

@Override
protected void paintComponent(Graphics g) {
Graphics2D g2= (Graphics2D)g;

Rectangle2D rect= new Rectangle2D.Double(100, 100, 200, 150);
g2.draw(rect);

Ellipse2D ellipse= new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.setPaint(Color.BLUE);
g2.fill(ellipse);

g2.draw(new Line2D.Double(100,100,300,250));

Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(rect.getCenterX(), rect.getCenterY(), rect.getCenterX()+150, rect.getCenterY()+150);
g2.draw(circle);

g2.setPaint(Color.RED);
g2.drawString("红色", 0, 10);
}

}



运行后,背景色没有任何效果,为什么呀
...全文
152 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gentalguo 2010-08-24
  • 打赏
  • 举报
回复 1
第一,你需要一个UI
第二,你必须将其设置为非透明。
第三,调用super的paintComponent方法。
希望你能理解为什么。


public class FrameTest {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
}
});
}
}

class SimpleFrame extends JFrame {

public SimpleFrame() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

setSize(screenWidth / 2, screenHeight / 2);
setLocationByPlatform(true);

DrawComponent component = new DrawComponent();
component.setBackground(Color.ORANGE);// 设置背景色
component.setForeground(Color.PINK);
getContentPane().add(component);

}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}

class DrawComponent extends JComponent {
public DrawComponent() {
super();
setUI(new ComponentUI() {
});
setOpaque(true);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

Rectangle2D rect = new Rectangle2D.Double(100, 100, 200, 150);
g2.draw(rect);

Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.setPaint(Color.BLUE);
g2.fill(ellipse);

g2.draw(new Line2D.Double(100, 100, 300, 250));

Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(rect.getCenterX(), rect.getCenterY(), rect
.getCenterX() + 150, rect.getCenterY() + 150);
g2.draw(circle);

g2.setPaint(Color.RED);
g2.drawString("红色", 0, 10);
}

}
gentalguo 2010-08-24
  • 打赏
  • 举报
回复
当然,你没有调用super.PaintComponent(g);也是有问题的。
这个必须调用,除非你能彻底地把jdk框架里面的原有东西替换掉。你必须知道你究竟要那些不要那些。
但是,就你提的这个问题来说,有三点,解决之后,就可以看到背景色。
gentalguo 2010-08-24
  • 打赏
  • 举报
回复
因为没有UI
继承自JComponent的组件都有其自己的UI。JComponent只有一个ComponentUI,且是一个抽象类,其他继承自JComponent的UI钧继承自ComponentUI。
JComponent是一个抽象类。不推荐继承。
如果有必要,有需要使用颜色字体等,就需要自己给它加上ui。
Tassdars 2010-08-24
  • 打赏
  • 举报
回复
楼主把DrawComponent改为继承JPanel,然后paintComponent方法最前面加上一句super.paintComponent(g);就行了,可能是JComponent默认不绘制背景色。

其实觉得楼主不用这么费劲,都已经覆盖了paintComponent方法了,背景就自己画算了呗,不过就是个和组件宽高相同、坐标为(0,0)的矩形嘛
Tassdars 2010-08-24
  • 打赏
  • 举报
回复
由外观决定是否遵守此属性,某些外观可以选择忽略它。

这是文档里面的节选

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧