Advertisement
rohitmehra

Untitled

Dec 17th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. // Handle an event in a Swing program.
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. class EventDemo {
  6. JLabel jlab;
  7. EventDemo() {
  8. // Create a new JFrame container.
  9. JFrame jfrm = new JFrame("An Event Example");
  10. // Specify FlowLayout for the layout manager.
  11. jfrm.setLayout(new FlowLayout());
  12. // Give the frame an initial size.
  13. jfrm.setSize(220, 90);
  14. // Terminate the program when the user closes the application.
  15. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. // Make two buttons.
  17. JButton jbtnAlpha = new JButton("Alpha");
  18. JButton jbtnBeta = new JButton("Beta");
  19. // Add action listener for Alpha.
  20. jbtnAlpha.addActionListener(new ActionListener() {
  21. public void actionPerformed(ActionEvent ae) {
  22. jlab.setText("Alpha was pressed.");
  23. }
  24. });
  25. // Add action listener for Beta.
  26. jbtnBeta.addActionListener(new ActionListener() {
  27. public void actionPerformed(ActionEvent ae) {
  28. jlab.setText("Beta was pressed.");
  29. }
  30. });
  31. // Add the buttons to the content pane.
  32. jfrm.add(jbtnAlpha);
  33. jfrm.add(jbtnBeta);
  34. // Create a text-based label.
  35. jlab = new JLabel("Press a button.");
  36. // Add the label to the content pane.
  37. jfrm.add(jlab);
  38. // Display the frame.
  39. jfrm.setVisible(true);
  40. }
  41. public static void main(String args[]) {
  42. // Create the frame on the event dispatching thread.
  43. SwingUtilities.invokeLater(new Runnable() {
  44. public void run() {
  45. new EventDemo();
  46. }
  47. });
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement