如何从新框架中调用上一个框架,如何再次设置可见?

时间:2018-08-25 13:09:12

标签: java eclipse swing

当我调用其他框架时,例如单击按钮,我将Meni JFrame设置为不可见。 Reports JFrame,以及现在如何从Reports框架中调用,再次与我以前使用的Meni框架相同,而不是新的框架。

我现在的代码是这样的:

梅尼

JButton btnReports = new JButton("   Reports");
    btnReports.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            Reports r = new Reports();
            reports.setVisible(true);
            setVisible(false); //setting current JFrame invisible 

        }
    });

现在我想返回到我之前设置为不可见的Meni框架,而不是创建一个新的Meni框架,如果可能的话,该怎么做?

报告

 JButton btnMeni = new JButton("   Meni");
    btnMeni.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //if i do the same like in Meni it will call new window, and 
            //i don't want that i simply want to setVisible Meni window again

            dispose();

        }
    });

谢谢!

2 个答案:

答案 0 :(得分:1)

您不应在此处使用2张相框。请改用CardLayout。这是一个小示例,说明如何将两个滤镜放置到单个JFrame

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.WindowConstants;

public class ScreenUI {

    private static final String FIRST_SCREEN = "first";

    private static final String SECOND_SCREEN = "second";

    public static void main(String[] args) {
        JFrame frame = new JFrame("Multiscreen");
        CardLayout cl = new CardLayout();
        // set card layout
        frame.getContentPane().setLayout(cl);
        // build first screen
        JPanel firstScreen = new JPanel(new BorderLayout());
        firstScreen.add(new JScrollPane(new JList<>(new String[] {"One", "Two", "Three", "Four"})));
        JButton nextScreen = new JButton("Next");
        // action to show second screen, which is referenced per string constant
        nextScreen.addActionListener(e -> cl.show(frame.getContentPane(), SECOND_SCREEN)); 
        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonsPanel.add(nextScreen);
        firstScreen.add(buttonsPanel, BorderLayout.SOUTH);
        frame.getContentPane().add(firstScreen, FIRST_SCREEN);
        // build second screen
        JPanel secondScreen = new JPanel(new BorderLayout());
        secondScreen.add(new JScrollPane(new JTree()));
        JButton previousScreen = new JButton("Previous");
        // action to show first screen, which is referenced per string constant
        previousScreen.addActionListener(e -> cl.show(frame.getContentPane(), FIRST_SCREEN));
        buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonsPanel.add(previousScreen);
        secondScreen.add(buttonsPanel, BorderLayout.SOUTH);
        frame.getContentPane().add(secondScreen, SECOND_SCREEN);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

答案 1 :(得分:0)

您可以在“主”框架中添加一个侦听器,以便在报告框架关闭时再次显示该框架:

   r.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent e)
        {
            setVisible(true);

        }
    });

WindowListener的Java文档:https://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html#windowClosing(java.awt.event.WindowEvent)

在报表框架上,只需通过以下方式关闭OnButtonClick方法中的窗口

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));