java 用图形化界面编写一个猜数游戏效果图
一个比较简单java猜数字窗体代码的代码, 供参考 ,可以根据自己的需求, 在进行修改
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class GuessNumFrame extends JFrame {
private JPanel contentPane;
private int num;
private final JTextField jtfInput;
private final JLabel label;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuessNumFrame frame = new GuessNumFrame();
frame.setVisible(true);//窗口可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuessNumFrame() {
setTitle("猜数字");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(360, 160);//窗口大小
setLocationRelativeTo(null);//居中
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new GridLayout(3, 1, 0, 0));
JPanel panel = new JPanel();
panel_1.add(panel);
JButton jbNum = new JButton("得到一个随机数");
panel.add(jbNum);
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
label = new JLabel("");
label.setOpaque(true);
label.setBackground(Color.CYAN);
panel_2.add(label);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
jtfInput = new JTextField();
panel_3.add(jtfInput);
jtfInput.setColumns(10);
JButton jbOk = new JButton("确定");
panel_3.add(jbOk);
jbNum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num = (int)(Math.random()*10);//生成随即数
label.setText("随机数已经生成范围0~9");
System.out.println(num);
}
});
jbOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x;
try {
x = Integer.parseInt(jtfInput.getText());//取得输入框的数字 进行比较
}catch (Exception e1) {
JOptionPane.showMessageDialog(null, "sorry,输入错误!");
return;
}
if(x==num){
JOptionPane.showMessageDialog(null, "厉害,猜对java猜数字窗体代码了~!");
}else if(xnum){
JOptionPane.showMessageDialog(null, "你猜小了", "提示",JOptionPane.WARNING_MESSAGE);
}else if(xnum) {
JOptionPane.showMessageDialog(null, "你猜大了", "提示",JOptionPane.WARNING_MESSAGE);
}
}
});
}
}
java窗体猜数字代码import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class caishuzi extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JPanel textpanel, buttonpanel, fopanel, downpanel;
private JButton btn;
private JTextField jtinput, jttishi;
private JLabel lbnum0, lbnum, lbtishi, lbinput;
private JTextArea tafo;
private Boolean start = false, jisuan = false, jinru = false;
MenuBar mb = new MenuBar();
Menu filemenu = new Menu("文件"), systemmenu = new Menu("系统"),
helpmenu = new Menu("帮助");
MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打开"),
closefile = new MenuItem("关闭"), quit = new MenuItem("退出"),
inputsecret = new MenuItem("输入密码"), help = new MenuItem("帮助"),
inputcishu = new MenuItem("输入限制次数");
char[] c = new char[4];
String strsecret = "123";
int xianzhi=8;
caishuzi()
{
super("猜数字");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocationRelativeTo(null);
// 设置菜单
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.addSeparator();
filemenu.add(closefile);
newfile.setEnabled(false);
openfile.setEnabled(false);
closefile.setEnabled(false);
inputsecret.addActionListener(this);
inputcishu.addActionListener(this);
inputcishu.setEnabled(false);
systemmenu.add(inputsecret);
systemmenu.add(inputcishu);
systemmenu.add(quit);
quit.addActionListener(this);
help.addActionListener(this);
helpmenu.add(help);
mb.add(filemenu);
mb.add(systemmenu);
mb.add(helpmenu);
this.setMenuBar(mb);
Container c = this.getContentPane();
BorderLayout b = new BorderLayout();
b.setVgap(10);
c.setLayout(b);
settextpanel();
c.add(textpanel, BorderLayout.NORTH);
setbuttonpanel();
setfopanel();
setdownpanel();
c.add(downpanel, BorderLayout.CENTER);
this.setSize(680,350);
//this.setResizable(false);
//this.pack();
this.setVisible(true);
}
void settextpanel()
{
textpanel = new JPanel();
textpanel.setLayout(new FlowLayout());
lbinput = new JLabel("输入:");
jtinput = new JTextField("", 10);
jtinput.setEditable(false);
jttishi = new JTextField(30);
lbnum = new JLabel("8");
lbnum0 = new JLabel("剩余次数:");
lbtishi = new JLabel("提示:");
jttishi.setEditable(false);
textpanel.add(lbinput);
textpanel.add(jtinput);
textpanel.add(lbtishi);
textpanel.add(jttishi);
textpanel.add(lbnum0);
textpanel.add(lbnum);
}
void setbuttonpanel()
{
String[] str =
{ "7", "8", "9", "4", "5", "6", "1", "2", "3", "确定", "0", "退格" };
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(4, 3, 4, 8));
for (int i = 0; istr.length; i)
{
btn = new JButton(str[i]);
btn.addActionListener(this);
buttonpanel.add(btn);
}
}
void setfopanel()
{
JButton btnright = new JButton("正确答案");
JPanel p = new JPanel();
btnright.addActionListener(this);
fopanel = new JPanel();
fopanel.setLayout(new BorderLayout());
btn = new JButton("开始");
btn.addActionListener(this);
tafo = new JTextArea(15,12);
p.setLayout(new GridLayout(1, 2));
p.add(btn);
p.add(btnright);
fopanel.add(p, BorderLayout.NORTH);
tafo.setEditable(false);
fopanel.add(tafo, BorderLayout.CENTER);
}
void setdownpanel()
{
downpanel = new JPanel();
downpanel.setLayout(new GridLayout(1, 2, 30, 0));
downpanel.add(buttonpanel);
downpanel.add(fopanel);
}
public static void main(String[] args)
{
new caishuzi();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("退出"))
{
int i = JOptionPane.showConfirmDialog(this, "确认退出java猜数字窗体代码?", "",
JOptionPane.YES_NO_OPTION);
//System.out.println(i);
if (i == 0)
{
System.exit(0);
}
}
if (s.equals("帮助"))
{
JOptionPane.showMessageDialog(this,
"猜数字游戏java猜数字窗体代码,可以输入限制次数java猜数字窗体代码,密码为123,\n在菜单:系统——输入密码 中输入!");
}
if (s.equals("输入密码"))
{
while (true)
{
String inputValuehttps://www.04ip.com/post/= "";
inputValue = https://www.04ip.com/post/JOptionPane.showInputDialog(this,"输入密码");
// System.out.println(inputValue);
if (inputValue =https://www.04ip.com/post/= null)
return;
if (inputValue.equals(strsecret))
{
jinru = true;
jttishi.setText("密码正确 , 点击开始");
inputsecret.setEnabled(false);
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "密码错误,重新输入?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
if (!jinru)
{
jttishi.setText("请输入密码:");
return;
}
if (s.equals("开始") || s.equals("重玩"))
{
StringBuffer sb = new StringBuffer("0123456789");
for (int i = 0; ic.length; i)
{
int n = (int) (Math.random() * sb.length());
c[i] = sb.charAt(n);
sb.deleteCharAt(n);
}
btn.setText("重玩");
start = true;
lbnum.setText(String.valueOf(xianzhi));
jisuan = false;
//System.out.println(c);
jttishi.setText("请输入四不重复的数:");
jtinput.setText("");
tafo.setText("");
}
if (s.compareTo("0") = 0s.compareTo("9") = 0)
{
jisuan = false;
if (!start)
jttishi.setText("点击开始!");
else
{
if (jtinput.getText().length()4)
{
if (jtinput.getText().indexOf(s)0)
{
jtinput.setText(jtinput.getText()s);
if (jtinput.getText().length()4)
jttishi.setText("还差"
(4 - jtinput.getText().length())"个数");
else
{
jttishi.setText("输入正确,点击确定");
jisuan = true;
}
} else
jttishi.setText("不能输入重复的数");
} else
{
jttishi.setText("不能超过四个数,点击确定");
jisuan = true;
}
}
}
if (s.equals("退格")start)
{
if (jtinput.getText().length()0)
{
jtinput.setText(jtinput.getText().substring(0,
jtinput.getText().length() - 1));
jttishi.setText("还差"(4 - jtinput.getText().length())"个数");
}
jisuan = false;
}
if (s.equals("确定"))
{
if (!start)
{
jttishi.setText("还没开始呢!");
return;
}
if (!jisuan)
{
jttishi.setText("输入不正确!");
return;
}
int numa = 0, numb = 0;
char[] c1 = jtinput.getText().toCharArray();
// System.out.println(c1);
for (int i = 0; ic1.length; i)
{
for (int j = 0; jc.length; j)
{
if (c1[i] == c[j]i != j)
{
numb;
}
if (c1[i] == c[j]i == j)
{
numa;
}
}
}
tafo.append(jtinput.getText()"\t"numa"A"numb"B"
"\n");
jtinput.setText("");
jisuan = false;
if (numa == 4)
{
jttishi.setText("答对了,再来一局吧!");
start = false;
btn.setText("开始");
tafo.append("恭喜,答对了! ");
} else
{
lbnum.setText(String
.valueOf(Integer.valueOf(lbnum.getText()) - 1));
if (Integer.valueOf(lbnum.getText()) == 0)
{
String str = "";
jttishi.setText("机会没有了!很遗憾");
for (int i = 0; ic.length; i)
str= c[i];
tafo.append("超过限制次数,失败!""正确答案是:"str);
start = false;
btn.setText("开始");
} else
jttishi.setText("输入四个数:");
}
}
if (s.equals("正确答案"))
{
if (!start)
{
jttishi.setText("还没开始呢!");
return;
}
String str = "";
for (int i = 0; ic.length; i)
str= c[i];
tafo.append("正确答案是:"str);
jttishi.setText("重新开始吧!");
start = false;
jisuan = false;
btn.setText("开始");
}
if (!start)
{
【java猜数字窗体代码 用java猜数字程序编写与实现】inputcishu.setEnabled(true);
} else
inputcishu.setEnabled(false);
if (s.equals("输入限制次数"))
{
while (true)
{
try
{
String str = JOptionPane.showInputDialog(this,
"输入小于15的整数数字:");
if (str == null)
return;
if (Integer.valueOf(str) = 15 Integer.valueOf(str) 0)
{
xianzhi=Integer.valueOf(str);
lbnum.setText(String.valueOf(xianzhi));
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
} catch (Exception ex)
{
int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?", "",
JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
}
}
我做课程设计的最初代码参考下吧运行时先看帮助~
java如何实现的简单猜数字游戏代码主要通过随机数与逻辑判断来实现游戏功能
实现代码如下java猜数字窗体代码:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 产生一个随机数
int number = (int) (Math.random() * 100)1;
// 加入count
int count = 0;
// 在这里加入最大值java猜数字窗体代码,和最小值
int max = 100;
int min = 1;
while (true) {
// 键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入java猜数字窗体代码你要猜的数据:("min"~"max")");
try {
count;
int guessNumber = sc.nextInt();
// 判断
if (guessNumbernumber) {
max = guessNumber;
System.out.println("你猜大java猜数字窗体代码了");
} else if (guessNumbernumber) {
min = guessNumber;
System.out.println("你猜小了");
} else {
System.out.println("恭喜你,花了"count"次就猜中了");
// 问是否继续
System.out.println("请问还要继续吗?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重写赋值随机数
number = (int) (Math.random() * 100)1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你输入的数据有误");
}
}
}
java编写一个“猜数”程序:该程序随机在1到1000的范围中选择一个供用户猜测的整数Java源程序附后 。
本程序的特点是:
(1) 文本框只能输入纯数字;
(2) 界面较美观;
(3) 代码可读性较好,有适当的注释;
(4) 窗体一出现就在桌面居中 。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GuessNumber {
private static final long serialVersionUID = 1L;
JFrame frame;
JTextField txtNum;//文本框
JButton btnStart;//按钮
JLabel lblPrompt;
JLabel lblMessage;
static int source = 0;
static Random rand = new Random();
public GuessNumber(){
frame = new JFrame("Guess Number");
JPanel pnl1, pnl2, pnl3, pnl4;
pnl1 = new JPanel();
pnl1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl2 = new JPanel();
pnl2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl3 = new JPanel();
pnl3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl4 = new JPanel();
pnl4.setLayout(new FlowLayout(FlowLayout.LEFT));
txtNum = new JTextField(10);
btnStart = new JButton("开始");
lblPrompt = new JLabel("htmlbodyI have a number between 1 and 1000 can you guess my number?br/Please enter your first guess./body/html");
lblMessage = new JLabel();
pnl1.add(lblPrompt);
pnl2.add(txtNum);
pnl3.add(lblMessage);
pnl4.add(btnStart);
frame.setLayout(new GridLayout(4, 1));
frame.add(pnl1);
frame.add(pnl2);
frame.add(pnl3);
frame.add(pnl4);
txtNum.addActionListener(this.new TextAction());
txtNum.addKeyListener(this.new KeyAction());
btnStart.addActionListener(this.new ButtonAction());
frame.setSize(400, 200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
public static void main(String[] args) {
new GuessNumber();
while((source=rand.nextInt(1000))==0);
}
//按钮单击后的事件处理
class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn == btnStart){
while((source=rand.nextInt(1000))==0);
txtNum.setEditable(true);
}
}
}
//文本框按回车后的事件处理
class TextAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField txt = (JTextField)e.getSource();
if(txt != txtNum){
return;
}
int num = Integer.parseInt(txtNum.getText());
if(num == source){
lblMessage.setText("Correct!");
txtNum.setEditable(false);
txtNum.setBackground(frame.getBackground());
}
else if(numsource){
lblMessage.setText("Too High");
txtNum.setBackground(Color.red);
}
else{
lblMessage.setText("Too Low");
txtNum.setBackground(Color.blue);
}
}
}
//限制文本框只能输入数字
class KeyAction implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
int k = e.getKeyChar();
String text = ((JTextField)e.getSource()).getText();
if(!((k47k 58) || (k==8 || k==KeyEvent.VK_PERIOD))){ //限制只能输入数字
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
if(text.length()4){ //限制数值的长度
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}
}
}
简单的java猜数字代码public static void main(String [] args){
int[] num_list={1,3,5,7,9};
java.util.Scanner str=new java.util.Scanner(System.in);
int num = str.nextInt();
for(int i=0;inum_list.length;i){
if(num!=nullnum.equals("")){
if(num_list[i]==num){
System.out.println("恭喜你,猜对了!");
}else{
System.out.println("抱歉,你猜错了!");
}
}
}
}
java猜数字窗体代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于用java猜数字程序编写与实现、java猜数字窗体代码的信息别忘了在本站进行查找喔 。
推荐阅读
- gts450什么cpu,gts450 cpuz
- b站看直播画质,b站看直播画质变差
- go语言并行笔记 go语言实战笔记
- 怎么把文字写入文件javascript,如何将js文件放入html
- 网络游戏反映什么思想,网络游戏的正面影响
- 角色扮演观察记录游戏反思,角色扮演游戏观察记录表
- 三角函数的c语言输入 三角函数 c语言
- 笔记本硬盘插槽怎么取下,笔记本电脑硬盘插槽
- gis怎么用算法,gis常用算法