Advertisement
Guest User

Untitled

a guest
Nov 14th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package com.example;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.IOException;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12.  
  13. public class Signal
  14. {
  15.  
  16.  
  17. private static boolean label1Visible;
  18. private static boolean label2Visible;
  19.  
  20. private static boolean label1SavedVisible;
  21. private static boolean label2SavedVisible;
  22.  
  23. private static void saveAndResetLabelsState()
  24. {
  25. label1SavedVisible = label1Visible;
  26. label2SavedVisible = label2Visible;
  27. label1Visible = false;
  28. label2Visible = false;
  29. }
  30.  
  31. public static void main(String[] args) throws IOException
  32. {
  33. JFrame frame = new JFrame();
  34.  
  35. JPanel panel = new JPanel(){
  36. @Override
  37. public void paint(Graphics g){
  38. saveAndResetLabelsState();
  39. super.paint(g);
  40. }
  41. };
  42.  
  43.  
  44.  
  45. JLabel label1 = new JLabel("first"){
  46. @Override
  47. public void paint(Graphics g){
  48. label1Visible = true;
  49. super.paint(g);
  50. }
  51. };
  52. JLabel label2 = new JLabel("second"){
  53. @Override
  54. public void paint(Graphics g){
  55. label2Visible = true;
  56. super.paint(g);
  57. }
  58. };
  59. JButton button = new JButton("areVisible?");
  60. button.addActionListener(new ActionListener(){
  61.  
  62. @Override
  63. public void actionPerformed(ActionEvent e)
  64. {
  65. System.out.println("label1: " + label1SavedVisible);
  66. System.out.println("label2: " + label2SavedVisible);
  67. }
  68.  
  69. });
  70.  
  71. button.setLocation(50, 50);
  72. button.setSize(50, 50);
  73.  
  74. label1.setLocation(0, 50);
  75. label2.setLocation(0, 250);
  76.  
  77. label1.setSize(50, 50);
  78. label2.setSize(50, 50);
  79.  
  80. panel.add(label1);
  81. panel.add(label2);
  82. panel.add(button);
  83. panel.setLocation(50, 50);
  84. panel.setLayout(null);
  85.  
  86. frame.setSize(200, 200);
  87. frame.add(panel);
  88. frame.setVisible(true);
  89. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement