Crenox

Winston Tutorial 4-5

Mar 24th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /*
  2. Links:
  3. https://stackoverflow.com/questions/9543320/how-to-position-the-form-in-the-center-screen
  4. https://www.youtube.com/watch?v=mjOicuXEvwg
  5. */
  6.  
  7. package com.samkough.main;
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. import javax.swing.*;
  13.  
  14. @SuppressWarnings("serial")
  15. public class FirstGui extends JFrame
  16. {
  17. private JLabel l1;
  18. private JLabel l2;
  19. private JButton b1;
  20. private JButton b2;
  21. private int x = 0, y = 0;
  22.  
  23. public FirstGui()
  24. {
  25. setLayout(new FlowLayout());
  26.  
  27. b1 = new JButton("Button 1");
  28. add(b1);
  29.  
  30. b2 = new JButton("Button 2");
  31. add(b2);
  32.  
  33. l1 = new JLabel("");
  34. add(l1);
  35.  
  36. l2 = new JLabel("");
  37. add(l2);
  38.  
  39. Gui e = new Gui();
  40. b1.addActionListener(e);
  41.  
  42. Gui2 ev = new Gui2();
  43. b2.addActionListener(ev);
  44. }
  45.  
  46. public class Gui implements ActionListener
  47. {
  48. @Override
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. if (x == 0)
  52. {
  53. l1.setText("This is Button 1.");
  54. x = 1;
  55. }
  56. else if (x == 1)
  57. {
  58. l1.setText("");
  59. x = 0;
  60. }
  61. }
  62.  
  63. }
  64.  
  65. public class Gui2 implements ActionListener
  66. {
  67. public void actionPerformed(ActionEvent e)
  68. {
  69. if (y == 0)
  70. {
  71. l2.setText("This is Button 2.");
  72. y = 1;
  73. }
  74. else if (y == 1)
  75. {
  76. l2.setText("");
  77. y = 0;
  78. }
  79. }
  80. }
  81.  
  82. public static void main(String args[])
  83. {
  84. FirstGui frame = new FirstGui();
  85.  
  86. frame.setVisible(true);
  87. frame.setSize(200, 200);
  88. frame.setTitle("First Gui");
  89. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90. frame.setLocationRelativeTo(null);
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment