Advertisement
timetraveller1992

ActionListener With Input

Aug 6th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package com.home;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.*;
  8.  
  9.  
  10.  
  11. public class Prasanna {
  12.    
  13.     public void showGUI() {
  14.         JFrame jf = new JFrame(); // Creates new JFrame in Memory
  15.         jf.setTitle("Prasanna"); // Sets title of jf to Prasanna
  16.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets the X mark/Alt+F4 control to exit app
  17.         jf.setSize(200,100); // Gives size to the Frame to fill objects
  18.         JButton jb = new JButton("Press Me"); // Create a button object
  19.         JTextField jtf = new JTextField(20);
  20.         JLabel jl = new JLabel("Do Something",JLabel.CENTER);
  21.         jf.add(jtf,BorderLayout.NORTH);
  22.         jf.add(jl, BorderLayout.CENTER);
  23.         jb.addActionListener(new myAction(jtf,jl));
  24.         jf.add(jb, BorderLayout.SOUTH); // Creates a new Button in that JFrame
  25.         jf.setVisible(true);
  26.     }
  27.    
  28.     public class myAction implements ActionListener
  29.     {
  30.         JLabel jl;
  31.         JTextField jtf;
  32.         public myAction(JTextField jtf, JLabel jl) {
  33.             this.jtf = jtf;
  34.             this.jl = jl;
  35.         }
  36.         public void actionPerformed(ActionEvent prasannaEvent) {
  37.             jl.setText("You typed " + this.jtf.getText());
  38.         }
  39.        
  40.     }
  41.    
  42.     public static void main(String[] args) {
  43.         Prasanna p = new Prasanna();
  44.         p.showGUI();
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement