frm1中有txtName,frm2有一個button的值為"dream"
點擊frm2的按鈕,frm1中的txtName的值為“dream”
怎樣實現??请根据这2个代码文件帮帮忙实现功能
frm1代码:
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;public class frm1 { private Shell sShell = null;
private Text txtName = null;
private Label label = null;
private Button button = null;

    frm1 g=new frm1();
public static void main(String[] args) {
Display display = Display.getDefault();
frm1 thisClass = new frm1();
thisClass.createSShell();
thisClass.sShell.open(); while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private void createSShell() {
sShell = new Shell();
sShell.setText("Shell");
sShell.setSize(new Point(300, 200));
sShell.setLayout(null);
txtName = new Text(sShell, SWT.BORDER);
txtName.setBounds(new Rectangle(78, 29, 116, 24));
label = new Label(sShell, SWT.NONE);
label.setBounds(new Rectangle(21, 29, 51, 25));
label.setFont(new Font(Display.getDefault(), "\u5b8b\u4f53", 11, SWT.NORMAL));
label.setText("姓名:");
button = new Button(sShell, SWT.NONE);
button.setBounds(new Rectangle(187, 112, 68, 29));
button.setText("OPEN");
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
frm2 a=new frm2();
a.open();
}
});
}}frm2代码:
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;public class frm2 { private Shell sShell = null;
private Button button = null;
private void createSShell() {
sShell = new Shell();
sShell.setText("Shell");
sShell.setSize(new Point(300, 200));
sShell.setLayout(null);
button = new Button(sShell, SWT.NONE);
button.setBounds(new Rectangle(53, 41, 79, 34));
button.setText("dream");
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

}
});
} public void open() {
Display display = Display.getDefault();
frm2 thisClass = new frm2();
thisClass.createSShell();
thisClass.sShell.open(); while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();

}}

解决方案 »

  1.   

    代码给你改好了,能实现你说的功能,你的代码问题不少,以后又时间再给你一点点建议。
    你先好好看看swt编程基础。
    package csdn;import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Text;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.graphics.Font;
    import org.eclipse.swt.widgets.Button;public class frm1 { private Shell sShell = null; private Text txtName = null; private Label label = null; private Button button = null; // frm1 g = new frm1(); public static void main(String[] args) {
    Display display = Display.getDefault();
    frm1 thisClass = new frm1();
    thisClass.createSShell(display);
    // thisClass.sShell.open(); while (!thisClass.sShell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    } private void createSShell(Display display) {
    sShell = new Shell(display);
    sShell.setText("Shell");
    sShell.setSize(new Point(300, 200));
    sShell.setLayout(null);
    txtName = new Text(sShell, SWT.BORDER);
    txtName.setBounds(new Rectangle(78, 29, 116, 24));
    label = new Label(sShell, SWT.NONE);
    label.setBounds(new Rectangle(21, 29, 51, 25));
    label.setFont(new Font(Display.getDefault(), "\u5b8b\u4f53", 11,
    SWT.NORMAL));
    label.setText("姓名:");
    button = new Button(sShell, SWT.NONE);
    button.setBounds(new Rectangle(187, 112, 68, 29));
    button.setText("OPEN");
    button
    .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
    public void widgetSelected(
    org.eclipse.swt.events.SelectionEvent e) {
    frm2 a = new frm2(sShell);
    txtName.setText(a.open());
    }
    });
    sShell.pack();
    Rectangle rectParent = Display.getDefault().getBounds();
    Rectangle rectShell = sShell.getBounds();
    sShell.setBounds(rectParent.x + (rectParent.width - rectShell.width)
    / 2, rectParent.y + (rectParent.height - rectShell.height) / 2,
    rectShell.width, rectShell.height); sShell.open(); }}
    package csdn;import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.Dialog;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Rectangle;public class frm2 extends Dialog { private Shell sShell = null; private Button button = null; private String btnText = null; public frm2(Shell shell) {
    super(shell, SWT.DIALOG_TRIM);
    } private void createSShell() {
    // sShell = new Shell(display);
    sShell.setText("Shell");
    sShell.setSize(new Point(300, 200));
    sShell.setLayout(null);
    button = new Button(sShell, SWT.NONE);
    button.setBounds(new Rectangle(53, 41, 79, 34));
    button.setText("dream");
    button
    .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
    public void widgetSelected(
    org.eclipse.swt.events.SelectionEvent e) {
    btnText = button.getText();
    sShell.dispose();
    }
    });
    } public String open() {
    sShell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

    createSShell();
    sShell.pack();
    sShell.open();

    Display display = sShell.getDisplay();

    while (!sShell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    return btnText;
    }}
      

  2.   

    非常谢谢qingkangxu 你的帮忙.
    稍后给大家打分.