多个JFrame同时打开,只需单击JAVA即可关闭

时间:2018-04-09 17:50:51

标签: java swing

我有一个主菜单,当需要看到添加的数据弹出一个带有ok按钮的新JFrame时,我添加了一个监听器,当我点击确定关闭它时,它关闭主菜单并保持GUI结果打开,我只关闭结果gui,而不是主菜单,我尝试dispose(),当然还有system.out(0),但它关闭所有

public void GuiResultados(Persona persona, Abs abs){

    JLabel titulo = new JLabel();  
    titulo.setBounds(5,8,700,30); 
    titulo.setText("Resultados");
    JFrame frame= new JFrame();  
    JButton button = new JButton("Ok"); \\button create
    CloseListener closelistener = new CloseListener(); \\closelistener process
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    button.setVisible(true); \\button visible
    frame.setTitle("Datos de entrenamiento");
    JTextArea area=new JTextArea();
    area.append("Edad: "+ persona.getEdad() + "\n");
    area.append("Estatura(m): " + persona.getTamano() + "\n");

    area.append("Cantidad de calorias quemadas en todas las series:" + totalCaloriasQuemadasAbs + "\n");


    button.setBounds(150,395,100,30);
    area.setEditable(false);
    area.setBounds(20,30, 350,350);  
    frame.add(area);  
    frame.add(button);

    button.addActionListener(closelistener); // calling the listener
    frame.add(titulo);
    frame.setSize(410,500);  
    frame.setLayout(null);  
    frame.setVisible(true); 
}

private class CloseListener implements ActionListener{

    public void actionPerformed(ActionEvent e) {

        dispose(); //not closing main window, not the gui where the button is
    System.out.println("Frame Closed. ");
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,抱歉,如果有人需要这些

private class CloseListener implements ActionListener{

    public void actionPerformed(ActionEvent e) {
        Window win = SwingUtilities.getWindowAncestor((Component) e.getSource());
        win.dispose();
    System.out.println("Frame Closed. ");
    }
}