Advertisement
arnobkumarsaha

JPanel + ActionListener

Mar 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1.  
  2. package swing;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8.  
  9. public class SWING {
  10. private JFrame frame;
  11. private JPanel panel;
  12. private JLabel label;
  13. private JTextField textField;
  14. private JButton button;
  15. private JButton button1;
  16.  
  17. public SWING(){
  18. label = new JLabel("Enter Your Name");
  19. textField = new JTextField(10);
  20. button = new JButton("OK");
  21. button1 = new JButton("Submit");
  22.  
  23. panel = new JPanel();
  24. panel.add(label);
  25. panel.add(textField);
  26. panel.add(button);
  27. panel.add(button1);
  28.  
  29. buttonPressed sPressed =new buttonPressed();
  30.  
  31. button.addActionListener(sPressed);
  32. button1.addActionListener(sPressed);
  33. JFrame frame = new JFrame("App Title");
  34. frame.add(panel);
  35.  
  36. frame.setSize(400, 300);
  37. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. frame.setLocation(150, 150);
  39. frame.setVisible(true);
  40. }
  41.  
  42. public class buttonPressed implements ActionListener{
  43.  
  44. @Override
  45. public void actionPerformed(ActionEvent e) {
  46. String input = textField.getText().toString();
  47. JOptionPane.showMessageDialog(panel, "Hello "+input);
  48. System.out.println("Hello World "+e.getActionCommand());
  49. }
  50.  
  51. }
  52.  
  53. public static void main(String[] args) {
  54. SWING app = new SWING();
  55. }
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement