从Swing JFrame调用SWT主应用程序窗口

时间:2019-04-04 02:34:08

标签: java swing swt

我正在使用Eclipse并创建了一个SWT应用程序。我有两个窗口:

  1. 我的第一个表单(MainForm)是SWT应用程序窗口
  2. 第二种形式(Form2)是JFrame

MainForm有一个按钮(b1),而Form2有一个按钮(b2)。

单击b1时,我创建了一个Form2setVisible()为true的对象,以便可以看到Form2。在此过程中,我还通过MainForm

隐藏了shell.setVisible(false);

现在,我想在MainForm单击b2后打开Form2,但是我还是找不到来做。你能帮我怎么做?

MainForm中的代码段:

public class MainForm {

protected Shell shell;
private Text text;
private Text text_1;

/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
    try {
        MainForm window = new MainForm();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    Button button1 = = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Form2 f = new Form2(5);
            f.setVisible(true);
            shell.setVisible(false);


        }
    });
}}

Form2代码段:

public class Form2 extends JFrame {

private JPanel contentPane;

    /**
 * Create the frame.
 */
public Form2( int a) {
    JOptionPane.showConfirmDialog(null, a);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 834, 560);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnBack = new JButton("Back");
    btnBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //I need to know what should I write here to display MainForm. I tried with the following but does not work.

            //MainForm f = new MainForm();
            //MainForm.this.shell.setVisible(true);
            //f.shell.setVisible(true);

        }
    });
    btnBack.setBounds(264, 46, 188, 80);
    contentPane.add(btnBack);
}
}

请参阅Form2的注释部分。如果您能给我一个想法,应该在Form2的注释部分写些内容,以便可以从那里显示MainForm,我将不胜感激。

0 个答案:

没有答案