1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.HeadlessException;
import java.awt.FlowLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 17-Jun-2006
* Time: 21:48:59
* To change this template use File | Settings | File Templates.
*/
public class ActionOnButton extends JFrame implements ActionListener {
JTextField myTextField = null;
public ActionOnButton() throws HeadlessException {
myTextField = new JTextField();
myTextField.setPreferredSize(new Dimension(175, 20));
JButton myButton = new JButton("Check...");
myButton.addActionListener(this);
setLayout(new FlowLayout());
add(myTextField);
add(myButton);
}
public void actionPerformed(ActionEvent e) {
String userInput = myTextField.getText();
if (userInput.length() > 0) {
System.out.println("Congrats ...");
} else {
System.out.println("mmmmm try again");
}
}
public static void main(String[] args) {
ActionOnButton a = new ActionOnButton();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.pack();
a.setLocationRelativeTo(null);
a.setVisible(true);
}
} |
Partager