JAVA的继承,构造函数,窗体

时间:2021-11-12 12:52:24
 import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class QQ extends JFrame //继承JFrame
{
QQ() //构造函数
{
display();//调用display()函数
}
JButton btn1;
JButton btn2;
JTextField jtf1;
JPasswordField jpf1; //密码文本
class MyListener extends WindowAdapter //适配器 是扩展 不需要覆盖WindowAdapter中的所有方法 功能代码较多
{
public void windowClosing(WindowEvent e)
{
System.out.println("windowClosing");
int res = JOptionPane.showConfirmDialog(null,"是否退出程序应用","提示", JOptionPane.YES_NO_OPTION); //弹出消息框
System.out.println("res =" + res);
if(res == 0)
{
System.out.println("退出");
System.exit(1);// 退出
System.out.println("退出1"); //不输出 因为exit先执行
}
else if(res == 1)
{
System.out.println("不退出");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//System.exit(1);// 退出
// 不退出
}
}
}
class BtnAction implements ActionListener //btn事件
{
public void actionPerformed(ActionEvent e)
{
System.out.println("actionPerformed");
if(jtf1.getText().equals("jk") && jpf1.getText().equals("jk"))
{
System.out.println("OK");
dispose();
(new JFrame("主窗口")).setVisible(true);
}
else
{
System.out.println("Error");
JOptionPane.showConfirmDialog(null,"密码错误","提示", JOptionPane.DEFAULT_OPTION);
}
}
}
class BtnAction2 implements ActionListener //内部类
{
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
JButton b = (JButton)o; //强制类型转换
System.out.println("芝麻开门" + btn2.getText()); //类的成员/函数,可以在内部类中使用 if(b == btn2) //b.getSource == "注册"
{
jtf1.setText("");
jpf1.setText("");
}
}
}
public void display()
{
//JFrame frm = new JFrame(); //窗体
ImageIcon img1 = new ImageIcon("timg.gif"); //背景
JLabel background1 = new JLabel(img1);
this.getLayeredPane().add(background1, new Integer(Integer.MIN_VALUE));
//background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
background1.setBounds(0, 0, 425, 450);
ImageIcon img2 = new ImageIcon("33.gif");
JLabel background2 = new JLabel(img2);
this.getLayeredPane().add(background2, new Integer(Integer.MIN_VALUE));
background2.setBounds(0, 0, img2.getIconWidth(), img2.getIconHeight()); jtf1 = new JTextField(30); //文本
//jtf1.setColumns(10);
jtf1.setSize(200,35);
jtf1.setLocation(130,130);
jpf1 = new JPasswordField(30); //密码文本
//jpf1.setEchoChar('#'); 设置密码文本内容
jpf1.setSize(200,35);
jpf1.setLocation(130,180); JLabel lab1 = new JLabel("账号:"); //标题
Font fnt = new Font("Serief",Font.ITALIC+Font.BOLD,15);
lab1.setFont(fnt);
lab1.setBackground(Color.BLUE); //label的背景颜色
lab1.setOpaque(true); //label是否透明
lab1.setForeground(Color.YELLOW); //label前景色
JLabel lab2 = new JLabel("密码:");
lab1.setSize(50,30);
lab2.setSize(50,30);
lab1.setLocation(70,130);
lab2.setLocation(70,180);
btn1 = new JButton("登录");
btn1.setBounds(100,230,180,50);
Image im1 = (new ImageIcon("QQ.png")).getImage(); //图标
this.setIconImage(im1);
//ImageIcon im2 = new ImageIcon("77.png"); //图标
ImageIcon im2 = new ImageIcon("./77.png");
btn1.setIcon(im2);
this.setLayout(null); //布局--绝对布局
MyListener ltn = new MyListener(); //监听
this.addWindowListener(ltn);
BtnAction act = new BtnAction(); //btn事件
btn1.addActionListener(act);
btn2 = new JButton("重置");
btn2.setBounds(300,230,100,50);
BtnAction2 act2 = new BtnAction2();
btn2.addActionListener(act2);
//frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(425,325);
this.setLocation(700,500);
this.setTitle("QQ登录");
this.setResizable(false);
this.add(btn1);
this.add(btn2);
this.add(lab1);
this.add(lab2);
this.add(jpf1);
this.add(jtf1);
this.add(background1);
this.add(background2);
this.setVisible(true);
System.out.println("OK");
}
public static void main(String[] args)
{
QQ Event1 = new QQ();
System.out.println("OK");
}
}

JAVA的继承,构造函数,窗体

JAVA的继承,构造函数,窗体

JAVA的继承,构造函数,窗体