简体   繁体   English

通过单击一个按钮来打开一个新的JFrame

[英]Opening a new JFrame by clicking a button

I want to know how to open this JFrame form (1) when I click a button in the second JFrame (2). 当我单击第二个JFrame(2)中的按钮时,我想知道如何打开此JFrame窗体(1)。 The problem is that I am unable to get the .setVisible method in the Form 2 . 问题是我无法在Form 2中获得.setVisible方法。 Please help. 请帮忙。 Thanks & Regards ! 感谢和问候 ! :) :)

Form 1 (to be opened when a button is clicked on Form 2 表格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();
                }
            }
        }
    }
}

Form 2 表格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

You can call setVisible(true) on PassengerForm() only if PassengerForm class extends JFrame. 仅当PassengerForm类扩展JFrame时,才可以在PassengerForm()上调用setVisible(true) If no you should use something like: 如果否,则应使用以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM