猫苘的Java练习项目1:Java1.8——复习Frame设置猜拳游戏

界面图:
在这里插入图片描述

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main {

    public static void main(String[] args) {
        madoFrame();

    }

    private static void madoFrame(){
        // Frame是带有标题和边框的顶级窗口
        Frame f = new Frame("猜拳游戏");
        Label p1 = new Label("石头");
        Label p2 = new Label("石头");
        p1.setLocation(100,50);
        p2.setLocation(150,50);
        Label str = new Label();
        str.setLocation(100,25);
        Button b = new Button("开始");
        b.setSize(10,30);
        b.setLocation(100,0);
        //将组件添加到Frame中
        f.add(p1);
        f.add(p2);
        f.add(str);
        f.add(b);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int count = 0;count < 10;count++){
                    Fist fist = new Fist();
                    String n1 = fist.getP1();
                    String n2 = fist.getP2();
                    p1.setText(n1);
                    p2.setText(n2);
                    if (n1.equals(n2)){
                        str.setText("不相上下");
                    }else {
                        if (("剪刀").equals(n1) && ("石头").equals(n2)){
                            str.setText("阿廖赢了");
                        }else if(("剪刀").equals(n1) && ("布").equals(n2)){
                            str.setText("猫苘赢了");
                        }else if(("石头").equals(n1) && ("布").equals(n2)){
                            str.setText("阿廖赢了");
                        }else if(("石头").equals(n1) && ("剪刀").equals(n2)){
                            str.setText("猫苘赢了");
                        }else if(("布").equals(n1) && ("剪刀").equals(n2)){
                            str.setText("阿廖赢了");
                        }else if(("布").equals(n1) && ("石头").equals(n2)){
                            str.setText("猫苘赢了");
                        }
                    }
                }
            }
        });
        f.setLayout(new java.awt.GridLayout());
        //设置框架大小
        f.setSize(500, 100);
        center(f);
        //显示窗体
        f.setVisible(true);
        f.addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    private static void center(Component c) {
        Toolkit kit = Toolkit.getDefaultToolkit();
        int x = (kit.getScreenSize().width - c.getWidth()) / 2;
        int y = (kit.getScreenSize().height - c.getHeight()) / 2;
        c.setLocation(x, y);
    }
}

但是关闭不了,只能参考他人的三个方法java中关闭Frame窗体的方法
改成这样子:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main extends  Frame{
    public void display(){
        this.setTitle("猜拳游戏");
        Label p1 = new Label("石头");
        Label p2 = new Label("石头");
        p1.setLocation(100,50);
        p2.setLocation(150,50);
        Label str = new Label();
        str.setLocation(100,25);
        Button b = new Button("开始");
        b.setSize(10,30);
        b.setLocation(100,0);
        //将组件添加到Frame中
        this.add(p1);
        this.add(p2);
        this.add(str);
        this.add(b);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int count = 0;count < 10;count++){
                    Fist fist = new Fist();
                    String n1 = fist.getP1();
                    String n2 = fist.getP2();
                    p1.setText(n1);
                    p2.setText(n2);
                    if (n1.equals(n2)){
                        str.setText("不相上下");
                    }else {
                        if (("剪刀").equals(n1) && ("石头").equals(n2)){
                            str.setText("阿廖赢了");
                        }else if(("剪刀").equals(n1) && ("布").equals(n2)){
                            str.setText("猫苘赢了");
                        }else if(("石头").equals(n1) && ("布").equals(n2)){
                            str.setText("阿廖赢了");
                        }else if(("石头").equals(n1) && ("剪刀").equals(n2)){
                            str.setText("猫苘赢了");
                        }else if(("布").equals(n1) && ("剪刀").equals(n2)){
                            str.setText("阿廖赢了");
                        }else if(("布").equals(n1) && ("石头").equals(n2)){
                            str.setText("猫苘赢了");
                        }
                    }
                }
            }
        });
        this.setLayout(new java.awt.GridLayout());
        center(this);
        //设置框架大小
        this.setSize(500, 100);

        //显示窗体
        this.setVisible(true);
    }

    public static void main (String args[]){
        Main f = new Main();
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        f.display();
    }

    private static void center(Component c) {
        Toolkit kit = Toolkit.getDefaultToolkit();
        int x = (kit.getScreenSize().width - c.getWidth()) / 2;
        int y = (kit.getScreenSize().height - c.getHeight()) / 2;
        c.setLocation(x, y);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值