• java继承系列之添加一个LinkLable类


      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("<html><font color=red><u>" + text);
     38         else
     39             setText("<html><font color=blue><u>" + 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             else
    130             {
    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 FixFrame
    223 {
    224     
    225 }
    226 class BackPswFrame extends FixFrame
    227 {
    228     
    229 }
    230 class MainFrame extends ResizableFrame
    231 {
    232     
    233 }
  • 相关阅读:
    office 2013激活
    MVC interview question
    javaScript true /false
    17 ways to Optimize VBA Code for FASTER Macros
    50 Excel VBA Oral Interview Questions
    How to compare two worksheets in a workoo
    Access窗体传值
    How to Convert a workbook/WorkSheet to PDF
    01_js数据类型
    02_js两个变量交换值
  • 原文地址:https://www.cnblogs.com/YUJIE666/p/6759059.html
Copyright © 2020-2023  润新知