swt和awt之间的互调

SWT中嵌入awt/swing控件:

public   static   void  main(String[] args)  ... {
        
final Display display = new Display();
        
final Shell shell = new Shell(display,SWT.EMBEDDED);
        Frame frame 
= SWT_AWT.new_Frame(shell);
        
        frame.setLayout(
new BorderLayout());
        frame.setBackground(Color.blue);
        frame.add(
new JLabel("I'm swing label."),BorderLayout.NORTH);
        JButton btnClose
=new JButton("close");
        btnClose.addActionListener(
new ActionListener()...{
            
public void actionPerformed(ActionEvent e) ...{

                display.syncExec(
new Runnable()...{
                    
public void run()...{
                        shell.close();
                    }

                }
);
            }

        }
);        
        frame.add(btnClose,BorderLayout.SOUTH);
        Panel panel 
= new Panel();
        panel.setBackground(Color.orange);
        frame.add(panel,BorderLayout.CENTER); 

        shell.setSize(
400300);
        shell.open();
        
while (!shell.isDisposed()) ...{
            
while (!display.readAndDispatch()) ...{
                display.sleep();
            }

        }

        display.dispose();
    }

 

Eclipse视图效果:

     public   void  createPartControl(Composite parent)  ... {
        Composite p
=new Composite(parent,SWT.EMBEDDED);
        Frame frame 
= SWT_AWT.new_Frame(p);
        
        frame.setLayout(
new BorderLayout());
        frame.setBackground(Color.blue);
        frame.add(
new JLabel("I'm swing label."),BorderLayout.NORTH);
        JButton btnClose
=new JButton("close");
        frame.add(btnClose,BorderLayout.SOUTH);
        Panel panel 
= new Panel();
        panel.setBackground(Color.orange);
        frame.add(panel,BorderLayout.CENTER); 
    }

 

swing/awt中使用swt

 

     public   static   void  main(String[] args)  ... {
        
final JFrame frame = new JFrame();
        Canvas canvas 
= new Canvas();
        frame.getContentPane().add(canvas,BorderLayout.CENTER);
        frame.setVisible(
true);

        Display display 
= new Display();
        
final Shell shell = SWT_AWT.new_Shell(display,canvas);
        shell.setLayout(
new FillLayout());
        Button button 
= new Button(shell,SWT.PUSH);
        button.setText(
"SWT Button");
        button.addSelectionListener(
new SelectionListener()...{
            
public void widgetDefaultSelected(SelectionEvent e) ...{
            }


            
public void widgetSelected(SelectionEvent e) ...{
                SwingUtilities.invokeLater(
new Runnable()...{
                    
public void run()...{
                        frame.dispose();
                    }

                }
);
            }

            
        }
);
        frame.setSize(
400,300);
        
        
while (!shell.isDisposed()) ...{
          
if (!display.readAndDispatch())...{
            display.sleep ();
          }

        }

        display.dispose();
    }

 

SWT的透明效果(对控件无效)

 

     public   static   void  main(String[] args)  ... {
        
final Display display = new Display();
        
final Shell shell = new Shell(display,SWT.NO_BACKGROUND);
        shell.addPaintListener(
new PaintListener() ...{
            
public void paintControl(PaintEvent e) ...{
                GC gc 
= e.gc;
                ImageData imageData 
= new ImageData("qd2.jpg");
                imageData.alpha
=90;
                Image image 
= new Image(display, imageData);
                gc.drawImage(image,
0,0);
            }

        }
);
        shell.setSize(
400,300);
        shell.open();
        
while (!shell.isDisposed()) ...{
          
if (!display.readAndDispatch())...{
            display.sleep ();
          }

        }

        display.dispose();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值