博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java继承系列之添加一个LinkLable类
阅读量:5936 次
发布时间:2019-06-19

本文共 7744 字,大约阅读时间需要 25 分钟。

1 import java.awt.*;  2 import javax.swing.*;  3 import javax.swing.JFrame;  4 import java.awt.event.WindowListener;  5 import java.awt.event.WindowEvent;  6 import java.awt.event.WindowAdapter;  7 import java.awt.event.ActionListener;  8 import java.awt.event.ActionEvent;  9 import java.awt.event.MouseListener; 10 import java.awt.event.MouseAdapter; 11 import java.awt.event.MouseEvent; 12 import java.awt.Desktop; 13 import java.awt.Cursor; 14  15 public class LinkLabel extends JLabel implements MouseListener 16 { 17     protected String text; 18     protected boolean isSupported; 19     protected JFrame parent = null; 20     public LinkLabel(String text, JFrame parent)   //方法 21     { 22         this.text = text;   //局部变量传递值给成员变量 23         this.parent = parent;   24         try  25         { 26             this.isSupported = Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE); 27         } catch (Exception e)  28         { 29             this.isSupported = false; 30         } 31         setText(false); 32         addMouseListener(this); 33     } 34     private void setText(boolean b) //设置LinkLable的文本颜色 35     { 36         if (b) 37             setText("" + text); 38         else 39             setText("" + text); 40     } 41     public void mouseEntered(MouseEvent e)  42     { 43         setText(isSupported); 44         if (isSupported) 45             setCursor(new Cursor(Cursor.HAND_CURSOR));  //光标变成手型 46     } 47     public void mouseExited(MouseEvent e)  48     { 49         setText(false); 50     } 51     public void mouseReleased(MouseEvent e) 52     { 53          54     } 55     public void mousePressed(MouseEvent e) 56     { 57          58     } 59     public void mouseClicked(MouseEvent e)  60     { 61     } 62 } 63  64 class FixFrame extends JFrame 65 { 66     this.setResizable(false); 67     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 } 69 class ResizableFrame extends JFrame 70 { 71     this.setResizable(true); 72     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 73 } 74 class LoginFrame extends FixFrame 75 { 76     LoginFrame()  //构造函数 77     { 78         display();//调用display()函数 79     } 80     private JButton btn1; 81     private JButton btn2; 82     private JTextField jtf1; 83     private JPasswordField jpf1; //密码文本 84     private ImageIcon img1; 85     private JLabel background1; 86     private ImageIcon img2; 87     private JLabel background2; 88     private JLabel lab1; 89     private Font fnt; 90     private JLabel lab2; 91     private Image im1; 92     private ImageIcon im2; 93     private MyListener ltn; 94     private BtnAction act; 95     private BtnAction2 act2; 96     class MyListener extends WindowAdapter //适配器   是扩展  不需要覆盖WindowAdapter中的所有方法 功能代码较多 97     { 98         public void windowClosing(WindowEvent e) 99         {100             System.out.println("windowClosing");101             int res = JOptionPane.showConfirmDialog(null,"是否退出程序应用","提示", JOptionPane.YES_NO_OPTION); //弹出消息框102             System.out.println("res =" + res);103             if(res == 0)104             {105                 System.out.println("退出");106                 System.exit(1);// 退出107                 System.out.println("退出1"); //不输出  因为exit先执行108             }109             else if(res == 1)110             {111                 System.out.println("不退出");112                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);113                 //System.exit(1);// 退出114                 // 不退出115             }116         }117     }118     class BtnAction implements ActionListener  //btn事件119     {120         public void actionPerformed(ActionEvent e)121         {122             System.out.println("actionPerformed");123             if(jtf1.getText().equals("jk") && jpf1.getText().equals("jk"))124             {125                 System.out.println("OK");126                 dispose();127                 (new JFrame("主窗口")).setVisible(true);128             }129             else130             {131                 System.out.println("Error");132                 JOptionPane.showConfirmDialog(null,"密码错误","提示", JOptionPane.DEFAULT_OPTION);133             }134         }135     }136     class BtnAction2 implements ActionListener   //内部类137     {138         public void actionPerformed(ActionEvent e)139         {140             Object o = e.getSource();141             JButton b = (JButton)o;  //强制类型转换142             System.out.println("芝麻开门" + btn2.getText());  //类的成员/函数,可以在内部类中使用143             144             if(b == btn2) //b.getSource == "重置"145             {146                 jtf1.setText("");147                 jpf1.setText("");148             }149         }150     }151     public void display()152     {153         //JFrame frm = new JFrame(); //窗体154         img1 = new ImageIcon("timg.gif"); //背景155         background1 = new JLabel(img1);156         this.getLayeredPane().add(background1, new Integer(Integer.MIN_VALUE)); 157         //background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());158         background1.setBounds(0, 0, 425, 450);159         img2 = new ImageIcon("33.gif"); 160         background2 = new JLabel(img2);161         this.getLayeredPane().add(background2, new Integer(Integer.MIN_VALUE)); 162         background2.setBounds(0, 0, img2.getIconWidth(), img2.getIconHeight());163         164         jtf1 = new JTextField(30); //文本165         //jtf1.setColumns(10);166         jtf1.setSize(200,35);167         jtf1.setLocation(130,130);168         jpf1 = new JPasswordField(30); //密码文本169         //jpf1.setEchoChar('#');     设置密码文本内容170         jpf1.setSize(200,35);171         jpf1.setLocation(130,180);172         173         lab1 = new JLabel("账号:"); //标题174         fnt = new Font("Serief",Font.ITALIC+Font.BOLD,15);175         lab1.setFont(fnt);176         lab1.setBackground(Color.BLUE); //label的背景颜色177         lab1.setOpaque(true); //label是否透明178         lab1.setForeground(Color.YELLOW); //label前景色179         lab2 = new JLabel("密码:");180         lab1.setSize(50,30);181         lab2.setSize(50,30);182         lab1.setLocation(70,130);183         lab2.setLocation(70,180);184         btn1 = new JButton("登录");185         btn1.setBounds(100,230,180,50);186         im1 = (new ImageIcon("QQ.png")).getImage(); //图标187         this.setIconImage(im1);188         //ImageIcon im2 = new ImageIcon("77.png"); //图标189         im2 = new ImageIcon("./77.png");190         btn1.setIcon(im2);191         this.setLayout(null);  //布局--绝对布局192         ltn = new MyListener();  //监听193         this.addWindowListener(ltn);194         act = new BtnAction();    //btn事件195         btn1.addActionListener(act);196         btn2 = new JButton("重置");197         btn2.setBounds(300,230,100,50);198         act2 = new BtnAction2();199         btn2.addActionListener(act2);200         //frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);201         this.setSize(425,325);202         this.setLocation(700,500);203         this.setTitle("QQ登录");204         this.setResizable(false);        205         this.add(btn1);206         this.add(btn2);207         this.add(lab1);208         this.add(lab2);209         this.add(jpf1);210         this.add(jtf1);211         this.add(background1);212         this.add(background2);213         this.setVisible(true);214         System.out.println("OK");215     }216     public static void main(String[] args)217     {218         LoginFrame Event1 = new LoginFrame();219         System.out.println("OK");220     }221 }222 class RegsterFrame extends FixFrame223 {224     225 }226 class BackPswFrame extends FixFrame227 {228     229 }230 class MainFrame extends ResizableFrame231 {232     233 }

 

转载于:https://www.cnblogs.com/YUJIE666/p/6759059.html

你可能感兴趣的文章
文本超过长度后隐藏,显示省略号
查看>>
netstat常见参数
查看>>
wpf Loading动画 AkeemLoading
查看>>
Ubuntu 里面 apt-get 三个有关更新的命令的区别
查看>>
POJ 1019, Number Sequence
查看>>
activiti插件安装-离线安装
查看>>
[译]准备 2017 前端面试
查看>>
RecyclerView的刷新分页
查看>>
MySQL——循环(双重循环)
查看>>
Html5学习笔记---1
查看>>
无聊的时候就看看
查看>>
UItableview section和cell的局部刷新
查看>>
字符串连接的效率问题
查看>>
紫书 例题 11-12 UVa 1515 (最大流最小割)
查看>>
紫书 习题 11-17 UVa 1670 (图论构造)
查看>>
洛谷P1108 低价购买 (最长下降子序列方案数)(int,long long等 范围)
查看>>
大道至简-第五章-心得体会
查看>>
Python编程从入门到实践,个人笔记
查看>>
哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)F - 苦逼的单身狗
查看>>
oracle数据迁移
查看>>