gpt4 book ai didi

java - 处理窗口的事件

转载 作者:行者123 更新时间:2023-12-01 13:25:50 25 4
gpt4 key购买 nike

当您单击“查看表”并查看所有内容时,我在 java 中有这个程序出现一个窗口,我需要隐藏第一个窗口并在关闭第二个窗口时,

第一个再次可见的

我不知道如何引用这些类中的对象,因为我已经像这样创建了它们新的主();和新的 View (); :)

这是一类:

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

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

关于java - 处理窗口的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21812698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com