swt与awt/swing互嵌

来源:互联网 发布:淘宝店铺级别分类 编辑:程序博客网 时间:2024/05/16 11:06

 

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();
    }
原创粉丝点击