Java 处理窗口时发生的事件

Java 处理窗口时发生的事件,java,swing,Java,Swing,我有一个java程序,当你点击View tables并查看所有 出现一个窗口,我需要隐藏第一个窗口,在关闭第二个窗口时 第一个再次可见的 import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.*; class View extends JFrame { private static final long serialVersionU

我有一个java程序,当你点击View tables并查看所有 出现一个窗口,我需要隐藏第一个窗口,在关闭第二个窗口时

第一个再次可见的

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);
      }
    }
我不知道如何引用这些类中的对象,因为我创建了它们 新的Main();和新视图();:)

这是一门课:

packarmanager;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类主框架{
私有静态最终长serialVersionUID=1L;
静态整数宽度=400;
静态内部高度=宽度/16*9;
静态字符串title=“汽车管理器”;
JButton viewTables=新JButton(“视图表”);
JButton客户机=新JButton(“客户机”);
JButton search=新JButton(“search”);
JButton viewCars=新JButton(“查看全部”);
JButton viewRent=新JButton(“租车”);
JButton viewBuy=新JButton(“购买汽车”);
JButton view附件=新JButton(“附件”);
公用干管(){
setLayout(空);
setLocationRelativeTo(空);
片名(片名);
设置尺寸(宽度、高度);
可设置大小(假);
setDefaultCloseOperation(关闭时退出);
setVisible(真);
JLabel background=新的JLabel(新的图像图标(“res\\background2.jpg”);
添加(背景);
背景。设置大小(宽度、高度);
添加(视图表);
添加(客户);
添加(搜索);
视图表.立根(20,20,110,30);
客户。挫折(20、70、110、30);
搜索。挫折(20、120、110、30);
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
添加(可视汽车);
观察车.立根(260,20,110,20);
添加(查看租金);
退步(260,50,110,20);
添加(viewBuy);
viewBuy.setBounds(260,80,110,20);
添加(查看附件);
视图附件.立根(260、110、110、20);
}
});
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
新视图();
setVisible(假);
}
});
}
公共静态void main(字符串参数[]){
新的Main();
}
}
这是出现的第二个窗口:

packarmanager;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类视图扩展了JFrame{
私有静态最终长serialVersionUID=1L;
整数宽度=400;
内部高度=宽度/16*9;
String title=“查看所有车辆”;
公众观点(){
setLayout(空);
setLocationRelativeTo(空);
片名(片名);
设置尺寸(宽度、高度);
可设置大小(假);
setDefaultCloseOperation(在关闭时处理);
setVisible(真);
JLabel background=新的JLabel(新的图像图标(“res\\background2.jpg”);
添加(背景);
背景。设置大小(宽度、高度);
}
}
我所需要的是当我关闭第二个窗口时,第一个窗口将再次可见

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);
      }
    }

请运行程序view.java i add Windows Adapter以在窗口关闭时获取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()
中装入新的
视图时,您可以注册自己的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);
        }
    });

只要小心,只要以前的实例仍然存在(但仅不可见),就不要创建Main()的新实例。好的,它可以工作,但在我关闭第二个窗口后,第一个窗口显示为应该的可见,但按钮不可见,我必须将鼠标悬停在它们上方才能显示,为什么会这样,以及如何修复它?嗯,这在我的测试中没有发生。无论如何,您可以尝试在主窗口上强制重新绘制:在
MyWindowAdapter
windowClosing()
添加(在
mainFrame.setVisible(true);
下)行
mainFrame.repaint()。这将通知
窗口尽快重新绘制其组件。虽然这不是必需的…告诉我它是否工作可能
mainFrame.revalidate()可以做到这一点。。但我猜是你的操作系统导致了这个小错误。。也许您的程序的其他实例正在后台运行。我会再次尝试重新启动,如果weired问题存在,请在SO中提出一个新问题。很抱歉,如果我不能在这里提供更多帮助,但是正如我所说的,当我运行上面的代码时,这种情况不会发生,所以我不知道去哪里查找。