📄 caishuframe.java
字号:
package caishu;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CaishuFrame extends JFrame {
int b, i = 1;
public CaishuFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
btnCs.setBounds(new Rectangle(130, 204, 125, 36));
btnCs.setFont(new java.awt.Font("楷体_GB2312", Font.BOLD, 14));
btnCs.setText("猜 数");
btnCs.addActionListener(new CaishuFrame_btnCs_actionAdapter(this));
jButton1.setBounds(new Rectangle(121, 254, 144, 36));
jButton1.setFont(new java.awt.Font("楷体_GB2312", Font.BOLD, 14));
jButton1.setText("生 成 随 机 数");
jButton1.addActionListener(new CaishuFrame_jButton1_actionAdapter(this));
txtSr.setFont(new java.awt.Font("宋体", Font.BOLD, 17));
txtSr.setBounds(new Rectangle(156, 66, 221, 46));
jLabel1.setFont(new java.awt.Font("楷体_GB2312", Font.BOLD, 16));
jLabel1.setText("输 入 数 :");
jLabel1.setBounds(new Rectangle(18, 67, 108, 45));
lblXX.setFont(new java.awt.Font("楷体_GB2312", Font.BOLD, 16));
lblXX.setForeground(Color.yellow);
lblXX.setBounds(new Rectangle(32, 123, 340, 62));
jLabel2.setFont(new java.awt.Font("楷体_GB2312", Font.BOLD, 16));
jLabel2.setForeground(Color.red);
jLabel2.setText(" 猜 数 游 戏 ");
jLabel2.setBounds(new Rectangle(29, 11, 348, 43));
this.getContentPane().setBackground(new Color(154, 154, 227));
this.getContentPane().add(jButton1);
this.getContentPane().add(btnCs);
this.getContentPane().add(jLabel1);
this.getContentPane().add(txtSr);
this.getContentPane().add(jLabel2);
this.getContentPane().add(lblXX);
this.setTitle("猜数0-100");
this.setSize(400, 350);
this.setLocation(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
CaishuFrame caishuframe = new CaishuFrame();
}
JButton btnCs = new JButton();
JButton jButton1 = new JButton();
JTextField txtSr = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel lblXX = new JLabel();
JLabel jLabel2 = new JLabel();
/**
* 生成随机数
* @param e ActionEvent
*/
public void jButton1_actionPerformed(ActionEvent e) {
Random obj = new Random();
b = obj.nextInt(100);
System.out.println(b);
}
/**
* 猜数
* @param e ActionEvent
*/
public void btnCs_actionPerformed(ActionEvent e) {
//
// if (txtSr.getText().trim().length() == 0) {
// lblXX.setText("请输入您要猜的数!");
// return;
// }
try {
int a = Integer.parseInt(txtSr.getText().trim());
System.out.println(b+" "+" "+a);
if (a > b) {
lblXX.setText("大量");
System.out.println("大了! 您猜了" + i + "次");
}
if (a < b) {
lblXX.setText("小了! 您猜了" + i + "次");
System.out.println("小了! 您猜了" + i + "次");
}
if (a == b) {
System.out.println("正确! 您猜了" + i + "次");
lblXX.setText("正确! 您猜了" + i + "次");
i=0;
}
i++;
} catch (Exception ex) {
lblXX.setText("请输入数字!!");
}
lblXX.setText("");
txtSr.setText("");
}
class CaishuFrame_btnCs_actionAdapter implements ActionListener {
private CaishuFrame adaptee;
CaishuFrame_btnCs_actionAdapter(CaishuFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCs_actionPerformed(e);
}
}
class CaishuFrame_jButton1_actionAdapter implements ActionListener {
private CaishuFrame adaptee;
CaishuFrame_jButton1_actionAdapter(CaishuFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -