关于awt和swing的一点小问题请教一下

why_rookie 2014-06-07 10:02:01
import java.awt.*;
import java.awt.event.*;

public class TankClient extends Frame {


int x = 50, y = 50;

Image offScreenImage = null;

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);

y += 5;
}

public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(800, 600);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, 800, 600);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}

public void lauchFrame() {
this.setLocation(400, 300);
this.setSize(800, 600);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}

private class PaintThread implements Runnable {

public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}


请问一下, 为什么当我用swing来写的时候效果很差, 我只是将frame改成JFrame, 还有一个问题就是, 为什么不能直接在JFrame上设置背景色呢, 谢谢了!
...全文
234 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
why_rookie 2014-06-09
  • 打赏
  • 举报
回复
引用 2 楼 A18767101172 的回复:
不能上背景色, this.setBackground(Color.GREEN);这个不是吗,既然可以setBackGround那肯定可以设置背景色,感觉你在教学视频吧,不过我觉得还是看书,结合动手,效果会更好一点
还是不行 ,不过还是谢谢啦
A18767101172 2014-06-07
  • 打赏
  • 举报
回复
不能上背景色,
this.setBackground(Color.GREEN);这个不是吗,既然可以setBackGround那肯定可以设置背景色,感觉你在教学视频吧,不过我觉得还是看书,结合动手,效果会更好一点
不是说好的吗 2014-06-07
  • 打赏
  • 举报
回复
可以吧,jar包里面设置
-江沐风- 2014-06-07
  • 打赏
  • 举报
回复
JFrame的层次结构和Frame不太一样,应该是比Frame麻烦多了; 你可以先了解一下;
this.setBackground(Color.GREEN);
还有我记得它好像重写的不是paint,而是paintComponent;

62,621

社区成员

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

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