您的位置:首页 > 其它

简单的事件处理程序

2013-11-18 18:00 344 查看
代码:

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() {

@Override
public 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按钮:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: