释放窗口的事件



当你点击查看表并查看全部时,我在java中有这个程序出现一个窗口,我需要隐藏第一个窗口,并在关闭第二个窗口时关闭窗口,

第一个再次可见的

我不知道如何从这些类中引用对象,因为我已经创建了它们,例如新主();和新视图();:)

这是一个类:

package CarManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
    private static final long serialVersionUID = 1L;
    static int width = 400;
    static int height = width / 16 * 9;
    static String title = "Car Manager";
    JButton viewTables = new JButton("View tables");
    JButton clients = new JButton("Clients");
    JButton search = new JButton("Search");
    JButton viewCars = new JButton("View all");
    JButton viewRent = new JButton("Rent a car");
    JButton viewBuy = new JButton("Buy a car");
    JButton viewAccessory = new JButton("Accessory");
    public Main() {
        setLayout(null);
        setLocationRelativeTo(null);
        setTitle(title);
        setSize(width, height);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        JLabel background = new JLabel(new ImageIcon("res\background2.jpg"));
        add(background);
        background.setSize(width, height);
        add(viewTables);
        add(clients);
        add(search);
        viewTables.setBounds(20, 20, 110, 30);
        clients.setBounds(20, 70, 110, 30);
        search.setBounds(20, 120, 110, 30);
        viewTables.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                add(viewCars);
                viewCars.setBounds(260, 20, 110, 20);
                add(viewRent);
                viewRent.setBounds(260, 50, 110, 20);
                add(viewBuy);
                viewBuy.setBounds(260, 80, 110, 20);
                add(viewAccessory);
                viewAccessory.setBounds(260, 110, 110, 20);
            }
        });
        viewCars.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new View();
                setVisible(false);
            }
        });
    }
    public static void main(String args[]) {
        new Main();
    }
}

这是出现的第二个窗口:

package CarManager;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class View extends JFrame {
    private static final long serialVersionUID = 1L;
    int width = 400;
    int height = width / 16 * 9;
    String title = "View all Cars";
    public View() {
        setLayout(null);
        setLocationRelativeTo(null);
        setTitle(title);
        setSize(width, height);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
        JLabel background = new JLabel(new ImageIcon("res\background2.jpg"));
        add(background);
        background.setSize(width, height);
    }
}

我所需要的只是当我关闭第二个窗口时,第一个窗口再次可见

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.*;

class View extends JFrame  {
private static final long serialVersionUID = 1L;
int width = 400;
int height = width / 16 * 9;
String title = "View all Cars";
    public View() {
    setLayout(null);
    setLocationRelativeTo(null);
    setTitle(title);
    setSize(width, height);
    setResizable(false);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setVisible(true);
    JLabel background = new JLabel(new ImageIcon("res\background2.jpg"));
    add(background);
    background.setSize(width, height);
   WindowListener listener = new WindowAdapter() {

   public void windowClosing(WindowEvent w) {
          new Main();
       }
      };
     addWindowListener(listener);
      }
    }

请运行程序视图.java我添加Windows适配器以在窗口关闭时获取Windows事件,然后显示main()窗口

您可以创建自己的 WindowAdapter:

public class MyWindowAdapter extends WindowAdapter {
    private Main mainFrame;         
    public MyWindowAdapter(Main mainFrame) {   // when creating an instance of this WindowAdapter, tell it with which Main Window you are working with
        this.mainFrame = mainFrame;
    }
    public void windowClosing(WindowEvent e) {
        mainFrame.setVisible(true);                 // when this WindowAdapter registers a closing operation it will set the Main Window visible again
    }
}

然后,当您在actionPerformed()中创建新View时,注册您自己的 WindowAdapter 的新实例,并告诉窗口适配器您的Main实例是什么样子的。

 viewCars.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            View view = new View();                         // your new View
            view.addWindowListener(new MyWindowAdapter(Main.this));   // register the WindowAdapter to your new View
            setVisible(false);
        }
    });

最新更新