当你点击View tables并查看全部时,我在java中有这个程序 出现一个窗口,我需要隐藏第一个窗口,然后关闭第二个窗口,
第一个再次可见
我不知道如何从这些类中引用一个对象,因为我已经创建了它们 new Main();和新的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);
}
}
我需要的是当我关闭第二个窗口时第一个窗口再次可见
答案 0 :(得分:1)
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 Adapter以在窗口关闭时获取Windows事件,然后main()窗口显示
答案 1 :(得分:0)
您可以创建自己的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
}
}
然后当您在View
中创建新的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);
}
});