有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单击JButton时打开新JFrame的java

当从IOUApplication.java按下btnAddNewDebt按钮时,我正试图打开DebtForm.java。按下按钮后IOUApplication.java窗口应关闭DebtForm.java窗口应打开

当按下btnAddNewDebt按钮时,我成功地打开了DebtForm.java,但我无法关闭IOUApplication.java窗口

我尝试使用以下方法:

public void close(){
    WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOS­ING);
    Toolkit.getDefaultToolkit().getSystemEve­ntQueue().postEvent(winClosingEvent);
}

然而,我不知道该把代码放在哪里,或者是否有其他方法可以关闭窗口

以下是上下文的IOUApplication.java片段:

public void initialize() {

    frame = new JFrame();
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.setBounds(100, 100, 450, 132);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnAddNewDebt = new JButton("Add new debt");
    btnAddNewDebt.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DebtForm frame = new DebtForm();
            frame.setVisible(true);
        }
    });

    btnAddNewDebt.setBounds(81, 18, 117, 29);
    frame.getContentPane().add(btnAddNewDebt);      

    JButton btnPersonalDebt = new JButton("Personal Debt");
    btnPersonalDebt.setBounds(266, 18, 117, 29);
    frame.getContentPane().add(btnPersonalDebt);

    JLabel lblWrittenAndCoded = new JLabel("Written and coded by Samuel Kahessay");
    lblWrittenAndCoded.setForeground(Color.WHITE);
    lblWrittenAndCoded.setBounds(108, 88, 252, 16);
    frame.getContentPane().add(lblWrittenAndCoded);
}

共 (1) 个答案

  1. # 1 楼答案

    要关闭JFrame,只需调用frame。dispose()。这种方法将完全消除内存中的JFrame。如果你想稍后重新打开窗口,你需要做的就是框。设置Visible(false),然后设置frame。要重新打开时,设置为可见(true)。要做到这一点,可能需要将JFrame设置为全局变量。假设您为DebtForm()编写的代码可以创建自己的JFrame,下面是它的样子:

    public void initialize() {
    
    /* Your same code */
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.setBounds(100, 100, 450, 132);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    
    JButton btnAddNewDebt = new JButton("Add new debt");
    btnAddNewDebt.addActionListener(new ActionListener() {
    
        public void actionPerformed(ActionEvent e) {
            DebtForm debtForm = new DebtForm();
            debtForm.setVisible(true);
            /* THE ONLY NEW LINE OF CODE */
            frame.setVisible(false); //This will make the first window disapear.
            /*             - */
        }
    });
    
    btnAddNewDebt.setBounds(81, 18, 117, 29);
    frame.getContentPane().add(btnAddNewDebt);      
    
    JButton btnPersonalDebt = new JButton("Personal Debt");
    btnPersonalDebt.setBounds(266, 18, 117, 29);
    frame.getContentPane().add(btnPersonalDebt);
    
    JLabel lblWrittenAndCoded = new JLabel("Written and coded by Samuel Kahessay");
    lblWrittenAndCoded.setForeground(Color.WHITE);
    lblWrittenAndCoded.setBounds(108, 88, 252, 16);
    frame.getContentPane().add(lblWrittenAndCoded);
    }
    

    稍后,为了使原始窗口重新出现,在initialize()方法之外,您需要如下声明“frame”:

    public static JFrame frame = new JFrame();
    

    现在以你的形式。java,您可以像这样再次使框架可见:

    IOUApplication.frame.setVisible(true);
    

    我希望这有帮助。谢谢你的阅读