SWING/SWT/AWT components are embedded in each other

Source: Internet
Author: User

SWT Embedding awt/swing Control:

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

Eclipse View Effects:

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

Using SWT in SWING/AWT

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's transparency effect (invalid for control)

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.