Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class Signal
- {
- private static boolean label1Visible;
- private static boolean label2Visible;
- private static boolean label1SavedVisible;
- private static boolean label2SavedVisible;
- private static void saveAndResetLabelsState()
- {
- label1SavedVisible = label1Visible;
- label2SavedVisible = label2Visible;
- label1Visible = false;
- label2Visible = false;
- }
- public static void main(String[] args) throws IOException
- {
- JFrame frame = new JFrame();
- JPanel panel = new JPanel(){
- @Override
- public void paint(Graphics g){
- saveAndResetLabelsState();
- super.paint(g);
- }
- };
- JLabel label1 = new JLabel("first"){
- @Override
- public void paint(Graphics g){
- label1Visible = true;
- super.paint(g);
- }
- };
- JLabel label2 = new JLabel("second"){
- @Override
- public void paint(Graphics g){
- label2Visible = true;
- super.paint(g);
- }
- };
- JButton button = new JButton("areVisible?");
- button.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e)
- {
- System.out.println("label1: " + label1SavedVisible);
- System.out.println("label2: " + label2SavedVisible);
- }
- });
- button.setLocation(50, 50);
- button.setSize(50, 50);
- label1.setLocation(0, 50);
- label2.setLocation(0, 250);
- label1.setSize(50, 50);
- label2.setSize(50, 50);
- panel.add(label1);
- panel.add(label2);
- panel.add(button);
- panel.setLocation(50, 50);
- panel.setLayout(null);
- frame.setSize(200, 200);
- frame.add(panel);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement