怎么实现SWT窗口间的数据交流? 本帖最后由 dddddz 于 2010-10-23 21:14:37 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不难吧,无非就一个Dialog,有返回值的就行了。 请看代码。主应用类(含主窗口)import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;public class MainApp { protected Shell shell; Label label; InputDialog dialog; public static void main(String[] args) { try { MainApp window = new MainApp(); window.open(); } catch (Exception e) { e.printStackTrace(); } } public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } protected void createContents() { shell = new Shell(); shell.setSize(450, 300); shell.setText("mainFrame"); label = new Label(shell, SWT.NONE); label.setBounds(10, 21, 106, 12); label.setText("New Label"); Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { dialog = new InputDialog(shell); dialog.open(); label.setText(dialog.value); } }); button.setBounds(10, 50, 72, 22); button.setText("New Button"); }}subFrame(对话框)类import org.eclipse.jface.dialogs.Dialog;import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;public class InputDialog extends Dialog { Text input; String value = ""; protected InputDialog(Shell parentShell) { super(parentShell); } @Override protected Control createDialogArea(Composite parent) { input = new Text(parent, SWT.BORDER); return super.createDialogArea(parent); } @Override protected void okPressed() { value = input.getText(); this.close(); super.okPressed(); }} Swing的JPanel、JFrame...就完全可以实现 给你以前弄的一个“判断唯一可译码”的demoimport java.util.*;import javax.swing.JOptionPane;public class Textlist { static boolean flag = true; // 看两个集合是否还要继续比较,加入后缀的标志。 static boolean result = true; // 唯一可译码的标志。 static String cstring = new String(); // 用来引用输入的字符。 static ArrayList<String> ccString = new ArrayList<String>(); // 用来存储输入的码符号。 static int c; // 用来记录输入的信源的个数。 static String endString;// 用来引用要加入mylist中的后缀。 // 信源个数的输入,码符号的输入函数。 public static void inputString() { ccString.clear(); cstring = JOptionPane.showInputDialog("请输入信源的个数"); c = Integer.parseInt(cstring); for (int i = 0; i < c; i++) { cstring = JOptionPane.showInputDialog("请输入第" + (i + 1) + "个码符号"); ccString.add(cstring); } JOptionPane.showMessageDialog(null, ccString, "所有的码字是", JOptionPane.INFORMATION_MESSAGE); String check = JOptionPane.showInputDialog("所有的码字正确吗?" + "输入yes or no"); if (check.equals("no")) { Textlist.inputString(); } } // 比较两个字符串,若相等,即不可能是唯一可译码,使result为假。 // 若满足前缀关系,则返回后缀。 public static String comparestring(String str1, String str2) { String theResult = null; if (str1.equals(str2)) { result = false; } if (result) { if (str1.startsWith(str2)) { theResult = str1.substring(str2.length(), str1.length()); } if (str2.startsWith(str1)) { theResult = str2.substring(str1.length(), str2.length()); } } return theResult; } // 比较两个集合。若有相同的元素,则不是唯一可译码。使result为假。 // 若其中有元素满足前缀关系。则提取后缀添加到mylist中。 public static void com(ArrayList a, ArrayList b) { flag = false; String aa; String bb; cp2: for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { aa = (String) a.get(i); bb = (String) b.get(j); endString = comparestring(aa, bb); if (!result) { break cp2; } if (endString != null) { if (!b.contains(endString)) { b.add(endString); flag = true; break cp2; } } } } if (flag) { com(a, b); } } public static void main(String args[]) { try { String st1, st2; // boolean addString=false; Textlist test = new Textlist(); ArrayList mylist = new ArrayList(); test.inputString();// cp: for (int m = 0; m < test.c; m++) { for (int j = m + 1; j < test.c; j++) { st1 = (String) ccString.get(m); st2 = (String) ccString.get(j); endString = comparestring(st1, st2); if (!result) { break cp; } if (endString != null) { /* * addString=true; for( int k=0;k<mylist.size();k++) { * if(endString.equals(mylist.get(k))) { * addString=false; } } if(addString) { * mylist.add(endString); } */ if (!mylist.contains(endString)) { mylist.add(endString); } } } } test.com(test.ccString, mylist); if (result) { JOptionPane.showMessageDialog(null, "是唯一可译码。", "判断是否是唯一可译码", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "不是唯一可译码。", "判断是否是唯一可译码", JOptionPane.INFORMATION_MESSAGE); } // System.out.println(mylist); System.exit(0); } catch (Exception e) { System.exit(0); } }}// 测试码可以为:6 {01,111,011,00,010,110}或 7 {a,ac,ad,abb,bad,deb,bbcde}// the result is yes and not. SQL问题 JAVA电子词典? 关于jsplitpane的问题,up有分,多谢啦~ 一道题,我想了几天也没想出来,有兴趣的来试试 JTable中实现undo 请教!! 很多组件的构造函数中有int型参数,可很多时候用RIGHT之类的,是宏吗? 强烈建议--熟悉j2me开发的同道中人把qq留下来!!! 谁来帮帮初学者,指点一条学习路线 如何将数据库中得到的数据,存储到二维数组中? java二维数组对象分配 java byte流的问题!!!急!
主应用类(含主窗口)
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;public class MainApp { protected Shell shell;
Label label;
InputDialog dialog; public static void main(String[] args) {
try {
MainApp window = new MainApp();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("mainFrame");
label = new Label(shell, SWT.NONE);
label.setBounds(10, 21, 106, 12);
label.setText("New Label");
Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialog = new InputDialog(shell);
dialog.open();
label.setText(dialog.value);
}
});
button.setBounds(10, 50, 72, 22);
button.setText("New Button"); }
}subFrame(对话框)类
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;public class InputDialog extends Dialog {
Text input;
String value = "";
protected InputDialog(Shell parentShell) {
super(parentShell);
} @Override
protected Control createDialogArea(Composite parent) {
input = new Text(parent, SWT.BORDER);
return super.createDialogArea(parent);
} @Override
protected void okPressed() {
value = input.getText();
this.close();
super.okPressed();
}
}
import javax.swing.JOptionPane;public class Textlist {
static boolean flag = true;
// 看两个集合是否还要继续比较,加入后缀的标志。
static boolean result = true;
// 唯一可译码的标志。
static String cstring = new String();
// 用来引用输入的字符。
static ArrayList<String> ccString = new ArrayList<String>();
// 用来存储输入的码符号。
static int c; // 用来记录输入的信源的个数。
static String endString;// 用来引用要加入mylist中的后缀。 // 信源个数的输入,码符号的输入函数。 public static void inputString() {
ccString.clear();
cstring = JOptionPane.showInputDialog("请输入信源的个数");
c = Integer.parseInt(cstring);
for (int i = 0; i < c; i++) {
cstring = JOptionPane.showInputDialog("请输入第" + (i + 1) + "个码符号");
ccString.add(cstring);
}
JOptionPane.showMessageDialog(null, ccString, "所有的码字是",
JOptionPane.INFORMATION_MESSAGE);
String check = JOptionPane.showInputDialog("所有的码字正确吗?" + "输入yes or no");
if (check.equals("no")) {
Textlist.inputString();
}
} // 比较两个字符串,若相等,即不可能是唯一可译码,使result为假。
// 若满足前缀关系,则返回后缀。
public static String comparestring(String str1, String str2) {
String theResult = null;
if (str1.equals(str2)) {
result = false;
}
if (result) {
if (str1.startsWith(str2)) {
theResult = str1.substring(str2.length(), str1.length());
}
if (str2.startsWith(str1)) {
theResult = str2.substring(str1.length(), str2.length());
}
}
return theResult;
} // 比较两个集合。若有相同的元素,则不是唯一可译码。使result为假。
// 若其中有元素满足前缀关系。则提取后缀添加到mylist中。
public static void com(ArrayList a, ArrayList b) {
flag = false;
String aa;
String bb;
cp2: for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
aa = (String) a.get(i);
bb = (String) b.get(j);
endString = comparestring(aa, bb);
if (!result) {
break cp2;
}
if (endString != null) {
if (!b.contains(endString)) {
b.add(endString);
flag = true;
break cp2;
}
}
}
}
if (flag) {
com(a, b);
}
} public static void main(String args[]) {
try {
String st1, st2;
// boolean addString=false;
Textlist test = new Textlist();
ArrayList mylist = new ArrayList();
test.inputString();//
cp: for (int m = 0; m < test.c; m++) {
for (int j = m + 1; j < test.c; j++) {
st1 = (String) ccString.get(m);
st2 = (String) ccString.get(j);
endString = comparestring(st1, st2);
if (!result) {
break cp;
}
if (endString != null) {
/*
* addString=true; for( int k=0;k<mylist.size();k++) {
* if(endString.equals(mylist.get(k))) {
* addString=false; } } if(addString) {
* mylist.add(endString); }
*/
if (!mylist.contains(endString)) {
mylist.add(endString);
}
}
}
}
test.com(test.ccString, mylist);
if (result) {
JOptionPane.showMessageDialog(null, "是唯一可译码。", "判断是否是唯一可译码",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "不是唯一可译码。", "判断是否是唯一可译码",
JOptionPane.INFORMATION_MESSAGE);
}
// System.out.println(mylist);
System.exit(0);
} catch (Exception e) {
System.exit(0);
}
}
}
// 测试码可以为:6 {01,111,011,00,010,110}或 7 {a,ac,ad,abb,bad,deb,bbcde}
// the result is yes and not.