代码:
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按钮: