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();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章