java 从另一个 class 打开一个框架

java open a frame from another class

所以我试图做到这一点,所以当我单击一个按钮时,它会打开另一个框架 class 我的文件选择器框架无法打开。

这是 class 调用的 mainFrame:

package javatut;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class mainFrame {

    private JFrame frmMainwindow;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainFrame window = new mainFrame();
                    window.frmMainwindow.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public mainFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmMainwindow = new JFrame();
        frmMainwindow.setTitle("Mainwindow");
        frmMainwindow.setBounds(100, 100, 280, 150);
        frmMainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmMainwindow.getContentPane().setLayout(null);

        JButton btnBrowse = new JButton("Browse");
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                myButtonAction();
            }
        });
        btnBrowse.setBounds(155, 11, 100, 23);
        frmMainwindow.getContentPane().add(btnBrowse);

        textField = new JTextField();
        textField.setBounds(10, 12, 135, 20);
        frmMainwindow.getContentPane().add(textField);
        textField.setColumns(10);
    }

    private void myButtonAction(){
        Toolkit.getDefaultToolkit().beep();
    }
}

这是 FileChooser class 简单地称为 frame:

package javatut;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class frame {


    private JFrame frmFilechooser;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame window = new frame();
                    window.frmFilechooser.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public frame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmFilechooser = new JFrame();
        frmFilechooser.setTitle("FileChooser");
        frmFilechooser.setBounds(100, 100, 470, 350);
        frmFilechooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmFilechooser.getContentPane().setLayout(new BorderLayout(0, 0));

        JFileChooser fileChooser = new JFileChooser();
        frmFilechooser.getContentPane().add(fileChooser);

    }

}

如果我没理解错的话,你只是少了两行。 mainFrame 的 myButtonAction 中的第一个:

 private void myButtonAction(){
  //  Toolkit.getDefaultToolkit().beep();
    new frame();
}

另一个在框架的初始化中:

    private void initialize() {
    frmFilechooser = new JFrame();
    frmFilechooser.setTitle("FileChooser");
    frmFilechooser.setBounds(100, 100, 470, 350);
    frmFilechooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmFilechooser.getContentPane().setLayout(new BorderLayout(0, 0));

    JFileChooser fileChooser = new JFileChooser();
    frmFilechooser.getContentPane().add(fileChooser);
    frmFilechooser.setVisible(true);
}

(我刚刚在底部添加了 setVisible 行)

最简单和最合乎逻辑的解决方案是将 myButtonAction() 方法更改为:

private void myButtonAction() {
    JFileChooser fileChooser = new JFileChooser();
    int result = fileChooser.showOpenDialog(frmMainwindow);
    if (result==JFileChooser.APPROVE_OPTION) {
        // do something with the chosen file ..
    }
}