java swing 跳转窗口_Java建立窗口并通过按钮实现窗口跳转

为之前的游戏做了一个菜单的模型

这是菜单类import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Menu extends JFrame implements ActionListener {

//定义两个按钮

private JButton Game_Start;

private JButton Game_Over;

public Menu(){

//定义按钮的排列方式

setLayout(new FlowLayout());

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this.setSize(300,200);

this.setLocation(300,400);

Game_Start = new JButton("开始游戏");

Game_Over = new JButton("结束游戏");

this.add(Game_Start);

this.add(Game_Over);

Game_Start.addActionListener(this);

Game_Over.addActionListener(this);

this.setVisible(true);

}

public static void main(String[] args) {

Menu menu = new Menu();

}

/**

* Invoked when an action occurs.

*

* @param e

*/

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource() == Game_Start){

this.dispose();

new Newframe();

}

if(e.getSource() == Game_Over){

this.dispose();

System.exit(0);

}

}

}

这是新的一个类(模拟我的游戏主类)import javax.swing.*;

public class Newframe extends JFrame {

public Newframe(){

this.setSize(300,200);

this.setLocation(300,400);

this.setVisible(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

}

演示:

654a406290defcce1d54657899e0147f.png

当点击按钮开始游戏的时候,会跳转到下一个新的窗口,而点击结束游戏的时候会关闭窗口,并停止运行这个程序。

1f237add84e20e7842cb9f0fb108a97e.png

这个是点击开始游戏之后的新窗口 ↑ ↑ ↑

  • 8
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值