Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class TextFieldAndButtonTutorial extends JFrame implements
- ActionListener {
- /*
- * Creating instances of text fields, buttons and a panel that we will use
- * in this tutorial
- */
- JTextField txtField;
- JButton btn1, btn2, btn3;
- JPanel pnl;
- /*
- * In this constructor we will initialize all the UI components and them to
- * the Panel. One new thing that we will be doing is the Event Handling in
- * this tutorial. Events are basically actions that we want the program to
- * perform when we click on a button. For this tutorial we will cover just
- * one type of event listener which is used for clicking events. Its is
- * called "ActionListener".
- */
- public TextFieldAndButtonTutorial() {
- /*
- * This super calls the constructor of JFrame class that we extended and
- * arguments passed in it set that as the title of the window.
- */
- super("A simple TextField and Button tutorial");
- /*
- * Setting the Layout as simple FlowLayout
- */
- setLayout(new FlowLayout());
- /*
- * Initializing all the components
- */
- pnl = new JPanel();
- /*
- * We are initializing the TextField and passing an argument of 15 in
- * the JTextField constructor to set the length of the TextField.
- */
- txtField = new JTextField(15);
- /*
- * Here JButtons are being initialized. The text passed as the argument
- * in the constructor will be the title that we will be see on buttons.
- */
- btn1 = new JButton("Enable Text Field");
- btn2 = new JButton("Disable Text Field");
- btn3 = new JButton("Submit");
- /*
- * adding all the components in the JPanel object and adding the JPanel
- * object in the Frame
- */
- pnl.add(txtField);
- pnl.add(btn1);
- pnl.add(btn2);
- pnl.add(btn3);
- add(pnl);
- /*
- * Now this one is new. What this code do is it registers the button
- * components to the click events. But what we want to perform on these
- * events will be done in a seperate method of ActionListener interface
- * that we implemented in this class.
- */
- btn1.addActionListener(this);
- btn2.addActionListener(this);
- btn3.addActionListener(this);
- /*
- * In the above methods "this" is passed as an argument because the
- * method of the ActionListener interface is in the same class. There
- * are several methods to perform this functionality using multiple
- * classes. Those will be discussed in the next 2-3 tutorials before
- * discussing other event listeners.
- */
- }
- /*
- * This is the method of the ActionListener interface. It will be called
- * when we click on a button that is registered to the ActionListener
- * events. i.e. btn1.addActionListener(this);
- */
- @Override
- public void actionPerformed(ActionEvent e) {
- /*
- * What we are doing here is we are recogonizing the button clicked by
- * the ActionEvent class's method getSource(). What this getSource()
- * method do is recogonize which button was clicked and then we can
- * perform the desired operation by recogonizing the compoment object
- */
- if (e.getSource() == btn1) {
- txtField.setEnabled(true);
- } else if (e.getSource() == btn2) {
- txtField.setEnabled(false);
- } else if (e.getSource() == btn3) {
- String message = txtField.getText();
- if (message.equals("")) {
- JOptionPane.showMessageDialog(null, "Please enter something. You have left the field empty");
- } else {
- /*
- * This is a way to show a message dialog box.
- * More of it will be discussed in a later tutorial
- */
- JOptionPane.showMessageDialog(null, "You entered: " + message);
- }
- }
- }
- public static void main(String[] args) {
- TextFieldAndButtonTutorial obj = new TextFieldAndButtonTutorial();
- obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- obj.setSize(700, 200);
- obj.setResizable(false);
- obj.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement