用swing做了例子,重写控件的paintComponent实现JLabel上重绘图形的例子,代码如下,现在想切换成SWT的界面,貌似SWT中没有paintComponent?怎么实现同样的功能?
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;public class MyFrame extends JFrame
{
public static void main(String[] args) throws Exception
{
new MyFrame();
}
public MyFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 250);
getContentPane().setLayout(null);// 这个要设置成 null
MyJLable1 label = new MyJLable1(50,Color.blue);
getContentPane().add(label);
setVisible(true);
}
}
class MyJLable1 extends JLabel implements Runnable
{
boolean run = false;
int X, Y, moveX, moveY,mx,my,size;
Color color;
Thread thread;
MyJLable1(int size,Color color)
{
X=0;
Y=0;
moveX = 1;
moveY=1;
this.size = size;
this.color = color;
setSize(size,size);
thread = new Thread(this);
thread.start();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
}
public void run()
{
int i =0;
while(true)
{
X = X + moveX * i++;// 重新计算X、Y坐标
Y = Y + moveY* i++;
setLocation(X,Y);
try
{
Thread.sleep(300); } catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;public class MyFrame extends JFrame
{
public static void main(String[] args) throws Exception
{
new MyFrame();
}
public MyFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 250);
getContentPane().setLayout(null);// 这个要设置成 null
MyJLable1 label = new MyJLable1(50,Color.blue);
getContentPane().add(label);
setVisible(true);
}
}
class MyJLable1 extends JLabel implements Runnable
{
boolean run = false;
int X, Y, moveX, moveY,mx,my,size;
Color color;
Thread thread;
MyJLable1(int size,Color color)
{
X=0;
Y=0;
moveX = 1;
moveY=1;
this.size = size;
this.color = color;
setSize(size,size);
thread = new Thread(this);
thread.start();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
}
public void run()
{
int i =0;
while(true)
{
X = X + moveX * i++;// 重新计算X、Y坐标
Y = Y + moveY* i++;
setLocation(X,Y);
try
{
Thread.sleep(300); } catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
解决方案 »
- 静态环境下不允许类的参数是泛型类型,实在不理解!
- 在jsp中调用dlgHelper.ChooseColorDlg会报错误....一样的代码在html中可以运行 为什么到jsp中就不可以了呢....
- 如何测试jdk源码 现在想测试String类
- FTPClient访问linux的vsftp看不到文件
- 为什么下面两句不能同时有效?只会创建其中一个。
- 拜求解法
- 当timer.schedule(oneTimerTask, date, PERIOD)第一次运行时,并不都在“date”时才启动,只要大于这个date的任何时刻它都可以启动,为什
- 我在CLASS中用NEW SOCKET()不会出问题,但是到APPLICATION中则会提示java.security.AccessControlException: access denied (java.net.
- 配置weblogic'pools出错请大家看看
- 求大神解答
- hashCode()
- 有关jdbc 的一个小例子,望牛人帮忙解决!!!
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;public class MyFrame extends JFrame {
public static void main(String[] args) throws Exception {
new MyFrame();
} public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 250); getContentPane().setLayout(null);// 这个要设置成 null MyJLable1 label = new MyJLable1(50, Color.blue);
getContentPane().add(label);
setVisible(true);
}
}class MyJLable1 extends JLabel implements Runnable {
boolean run = false;
int X, Y, moveX, moveY, mx, my, size;
Color color;
Thread thread; MyJLable1(int size, Color color) {
X = 0;
Y = 0;
moveX = 1;
moveY = 1;
this.size = size;
this.color = color;
setSize(size, size);
thread = new Thread(this);
thread.start(); }
// repaint
@Override
public void repaint() {
super.repaint();
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
} public void run() {
int i = 0;
while (true) {
X = X + moveX * i++;// 重新计算X、Y坐标
Y = Y + moveY * i++; setLocation(X, Y);
try {
Thread.sleep(300); } catch (InterruptedException e) {
e.printStackTrace();
} }
}
}
现在是要把swing写的这段代码转成SWT的,就是Shell,Label控件的。