Java GUI实现多个窗口切换效果
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了Java GUI实现多个窗口切换效果的具体代码,供大家参考,具体内容如下
功能:
主要实现的功能为实现多个界面的切换,并且一个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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341