昨天晚上看到有人询问,回答一下。
因为JFrame继承自Frame,是父子关系,所以Frame不能强转为JFrame使用。但是,却有取巧的方法令SWT操作Swing~
//JFrame实例
JFrame frame = new JFrame();
//获得容器
Container container = frame.getContentPane();
//画布
Canvas canvas = new Canvas();
//容器加载画布
container.add(canvas,BorderLayout.CENTER);
//显示
frame.setVisible(true);
//获得Display
Display display = new Display();
//转换canvas不就得了~~~为什么能转换?看看Frame继承自谁就知道了……
Shell shell = SWT_AWT.new_Shell(display,canvas);
//布局
shell.setLayout(new FillLayout());
//SWT按钮加载
Button button = new Button(shell,SWT.PUSH);
button.setText("SWT使用JFrame");
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()){
display.sleep ();
}
}
另外,再说一种利用new_Frame的方式,先写一个ChartDirector例子,应用了两个类。
DemoModule .java
CakyDemo.java
我构建一个ShellSwingToSWT.java用作演示:
其实也没什么可说的,只不过利用转换后的Frame载入JFrame的 container罢了。
这两种方式都可以实现Swing在SWT中的显示
只不过第一种利用Swing窗体的hWnd实例,一种在新建窗体中显示Frame罢了。
关于AWT/Swing在SWT中显示的原理其实很简单,为此昨天还写了个在AWT/Swing实现SWT功能的例子,一会有时间整理下发上来。
因为JFrame继承自Frame,是父子关系,所以Frame不能强转为JFrame使用。但是,却有取巧的方法令SWT操作Swing~
//JFrame实例
JFrame frame = new JFrame();
//获得容器
Container container = frame.getContentPane();
//画布
Canvas canvas = new Canvas();
//容器加载画布
container.add(canvas,BorderLayout.CENTER);
//显示
frame.setVisible(true);
//获得Display
Display display = new Display();
//转换canvas不就得了~~~为什么能转换?看看Frame继承自谁就知道了……
Shell shell = SWT_AWT.new_Shell(display,canvas);
//布局
shell.setLayout(new FillLayout());
//SWT按钮加载
Button button = new Button(shell,SWT.PUSH);
button.setText("SWT使用JFrame");
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()){
display.sleep ();
}
}
另外,再说一种利用new_Frame的方式,先写一个ChartDirector例子,应用了两个类。
DemoModule .java
package
org.test.chartdir;
/**
*
*
*
*
*
* @author chenpeng
* @email:[email][email protected][/email]
* @version 0.1
*/
public interface DemoModule {
public String toString();
public int getNoOfCharts();
public void createChart(ChartDirector.ChartViewer viewer, int index);
}
/**
*
* Title: LoonFramework
*
*
* Description:
*
*
* Copyright: Copyright (c) 2007
*
*
* Company: LoonFramework
*
*
* @author chenpeng
* @email:[email][email protected][/email]
* @version 0.1
*/
public interface DemoModule {
public String toString();
public int getNoOfCharts();
public void createChart(ChartDirector.ChartViewer viewer, int index);
}
CakyDemo.java
package
org.test.chartdir;
import ChartDirector.ChartViewer;
import ChartDirector.PieChart;
/**
*
*
*
*
* @author chenpeng
* @email:[email][email protected][/email]
* @version 0.1
*/
public class CakyDemo implements DemoModule {
public String toString() { return "ChartDirector饼图"; }
public int getNoOfCharts() { return 2; }
public void createChart(ChartViewer viewer, int index)
{
int angle = 0;
boolean clockwise = true;
if (index != 0) {
angle = 90;
clockwise = false;
}
double[] data = {25, 18, 15, 12, 8, 30, 35};
String[] labels = {"Labor", "Licenses", "Taxes", "Legal", "Insurance",
"Facilities", "Production"};
PieChart c = new PieChart(280, 240);
c.setPieSize(140, 130, 80);
if (clockwise) {
c.addTitle("Start " + angle);
} else {
c.addTitle("Start " + angle);
}
c.setStartAngle(angle, clockwise);
c.set3D();
c.setData(data, labels);
c.setExplode(0);
viewer.setImage(c.makeImage());
viewer.setImageMap(c.getHTMLImageMap("clickable", "",
"title='{label}: US${value}K ({percent}%)'"));
}
}
import ChartDirector.ChartViewer;
import ChartDirector.PieChart;
/**
*
Title: LoonFramework
*
Description:
*
Copyright: Copyright (c) 2007
*
Company: LoonFramework
* @author chenpeng
* @email:[email][email protected][/email]
* @version 0.1
*/
public class CakyDemo implements DemoModule {
public String toString() { return "ChartDirector饼图"; }
public int getNoOfCharts() { return 2; }
public void createChart(ChartViewer viewer, int index)
{
int angle = 0;
boolean clockwise = true;
if (index != 0) {
angle = 90;
clockwise = false;
}
double[] data = {25, 18, 15, 12, 8, 30, 35};
String[] labels = {"Labor", "Licenses", "Taxes", "Legal", "Insurance",
"Facilities", "Production"};
PieChart c = new PieChart(280, 240);
c.setPieSize(140, 130, 80);
if (clockwise) {
c.addTitle("Start " + angle);
} else {
c.addTitle("Start " + angle);
}
c.setStartAngle(angle, clockwise);
c.set3D();
c.setData(data, labels);
c.setExplode(0);
viewer.setImage(c.makeImage());
viewer.setImageMap(c.getHTMLImageMap("clickable", "",
"title='{label}: US${value}K ({percent}%)'"));
}
}
我构建一个ShellSwingToSWT.java用作演示:
package
org.test.chartdir;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import ChartDirector.ChartViewer;
/**
*
*
*
*
*
* @author chenpeng
* @email:[email][email protected][/email]
* @version 0.1
*/
public class ShellSwingToSWT {
public static void main(String[] args) {
int width = 600;
int height = 400;
// ChartDirector测试
DemoModule demo = new CakyDemo();
JFrame frame = new JFrame(demo.toString());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(width, height);
for (int i = 0; i < demo.getNoOfCharts(); ++i) {
ChartViewer viewer = new ChartViewer();
demo.createChart(viewer, i);
frame.getContentPane().add(viewer);
}
// 获得容器
Container container = frame.getContentPane();
Display display = new Display();
Shell shell = new Shell(display, SWT.CLOSE);
shell.setSize(new Point(width, height));
shell.setLayout(new FillLayout());
shell.setText("ChartDirector in SWT");
// 新建swt组件
Composite composite = new Composite(shell, SWT.NO_BACKGROUND
| SWT.EMBEDDED);
// 注入组件,并返回为frame
Frame frm = SWT_AWT.new_Frame(composite);
// 加载容器
frm.add(container);
// 打开shell
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import ChartDirector.ChartViewer;
/**
*
* Title: LoonFramework
*
*
* Description:
*
*
* Copyright: Copyright (c) 2007
*
*
* Company: LoonFramework
*
*
* @author chenpeng
* @email:[email][email protected][/email]
* @version 0.1
*/
public class ShellSwingToSWT {
public static void main(String[] args) {
int width = 600;
int height = 400;
// ChartDirector测试
DemoModule demo = new CakyDemo();
JFrame frame = new JFrame(demo.toString());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(width, height);
for (int i = 0; i < demo.getNoOfCharts(); ++i) {
ChartViewer viewer = new ChartViewer();
demo.createChart(viewer, i);
frame.getContentPane().add(viewer);
}
// 获得容器
Container container = frame.getContentPane();
Display display = new Display();
Shell shell = new Shell(display, SWT.CLOSE);
shell.setSize(new Point(width, height));
shell.setLayout(new FillLayout());
shell.setText("ChartDirector in SWT");
// 新建swt组件
Composite composite = new Composite(shell, SWT.NO_BACKGROUND
| SWT.EMBEDDED);
// 注入组件,并返回为frame
Frame frm = SWT_AWT.new_Frame(composite);
// 加载容器
frm.add(container);
// 打开shell
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
其实也没什么可说的,只不过利用转换后的Frame载入JFrame的 container罢了。
这两种方式都可以实现Swing在SWT中的显示
只不过第一种利用Swing窗体的hWnd实例,一种在新建窗体中显示Frame罢了。
关于AWT/Swing在SWT中显示的原理其实很简单,为此昨天还写了个在AWT/Swing实现SWT功能的例子,一会有时间整理下发上来。