⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untitled1.java

📁 java 猜大小 图形界面 简单的java程序 适合初学java GUI着
💻 JAVA
字号:
package untitled1;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Untitled1 extends Applet  implements ActionListener {
    Frame f = new Frame("哈哈猜大小");
    Label l = new Label("随机产生整数数0~9,大于等于5为大,小于等于4为小.");
    Label lb = new Label("");
    Label la = new Label("");
    Label lc1 = new Label("您猜对的次数:");
     Label lc2 = new Label("0");
      Label lc3 = new Label("您猜错的次数:");
       Label lc4 = new Label("0");
       Label lc5 = new Label("答案是:");
    Button b1 = new Button("小");
    Button b2 = new Button("大");
    Panel pan = new Panel();
    public Untitled1() {

        b1.addActionListener(this);
        b2.addActionListener(this);

        b1.setSize(80, 20);
        b2.setSize(80, 20);

        pan.setSize(100, 100);

        pan.setLayout(new GridLayout(5,2));

        pan.add(b2);  pan.add(b1);
        pan.add(lc1);pan.add(lc2);
        pan.add(lc3);pan.add(lc4);
        pan.add(lc5);pan.add(lb);
        f.add(l,BorderLayout.NORTH);
        f.add(pan,BorderLayout.CENTER);
        f.add(la,BorderLayout.SOUTH);
        f.setSize(300, 200);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e)
                    {System.exit(0);}
        });
        f.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        Random x = new Random();
        int y = x.nextInt(10);

        String s = e.getActionCommand();
        if (s == "小") {
            lb.setText(Integer.toString(y));
            if (y <= 4)
            {
                la.setText("恭喜,您猜对了!");
                lc2.setText(Integer.toString(Integer.parseInt(lc2.getText())+1));
            }
              else{
                  la.setText("对不起,您猜错了!");
                  lc4.setText(Integer.toString(Integer.parseInt(lc4.getText()) +
                                             1));
              }


        }
        if (s == "大") {lb.setText(Integer.toString(y));
            if (y >= 5)
            {

                la.setText("恭喜,您猜对了!");
                lc2.setText(Integer.toString(Integer.parseInt(lc2.getText()) +
                                             1));
            }
            else
            {
                la.setText("对不起,您猜错了!");
                lc4.setText(Integer.toString(Integer.parseInt(lc4.getText()) +
                                             1));
            }
        }


    }

    public static void main(String args[]) {
        Untitled1 pp = new Untitled1();

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -