数字记忆小游戏

package y1;


import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;


import javax.swing.*;
import java.awt.*;


 public class GameFrame extends JFrame implements ActionListener,ItemListener{
static int j=4;
static int m=1;
JPanel pan=new JPanel();
JButton bt1,bt2,bt3;
JLabel lab;
JMenuBar mb;
JMenu it,it1;
JComboBox cb;
JRadioButtonMenuItem b1,b2,b3;
Color co=new Color(200,100,50);
GameFrame(String b){
super(b);
setSize(150,200);
this.setLocationRelativeTo(null);
setContentPane(pan);
mb=new JMenuBar();
setJMenuBar(mb);
lab=new JLabel("等级");
it=new JMenu();
it=new JMenu("设置");
it1=new JMenu("等级");
cb=new JComboBox();
String str[]={"低","中","高"};
for(int i=0;i<str.length;i++)
cb.addItem(str[i]);
cb.addItemListener(this);
it.add(it1);
mb.add(it);
it.add(it1);
pan.add(lab);
pan.add(cb);
bt1=new JButton("开始游戏");
bt1.addActionListener(this);
bt2=new JButton("游戏说明");
bt2.addActionListener(this);
bt3=new JButton("退出游戏");
bt3.addActionListener(this);
pan.add(bt1);
pan.add(bt2);
pan.add(bt3);
pan.setBackground(co);
it.setBackground(Color.GREEN);
bt1.setBackground(Color.GREEN);
bt2.setBackground(Color.GREEN);
bt3.setBackground(Color.GREEN);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public void itemStateChanged(ItemEvent d){
if(cb.getSelectedIndex()==0) {
m=1;
j=4;
}

if(cb.getSelectedIndex()==1) {
m=3;
j=4;
}
if(cb.getSelectedIndex()==2){
m=5;
j=4;
}
}
public void actionPerformed(ActionEvent c){
if(c.getSource()==bt1){
Frame1 f1=new Frame1("默背");
}
if(c.getSource()==bt2){
JOptionPane.showMessageDialog(null,
"背下字符串,按下ok进行默写",
"说明",
JOptionPane.INFORMATION_MESSAGE);
}
if(c.getSource()==bt3){
setVisible(false);
}
}

}
 class Frame1 extends JFrame implements ActionListener{
JPanel pan1=new JPanel();
JLabel lb1=new JLabel(); 
JButton btn1,btn;
static String c,s3;
StringBuffer s2=new StringBuffer();
Color co1=new Color(200,100,50);
Frame1(String t){
super(t);
setSize(300,200);
this.setLocationRelativeTo(null);
setContentPane(pan1);
btn1=new JButton("ok");
btn1.addActionListener(this);
btn=new JButton("Cancel");
btn.addActionListener(this);
pan1.add(lb1);
pan1.add(btn1);
pan1.add(btn);
pan1.setBackground(co1);
btn1.setBackground(Color.YELLOW);
GameFrame.j=GameFrame.j+GameFrame.m;
int[] s=new int[GameFrame.j];
Random rand=new Random();
for(int i=0;i<GameFrame.j;i++){
s[i]=rand.nextInt((0)+10);
s2.append(s[i]);
}
String s1=s2.toString();
lb1.setText(s1);
c=lb1.getText();
this.setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
setVisible(false);
Frame2 f2=new Frame2("输入");
}
if(e.getSource()==btn)
setVisible(false);
}

 }


class Frame2 extends JFrame implements ActionListener{
JPanel pan2=new JPanel();
JLabel lb2=new JLabel();
JTextArea ta;
JButton btn2,btn3;
Icon icon=new ImageIcon("01.jpg");
Color co2=new Color(100,100,50);
static String c1;
Frame2(String t){
super(t);
setSize(300,400);
this.setLocationRelativeTo(null);
setContentPane(pan2);
ta=new JTextArea(5,20);
btn2=new JButton("确认");
btn3=new JButton("取消");
btn2.addActionListener(this);
btn3.addActionListener(this);
pan2.add(ta);
pan2.add(btn2);
pan2.add(btn3);
pan2.add(lb2);
pan2.setBackground(co2);
btn2.setBackground(Color.YELLOW);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent a){
if(a.getSource()==btn2){
c1=ta.getText();
if(Frame1.c.equals(Frame2.c1)){
setVisible(false);
Frame1 f=new Frame1("默背");

}
else {
lb2.setIcon(icon);
JOptionPane.showMessageDialog(this,"正确答案是"+Frame1.c );
GameFrame.j=GameFrame.j-GameFrame.m;
}
}
if(a.getSource()==btn3){
setVisible(false);
GameFrame.j=GameFrame.j-GameFrame.m;
}
}

}

public class Text1 {


public static void main(String[] args) {
// TODO Auto-generated method stub
GameFrame g=new GameFrame("小游戏");


}


}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值