单击按钮打开新的JFrame

时间:2015-04-17 13:26:11

标签: java swing jframe

我想知道当我点击第二个JFrame(2)中的按钮时如何打开这个JFrame表单(1)。问题是我无法在表单2 中获取.setVisible方法。请帮忙。谢谢&问候 ! :)

表单1(在表单2上单击按钮时打开

public class FlightForm {

    public FlightForm() {
        initialize();
    }

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

表格2

public class MainMenu{

private JFrame frame;

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

/**
 * Create the application.
 */
public MainMenu() {
    frame = new JFrame("Main Menu");
    setBounds(100, 100, 830, 574);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("Flight Form");
    );
    btnNewButton.setFont(new Font("Candara", Font.BOLD, 15));
    btnNewButton.setBounds(169, 328, 193, 77);
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("Passenger Form");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            PassengerForm window = new PassengerForm();
                window.setVisible(true); // This is not working

1 个答案:

答案 0 :(得分:0)

只有当setVisible(true)类扩展JFrame时,您才能在PassengerForm()上致电PassengerForm。如果不是,你应该使用类似的东西:

PassengerForm window = new PassengerForm();
window.getFrame().setVisible(true)