java实现简单猜数字

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

本文实例为大家分享了java实现简单猜数字的具体代码,供大家参考,具体内容如下

代码不多说,图片自加,实现功能有数字比大小,菜单开始,帮助,退出,关于等,运行结果如图:

代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Bingo extends JFrame implements ActionListener {
JFrame frame;
JMenuBar topBbr;
JMenu menuQ;
JMenu menuF;
JMenuItem start = new JMenuItem("开始(n)");
JMenuItem out = new JMenuItem("不玩了(e)");
JMenuItem help = new JMenuItem("帮助(h)");
JMenuItem about = new JMenuItem("关于(a)");
JPanel panelMain = new JPanel(new BorderLayout());
JPanel panelNext = new JPanel(new BorderLayout());
JButton btnStart;
JPanel jp1, jp2, jp3, jp4;

public static void main(String[] args) {
new Bingo();
}

public Bingo() {
frame = new JFrame();
topBbr = new JMenuBar();
frame.setTitle("王氏猜数字游戏1.0版");
frame.setSize(400, 320);
frame.setLocation(450, 240);
frame.setLayout(getLayout());
// frame.setLayout(new FlowLayout());
menuQ = new JMenu("游戏(q)");
menuQ.add(start);
menuQ.add(out);
menuF = new JMenu("帮助(f)");
ImageIcon icon = new ImageIcon("imgs/logo.jpg");
btnStart = new JButton(icon);
JButton caiButton = new JButton("猜一猜");
JButton congButton = new JButton("重新来");
JTextField text = new JTextField(6);
JLabel mes = new JLabel("  请你输入一个2位数");
menuF.add(help);
menuF.add(about);
topBbr.add(menuQ);
topBbr.add(menuF);
frame.setJMenuBar(topBbr);
panelMain.add("Center", btnStart);
frame.add(panelMain);
frame.setResizable(false);
frame.setVisible(true);
// Image im = new ImageIcon("imgs/logo.jpg").getImage();
// im = im.getScaledInstance(-1, 90, DO_NOTHING_ON_CLOSE);
// setIconImage(im);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panelMain.setVisible(false);
start.setEnabled(false);
frame.add(panelNext);
jp1 = new JPanel();
jp2 = new JPanel(new GridLayout(2, 1));
jp3 = new JPanel();
JLabel top = new JLabel("??");
// top.setBackground(new Color(100, 100, 0, 100));
// top.setOpaque(true);
JLabel jb = new JLabel(" ");
JLabel jb2 = new JLabel(
" ");
top.setFont(new Font("隶体", Font.BOLD, 100));
top.setForeground(Color.yellow);
jp1.add(top);
jp2.add(mes);
jp2.add(text);
jp3.add(caiButton);
jp3.add(congButton);
panelNext.add("North", jp1);
panelNext.add("Center", jp2);
panelNext.add("West", jb);
panelNext.add("East", jb2);
panelNext.add("South", jp3);
panelNext.setVisible(true);
}
});
// 开始监听1
btnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start.setEnabled(false);
panelMain.setVisible(false);
frame.add(panelNext);
jp1 = new JPanel();
jp2 = new JPanel(new GridLayout(2, 1));
jp3 = new JPanel();
JLabel top = new JLabel("??");
// top.setBackground(new Color(100, 100, 0, 100));
// top.setOpaque(true);
JLabel jb = new JLabel("     ");
JLabel jb2 = new JLabel("     ");
top.setFont(new Font("隶体", Font.BOLD, 100));
top.setForeground(Color.yellow);
jp1.add(top);
jp2.add(mes);
jp2.add(text);
jp3.add(caiButton);
jp3.add(congButton);
panelNext.add("North", jp1);
panelNext.add("Center", jp2);
panelNext.add("West", jb);
panelNext.add("East", jb2);
panelNext.add("South", jp3);
panelNext.setVisible(true);
}
});
// 退出监听
out.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);

}
});
// 帮助监听
help.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "请按开始进行游戏!就是猜数字而已,就不多说了!",
"帮助", JOptionPane.PLAIN_MESSAGE);
}
});
// 关于监听
about.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "谢谢尝脸试玩!", "3Q",
JOptionPane.WARNING_MESSAGE);
}
});
// 开始监听2
caiButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Random x = new Random();
int y = x.nextInt(100);
try {
int num = Integer.parseInt(text.getText());
if (num > y) {
mes.setText("  你输入的" + num + "大了哦!");
if (num > 99) {
mes.setText("  请输入小于99的数字!");
}
}else if(num == y){
mes.setText("  恭喜你猜对了哦!");
} 
else{
mes.setText("  你输入的" + num + "小了哦!");
if (num < 1) {
mes.setText("  请输入大于0的数字!");
}
} 
} catch (Exception e2) {
mes.setText("  请输入0-99的数字!");
}
}
});
// 重置监听
congButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});

}

@Override
public void actionPerformed(ActionEvent e) {
}
}

更多有趣的经典小游戏实现专题,也分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多