本文主要是介绍简单的事件处理程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码:
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按钮:
这篇关于简单的事件处理程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!