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

时间:2016-05-07 13:21:53

标签: java swing jfilechooser

所以我试图这样做,当我点击一个按钮时,它将打开另一个类的框架,我的filechooser框架将无法打开。

这是一个名为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类,简称为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);

    }

}

2 个答案:

答案 0 :(得分:2)

最简单,最合理的解决方案是将myButtonAction()方法更改为:

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

答案 1 :(得分:0)

如果我理解正确的话,你就会错过两行。 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行)