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(400, 300);
shell.open();
while (!shell.isDisposed()) ...{
while (!display.readAndDispatch()) ...{
display.sleep();
}
}
display.dispose();
}
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(400, 300);
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);
}
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();
}
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();
}
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();
}