game猜数字游戏

import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.*;import javax.swing.*;public class gamecaishuzi extends JFrame implements ActionListener{/** * 定义组件 */MyPanel jp1,jp2,jp3;JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9;JLabel jl1; public static void main(String []args){gamecaishuzi gcf = new gamecaishuzi ();}/** * 构造方法 */public gamecaishuzi(){/** * 创建组件 */jp1 = new MyPanel();jp2 = new MyPanel();jp3 = new MyPanel();jb1 = new JButton("1");jb2 = new JButton("2");jb3 = new JButton("3");jb4 = new JButton("4");jb5 = new JButton("5");jb6 = new JButton("6");jb7 = new JButton("7");jb8 = new JButton("8");jb9 = new JButton("9");jl1 = new JLabel("欢迎来到猜数字游戏!>_<");/** * 设置布局 */this.setLayout(new GridLayout(3,1));/** * 添加组件 */jp1.add(jl1);jp2.add(jb1);jp2.add(jb2);jp2.add(jb3);jp2.add(jb4);jp2.add(jb5);jp2.add(jb6);jp2.add(jb7);jp2.add(jb8);jp2.add(jb9);/** * 注册监听 */jb1.addActionListener(this);jb1.setActionCommand("1");jb2.addActionListener(this);jb2.setActionCommand("2");jb3.addActionListener(this);jb3.setActionCommand("3");jb4.addActionListener(this);jb4.setActionCommand("4");jb5.addActionListener(this);jb5.setActionCommand("5");jb6.addActionListener(this);jb6.setActionCommand("6");jb7.addActionListener(this);jb7.setActionCommand("7");jb8.addActionListener(this);jb8.setActionCommand("8");jb9.addActionListener(this);jb9.setActionCommand("9");/** * 添加面板 */this.add(jp1);this.add(jp2);this.add(jp3);/** * 设置窗体 */this.setTitle("game!");jp3.setBackground(Color.BLACK);this.setSize(500,200);this.setLocation(400,400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}public void actionPerformed(ActionEvent e) {Random random = new Random();int num ;num = random.nextInt(9)+1;System.out.println(num);if(e.getActionCommand().equals("1") && num == 1){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("2")&& num == 2 ){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("3")&& num == 3 ){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("4") && num == 4 ){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("5")&& num == 5 ){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("6")&& num == 6 ){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("7") && num == 7){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("8") && num == 8){jp3.setBackground(Color.RED);}elseif(e.getActionCommand().equals("9") && num == 9 ){jp3.setBackground(Color.RED);}}} /** * 创建面板类 */class MyPanel extends JPanel {public void paint(Graphics g){super.paint(g);}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值