Repaint many elements with one button click in GUI app

106 views Asked by At

Hello, I've been working on a GUI application that should resize dynamically and change background color multiple times after clicking a button. My first shot was to make a for loop inside an actionPerformed(ActionEvent e) method but it doesn't work properly. Strangely enough, the app changes properly if the listener is removed and the commented code from main function is used. Why is that?

This code is actually a simple example of a problem which I've came across making a slightly more complex app. Could You kindly add a line or two if the solution varies if my window consists of many JPanels and I decide to repeatedly change properties of some other elements with one button click?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


public class TempWindow {

    public static void main(String [] args ) {
        Frame f = new Frame();

//      for (int i = 0; i < 3; i++) {
//          if(i == 0) {
//          f.getMyPanel().setBackground(Color.red);
//          }
//          if (i == 1) {
//          f.setSize(500, 400);
//          f.getMyPanel().setBackground(Color.yellow);
//          }
//          if( i == 2) {
//              f.setSize(100, 200);
//              f.getMyPanel().setBackground(Color.green);
//          }
//          
//          try {
//              Thread.sleep(500);
//          } catch (InterruptedException e) {
//              e.printStackTrace();
//          }
//          f.repaint();
//      }

    }

}

class Frame extends JFrame {

    MyPanel panel;
    JButton but1;

    public Frame() {

        this.setSize(400, 300);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        but1 = new JButton("Start");
        this.getContentPane().add(but1, BorderLayout.NORTH);

        panel = new MyPanel();
        this.getContentPane().add(panel, BorderLayout.CENTER);
        but1.addActionListener(new DanceActionListener(this));

        this.setVisible(true);
    }

    public MyPanel getMyPanel() {
        return panel;
    }
}

class MyPanel extends JPanel {

    JLabel text;

    public MyPanel() {
        this.setBackground(Color.black);
        this.setLayout(new BorderLayout());
        text  = new JLabel(" xxx ");
        text.setFont(new Font(Font.SERIF, Font.BOLD, 40));
        this.add(text, BorderLayout.CENTER);

        text.setHorizontalAlignment(SwingConstants.CENTER);
        text.setVerticalAlignment(SwingConstants.CENTER);
    }
}

class DanceActionListener implements ActionListener {

    MyPanel panel;
    Frame f;

    public DanceActionListener(Frame f) {
        this.f = f;
        this.panel = f.getMyPanel();
    }

    public void actionPerformed(ActionEvent e ){ 
        for (int i = 0; i < 3; i++) {
            if(i == 0) {
                panel.setBackground(Color.red);
                panel.repaint();
            }
            if (i == 1) {
                panel.setBackground(Color.yellow);
                f.setSize(500, 400);
            }
            if( i == 2) {
                f.setSize(100, 200);
                panel.setBackground(Color.green);
            }

            f.repaint();

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }

}
0

There are 0 answers