javabean自定义组件
生活随笔
收集整理的這篇文章主要介紹了
javabean自定义组件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JavaBean自定組件
package com.wt010.jframe;import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.EventListener; import java.util.EventObject;import javax.swing.*; /*** @author konecms* @date 2018年1月20日* @version 1.0*/ class LoginEvent extends EventObject{public String sUserName,sUserPass;public LoginEvent(Object obj,String sUserName,String sUserPass) {super(obj);// TODO Auto-generated constructor stubthis.sUserName=sUserName;this.sUserPass=sUserPass;}} interface LoginListener extends EventListener{public void validateLogin(LoginEvent mec); }class LoginBean extends JPanel implements ActionListener{JLabel lblUser=new JLabel("用戶(hù)名:");JLabel lblPass=new JLabel("密碼:");JTextField txtUser=new JTextField(15);JPasswordField txtPass=new JPasswordField(15);JButton buttonLogin =new JButton("登錄");JButton buttonCancel=new JButton("取消");public LoginBean(){setLayout(new FlowLayout());add(lblUser);add(txtUser);add(lblPass);add(txtPass);add(buttonLogin);buttonLogin.addActionListener(this);add(buttonCancel);buttonCancel.addActionListener(this);}LoginListener mel;public void addLoginListener(LoginListener mel){this.mel=mel;}LoginEvent mec;public void fireLoginEvent(LoginEvent mec){if(mel!=null){mel.validateLogin(mec);}else{System.out.println("Client not init");}}@Overridepublic void actionPerformed(ActionEvent ae) {// TODO Auto-generated method stubif(ae.getSource()==buttonLogin){String temp1=txtUser.getText();String temp2=new String(txtPass.getPassword());mec=new LoginEvent(buttonLogin,temp1,temp2);fireLoginEvent(mec);return;}if(ae.getSource()==buttonCancel){System.exit(0);}}} class JavaBean extends JFrame implements LoginListener{LoginBean meb;public JavaBean(){super("javabean");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);meb=new LoginBean();this.getContentPane().add(meb);meb.addLoginListener(this);}@Overridepublic void validateLogin(LoginEvent mec) {// TODO Auto-generated method stubString s1=mec.sUserName;String s2=mec.sUserPass;JOptionPane.showMessageDialog(this, "用戶(hù)名:"+s1+"\t密碼是:"+s2);}public static void main(String[] args) {JavaBean mec=new JavaBean();mec.setSize(200,200);mec.setVisible(true);}}?
轉(zhuǎn)載于:https://www.cnblogs.com/phpdo100/p/8320808.html
總結(jié)
以上是生活随笔為你收集整理的javabean自定义组件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 让异常名称显示出来
- 下一篇: Redis数据库的管理