Problem getting JFrame borders inside a timer in Netbeans

I want the JFrame animation to become half when I click a button in my program. I think the easiest way is to set the current JFrame borders to a timer and reduce the borders by 1 to 1 when the timer starts. But when I declare a new timer in the netbeans IDE, it will look like this.

      Timer t = new Timer(5,new ActionListener() {

        public void actionPerformed(ActionEvent e) {

          //inside this I want to get my Jframe bounds like this
           //    int width = this.getWidth();---------here,"this" means the Jframe

           }

        }
    });

But the problem is that here "this" does not refer to JFrame.And also I can not even create a new object of my JFrame. Because he will give me another window. Can someone help me solve this problem?

+5
source share
2 answers

Try

int width = Foo.this.getWidth();

where are the Foosubclasses JFrame.

+5

, JFrame ,

, , . :

SwingUtilities.windowForComponent( theButton );

.

, , ActionListener , ActionListener.

Edit:

mre (, , ).

, SwingUtilities, , , .

- :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AnimationSSCCE extends JPanel
{
    public AnimationSSCCE()
    {
        JButton button = new JButton("Start Animation");
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton button = (JButton)e.getSource();
                WindowAnimation wa = new WindowAnimation(
                    SwingUtilities.windowForComponent(button) );
            }
        });

        add( button );
    }


    class WindowAnimation implements ActionListener
    {
        private Window window;
        private Timer timer;

        public WindowAnimation(Window window)
        {
            this.window = window;
            timer = new Timer(20, this);
            timer.start();
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            window.setSize(window.getWidth() - 5, window.getHeight() - 5);
//          System.out.println( window.getBounds() );
        }
    }


    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("AnimationSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new AnimationSSCCE() );
        frame.setSize(500, 400);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

, , winow . .

+5

All Articles