赶紧,用JAVA编个猜数字的小游戏。
导入Java . awt . event . action listener;
导入Java . awt . event . key event;
导入Java . awt . event . key listener;
导入javax . swing . jbutton;
导入javax . swing . jframe;
导入javax . swing . jlabel;
导入javax . swing . jmenu;
导入javax . swing . jmenubar;
导入javax . swing . jmenuitem;
导入javax . swing . joptionpane;
导入javax . swing . jpanel;
导入javax . swing . jtextarea;
导入javax . swing . jtextfield;
导入javax . swing . event . caretevent;
导入javax . swing . event . caret listener;
公共类Guess扩展JFrame{
private int计数;
私有字符串rs =
私有JTextField trs
私有JTextField tinput
私人JTextArea ta
私有int a;
私有int b;
私有JLabel消息;
公共静态void main(String[] args) {
新猜想();
}
公众猜测(){
init menu();
JLabel l1=new JLabel("最终结果");
TRS = new JTextField(5);
TRS . set editable(false);
TRS . set enabled(false);
JLabel l2=new JLabel("请输入一个数字");
tin put = new JTextField(5);
tin put . set editable(false);
ta=new JTextArea(8,30);
ta . set editable(false);
final JButton OK = new JButton(" OK ");
ok . set enabled(false);
JPanel pa = new JPanel();
add(pa,“中心”);
pa . add(l 1);
pa . add(TRS);
pa . add(L2);
pa . add(tinput);
tinput . addcaretlistener(new caret listener(){
公共无效care update(care event e){
if(validate_input())
ok.setEnabled(真);
其他
ok . set enabled(false);
}
});
tinput . addkey listener(new key listener(){
公共void按键(按键事件e) {
if(e.getKeyCode() == KeyEvent。VK _回车)
ok . doclick();
}
public void key released(key event arg 0){
// TODO自动生成的方法存根
}
public void key typed(key event arg 0){
// TODO自动生成的方法存根
}
});
pa . add(ok);
pa . add(ta);
Msg=new JLabel("请点击菜单开始游戏");
pa . add(msg);
ok.addActionListener(新ActionListener(){
public void action performed(action event e){
setAB();
count++;
Ta.setText(ta.getText()+"第"+count+"次尝试:"+tin put . gettext()+" > & gt;& gt& gt& gt& gt& gt& gt>+A+" A "+b+ " B \ n));
tinput . settext(" ");
if(count==6){
joptionpane . showmessagedialog(null,“游戏结束”);
tin put . set editable(false);
Msg.setText("请点击菜单开始游戏");
tr . settext(RS);
rs =
}
if(a==4){
joptionpane . showmessagedialog(null,“你赢了”);
tin put . set editable(false);
Msg.setText("请点击菜单开始游戏");
tr . settext(RS);
rs =
}
a = 0;b = 0;
}
});
setSize(350,300);
setLocation(500,300);
setDefaultCloseOperation(EXIT _ ON _ CLOSE);
set visible(true);
}
public void initMenu(){
JMenuBar MB = new JMenuBar();
JMenu m = new JMenu(" function ");
MB . add(m);
j menuitem mi 1 = new j menuitem("开始游戏");
JMenuItem mi2 = new JMenuItem(" exit ");
m . add(mi 1);
m . add(mi2);
mi1.addActionListener(新ActionListener(){
public void action performed(action event e){
begin();
}
});
mi2.addActionListener(新ActionListener(){
public void action performed(action event e){
dispose();
}
});
MB . set visible(true);
add(mb,“北”);
}
public void begin(){
for(int I = 0;RS . length()& lt;4;i++){
int ran =(int)(math . random()* 10);
如果(!rs.contains(String.valueOf(ran)))
RS+= string . value of(ran);
}
TRS . settext(" * * * * ");
tin put . set editable(true);
count = 0;
ta.setText(空);
msg.setText(空);
}
公共布尔validate_input(){
string input = tin put . gettext();
返回input . matches(" \ \ d { 4 } ");
}
公共void setAB(){
string input = tin put . gettext();
for(int I = 0;我& lt4;i++){
if(RS . charat(I)= = input . charat(I))
a++;
}
字符串标志= " ";
for(int I = 0;我& lt4;i++){
for(int j = 0;j & lt4;j++)
if(RS . charat(I)= = input . charat(j)& amp;& amp!flag . contains(input . substring(j,j+1))){
b++;
flag+=input.substring(j,j+1);
}
}
b-= a;
}
}