简单的事件处理程序

来源:互联网 发布:手机淘宝如何追评 编辑:程序博客网 时间:2024/05/16 14:01

代码:

package pack1;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class Welcome extends JFrame {private JTextField userName = new JTextField();private JLabel jl = new JLabel();private JButton ok = new JButton("OK");public Welcome() {         super("Welcome java");         Container c=this.getContentPane();         c.setLayout(new GridLayout(3,1));         c.add(jl);         c.add(userName);         c.add(ok);         this.setSize(200,200);         this.setVisible(true);         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         ok.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String user=userName.getText();JOptionPane.showMessageDialog(Welcome.this, "欢迎光临:"+user,"提示",+JOptionPane.INFORMATION_MESSAGE);}});    }public static void main(String args[]){new Welcome();}}
运行结果:


输入文字编辑,点击ok按钮:


原创粉丝点击