Advertisement
rohitmehra

Untitled

Dec 17th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // A simple Swing-based applet
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. /*
  6. This HTML can be used to launch the applet:
  7. <object code="MySwingApplet" width=220 height=90>
  8. </object>
  9. */
  10. public class MySwingApplet extends JApplet {
  11. JButton jbtnAlpha;
  12. JButton jbtnBeta;
  13. JLabel jlab;
  14. // Initialize the applet.
  15. public void init() {
  16. try {
  17. SwingUtilities.invokeAndWait(new Runnable () {
  18. public void run() {
  19. makeGUI(); // initialize the GUI
  20. }
  21. });
  22. } catch(Exception exc) {
  23. System.out.println("Can't create because of "+ exc);
  24. }
  25. }
  26. // This applet does not need to override start(), stop(),
  27. // or destroy().
  28. // Set up and initialize the GUI.
  29. private void makeGUI() {
  30. // Set the applet to use flow layout.
  31. setLayout(new FlowLayout());
  32. // Make two buttons.
  33. jbtnAlpha = new JButton("Alpha");
  34. jbtnBeta = new JButton("Beta");
  35. // Add action listener for Alpha.
  36. jbtnAlpha.addActionListener(new ActionListener() {
  37. public void actionPerformed(ActionEvent le) {
  38. jlab.setText("Alpha was pressed.");
  39. }
  40. });
  41. // Add action listener for Beta.
  42. jbtnBeta.addActionListener(new ActionListener() {
  43. public void actionPerformed(ActionEvent le) {
  44. jlab.setText("Beta was pressed.");
  45. }
  46. });
  47. // Add the buttons to the content pane.
  48. add(jbtnAlpha);
  49. add(jbtnBeta);
  50. // Create a text-based label.
  51. jlab = new JLabel("Press a button.");
  52. // Add the label to the content pane.
  53. add(jlab);
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement