在 Netbeans 中获取计时器内的 JFrame 边界的问题

发布于 2024-11-28 02:38:55 字数 525 浏览 0 评论 0原文

当我按下程序中的按钮时,我想将 JFrame 动画化为一半大小。我认为最简单的方法是将 JFrame 的当前边界放入计时器中,并在计时器运行时将边界 1 减 1。但是当我在 netbeans IDE 中声明一个新计时器时,它会看起来像这样。

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

        public void actionPerformed(ActionEvent e) {

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

           }

        }
    });

但问题是在这里“this”不是指 JFrame。而且我什至无法创建 JFrame 的新对象。因为它会给我另一个窗口。任何人都可以帮我解决这个问题吗?

I want to animate a JFrame to become half-size when i press a button in my programme. I think the easiest way is putting the current bounds of JFrame into a timer and decrease bounds 1 by 1 when the timer running.But when I declare a new timer in netbeans IDE it will looks like this.

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

        public void actionPerformed(ActionEvent e) {

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

           }

        }
    });

But the problem is in here "this" not refering to JFrame.And also I cant even create a new object of my JFrame.Because it will give me another window.Can anyone help me solve this problem ?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想念有你 2024-12-05 02:38:55

尝试

int width = Foo.this.getWidth();

Foo 子类 JFrame

Try

int width = Foo.this.getWidth();

where Foo subclasses JFrame.

小兔几 2024-12-05 02:38:55

当我按下程序中的按钮时,我希望将 JFrame 动画化为一半大小

因此,当您单击该按钮时,您可以访问该按钮。然后您可以使用:

SwingUtilities.windowForComponent( theButton );

来获取对框架的引用。

因此,现在当您为计时器创建 ActionListener 时,您可以将 Window 作为 ActionListener 的参数传递。

编辑:

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();
            }
        });
    }
}

当然,当窗口达到某个最小尺寸时,您会希望停止计时器。我将该代码留给您。

I want to animate a JFrame to become half-size when i press a button in my programme

So when you click the button you have access to the button. Then you can use:

SwingUtilities.windowForComponent( theButton );

to get a reference to the frame.

So now when you create the ActionListener for the Timer you can pass in the Window as an argument for the ActionListener.

Edit:

The suggestion by mre is simple and straight forward and easy to use in many cases (and probably the better solution in this case).

My suggestion is a little more complicated but it was introducing you to the SwingUtilities method which will eventually allow you to write more reusable code that could potentially be used by any frame or dialog you might create.

A simple example would be something like:

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();
            }
        });
    }
}

Of course you would want to stop the timer when the winow reaches a certain minimum size. I'll leave that code up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文