求JAVA人机猜代码,类似界面。
导入Java . awt . borderlayout;
导入Java . awt . dimension;
导入Java . awt . gridbagconstraints;
导入Java . awt . gridbaglayout;
导入Java . awt . event . action event;
导入Java . util . random;
导入javax . swing . abstract action;
导入javax . swing . box;
导入javax . swing . jbutton;
导入javax . swing . jframe;
导入javax . swing . jlabel;
导入javax . swing . jpanel;
导入javax . swing . jtextfield;
公共类Demo2扩展JFrame {
私有JLabel lb1,lb2,lb3,lb4//提示标签
private JTextField ta1,ta2//两个文本框
私有JButton b1,b2,B3;//三个按钮
私有JPanel p1,p2;//两个JPanel面板
公共演示2() {
//初始化所有组件
Lb1 = new JLabel("欢迎来到人机猜谜节目");
lb2 = new JLabel(" You punch:");
Lb3 = new JLabel("电脑打孔:");
lb4 = new JLabel(" result ");
ta 1 = new JTextField();
ta1.setPreferredSize(新维度(60,60));//设置大小
ta 1 . set editable(false);//设置不可编辑。
ta2 = new JTextField();
ta2.setPreferredSize(新维度(60,60));
ta2 . set editable(false);//设置不可编辑。
B1 = new JButton("剪刀");
B2 =新JButton(“石头”);
B3 =新JButton("布");
p 1 = new JPanel();
p2 = new JPanel();
//设置第一个面板的内容
box box = box . createverticalbox();
box box 1 = box . createhorizontalbox();
box 1 . add(lb2);
box 1 . add(ta 1);
box 1 . add(lb3);
box 1 . add(ta2);
box . add(lb 1);
box . add(box . createverticalstrut(40));
box . add(box 1);
box . add(box . createverticalstrut(10));
box . add(lb4);
box . add(new JLabel());
p1.add(框);
//设置第二个面板
p2 . set layout(new gridbag layout());//使用GridBagLayout布局管理器
p2.setPreferredSize(新维度(0,60));
GridBagConstraints G2 = new GridBagConstraints();
g2.fill = GridBagConstraints。两者都有;
G2 . weightx = 1.0;
G2 . weighty = 1.0;
G2 . gridx = 0;
G2 . gridy = 0;
p2.add(b1,G2);
G2 . gridx = 1;
p2.add(b2,G2);
G2 . gridx = 2;
p2.add(b3,G2);
//为3个按钮添加事件
b 1 . addactionlistener(new button action());
B2 . addactionlistener(new button action());
B3 . addactionlistener(new button action());
this.getContentPane()。添加(p 1);
this.getContentPane()。添加(p2,BorderLayout。南);
This.setTitle(“机器人猜谜游戏”);
this.setSize(300,300);
this . set visible(true);
this . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);
}
//事件类
class buttonAction扩展AbstractAction{
@覆盖
public void action performed(action event e){
if(e.getSource()==b1){
Ta1.setText("剪刀");
init(ta 1 . gettext());
}else if(e.getSource()==b2){
ta 1 . settext(" stone ");
init(ta 1 . gettext());
}else if(e.getSource()==b3){
ta 1 . settext(" cloth ");
init(ta 1 . gettext());
}
}
//模拟电脑打孔产生三个随机数。0代表剪刀,1代表石头,2代表布。
公共字符串getQuan(){
String str =
int num=new Random()。nextInt(3);
if(num==0){
Str= "剪刀";
}else if(num==1){
Str = " stone
}else if(num==2){
Str = " cloth
}
返回字符串;
}
//判断输赢的方法
公共字符串isying(字符串s1,字符串s2){
字符串s =
if(s1.equals(s2)){
S = " tie
}else if(s1.equals("剪刀")&;& ampS2.equals("布") (
S= "你赢了";
} else if(s 1 . equals(" stone ")&;& ampS2.equals(“剪刀”)(
S= "你赢了";
} else if(s 1 . equals(" cloth ")&;& amp等于(石头)
S= "你赢了";
}否则{
S= "计算机获胜";
}
返回s;
}
公共void初始化(字符串wo){
字符串sy =//保存输赢结果
string dncq = get quan();//电脑打孔
if(wo.equals(dncq)){
Sy = " draw
}else if(wo.equals("剪刀")& amp;Dncq.equals("布")) (
Sy= "你赢了";
} else if(wo . equals(" stone ")&;& ampDncq.equals("剪刀")) (
Sy= "你赢了";
} else if(wo . equals(" cloth ")&;& ampDncq.equals("石头")) (
Sy= "你赢了";
}否则{
Sy= "电脑赢";
}
ta2 . settext(dncq);//电脑打孔
Lb4.setText("结果:"+sy);
}
}
公共静态void main(String[] args) {
new demo 2();
}
}