Java GUI多个窗口切换
养猪去 人气:0功能:
主要实现的功能为实现多个界面的切换,并且一个window的打开和关闭可以影响其他window。
不足:
①可以多次多开同一个界面(可以加一个变量控制)
②没有实现一个的窗体关闭,它的子窗体也随即关闭的效果
效果图:
第一个界面(主界面)
package 多界面跳转; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; class Frame1 extends JFrame implements WindowListener { JButton b2 = new JButton("界面2"); JButton b11 = new JButton("界面11"); private class btListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("界面2")) { setVisible(false); new Frame2(); } else if(e.getActionCommand().equals("界面11")) { new Frame11(); } } } public Frame1() { this.setTitle("界面1"); this.setSize(400, 300); this.setLayout(new FlowLayout()); b11.addActionListener(new btListener()); b2.addActionListener(new btListener()); this.add(b11); this.add(b2); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); } public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { setVisible(true); } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public static void main(String[] args) { Frame1 f1 = new Frame1(); f1.setVisible(true); } }
第二个界面
package 多界面跳转; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class Frame2 extends JFrame implements ActionListener { JButton bt = new JButton("界面21"); Frame2() { this.setSize(350, 300); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setTitle("界面2"); this.add(bt); bt.addActionListener(this); this.addWindowListener(new Frame1()); this.addWindowListener(new Frame11()); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { new Frame21(); } }
由第一个界面打开的界面
package 多界面跳转; import javax.swing.*; import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; class Frame11 extends JFrame implements WindowListener { Frame11() { this.setSize(300, 200); this.setLocationRelativeTo(null); this.setTitle("界面11"); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub if(this.isVisible()) { // setSize(800,600); } } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } }
由第二个界面打开的界面
package 多界面跳转; import javax.swing.*; import java.awt.*; class Frame21 extends JFrame { Frame21() { this.setSize(150, 100); this.setLocationRelativeTo(null); this.setTitle("界面21"); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); } }
加载全部内容