package First;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class _a6_猜猜看页面编程 {
public static void main(String[] args) {
MyLianLianKan mllk = new MyLianLianKan("猜猜看");
mllk.mySet();
}
}
class MyLianLianKan extends JFrame{
//创建标签
JLabel lab, lab2 , lab3;
//创建按钮
JButton jt;
//创建面板
JPanel jp1,jp2,jp3;
//创建单行文本框
JTextField jtf;
MyLianLianKan(String title) {
super(title);
}
public void mySet() {
//创建组件
lab = new JLabel("请输入你猜的数(1——100之内的整数):");
lab2 = new JLabel("");
lab3 = new JLabel("");
jtf = new JTextField(15) ;
jt = new JButton("确定");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jt.addActionListener(new MyMonintor3());
jp1.setLayout(new GridLayout(2,1));
this.add(jp1);
jp1.add(jp2);
jp1.add(jp3);
jp2.add(lab);
jp2.add(jtf);
jp2.add(jt);
jp3.add(lab2);
jp3.add(lab3);
//窗体的属性设置(前两个参数时位置,后两个参数是大小)
this.setBounds(300, 300, 530, 160);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//内部类
class MyMonintor3 implements ActionListener{
public void actionPerformed(ActionEvent e) {
String s = jtf.getText();
//将字符串转换为数字
int n =Integer.parseInt(s);
lab2.setText("反馈信息:");
if(n >= 1 && n <= 100) {
lab3.setText("恭喜你,猜对了");
lab3.setBackground(Color.GREEN);
}else {
lab3.setText("很抱歉,猜错了");
lab3.setBackground(Color.red);
}
lab3.setOpaque(true);
}
}
}