用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();
}

}
}
}

解决方案 »

  1.   

    http://www.ibm.com/developerworks/cn/opensource/os-cn-swtimage2/
      

  2.   

    你不是写了paintComponent了吗?还有个repaint方法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(); }

    // 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();
    } }
    }
    }
      

  3.   


    现在是要把swing写的这段代码转成SWT的,就是Shell,Label控件的。