我如何侦听此 Java 应用程序何时调整大小?

发布于 2025-01-05 12:20:18 字数 1891 浏览 1 评论 0原文

我有这个基本的 Java 应用程序,其中 dim_xdim_y 代表窗口及其内部画布的尺寸。当用户更改窗口的大小时,如何使这些值发生变化,以便画布上绘制的内容相应地缩小/扩展?

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

public class MLM extends Canvas {
    static int dim_x = 720;
    static int dim_y = 480;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Canvas canvas = new MLM();
        canvas.setSize(dim_x, dim_y);
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
        // some stuff is drawn here using dim_x and dim_y
    }
}

编辑: 按照 Binyamin 的回答,我尝试添加这个有效的方法,但是有更好的方法吗?比如,不使 canvas 静态,也许?

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

public class MLM extends Canvas {
    static int dim_x = 720;
    static int dim_y = 480;
    static Canvas canvas;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        canvas = new MLM();
        canvas.setSize(dim_x, dim_y);
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);

        frame.addComponentListener(new ComponentListener(){
            public void componentResized(ComponentEvent e) {
                Dimension d = canvas.getSize();
                dim_x = d.width;
                dim_y = d.height;
            }
            public void componentHidden(ComponentEvent e) {}
            public void componentMoved(ComponentEvent e) {}
            public void componentShown(ComponentEvent e) {}
        });
    }

    public void paint(Graphics g) {
        // some stuff is drawn here using dim_x and dim_y
    }
}

I have this basic Java application in witch dim_x and dim_y represent the dimensions of the window and the canvas within it. How can I get these values to change as the user changes the size of the window so that what is drawn on the canvas shrinks/expands accordingly?

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

public class MLM extends Canvas {
    static int dim_x = 720;
    static int dim_y = 480;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Canvas canvas = new MLM();
        canvas.setSize(dim_x, dim_y);
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
        // some stuff is drawn here using dim_x and dim_y
    }
}

EDIT:
following Binyamin's answer I've tried adding this which works, but is there a better way to do it? As in, with not making canvas static, maybe?

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

public class MLM extends Canvas {
    static int dim_x = 720;
    static int dim_y = 480;
    static Canvas canvas;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        canvas = new MLM();
        canvas.setSize(dim_x, dim_y);
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);

        frame.addComponentListener(new ComponentListener(){
            public void componentResized(ComponentEvent e) {
                Dimension d = canvas.getSize();
                dim_x = d.width;
                dim_y = d.height;
            }
            public void componentHidden(ComponentEvent e) {}
            public void componentMoved(ComponentEvent e) {}
            public void componentShown(ComponentEvent e) {}
        });
    }

    public void paint(Graphics g) {
        // some stuff is drawn here using dim_x and dim_y
    }
}

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

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

发布评论

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

评论(3

何以畏孤独 2025-01-12 12:20:18

添加组件侦听器,并实现 componentResized看这里

frame.addComponentListener(new ComponentListener(){
    @Override
    public void componentResized(ComponentEvent e) {
        //Get size of frame and do cool stuff with it   
    }
}

Add a component listener, and implement componentResized. Look here.

frame.addComponentListener(new ComponentListener(){
    @Override
    public void componentResized(ComponentEvent e) {
        //Get size of frame and do cool stuff with it   
    }
}
我偏爱纯白色 2025-01-12 12:20:18
  1. 不要混合使用 AWT 和 AWT。 Swing 组件没有充分的理由(这种使用不是充分的理由)。您可以使用 JComponentJPanel 代替 Canvas
  2. 这里没有用于检测调整大小的用例。如果调整 UI 大小,将调用自定义渲染组件的 paint()paintComponent(),您只需 getWidth() >/getHeight() 来发现渲染区域的大小。
  1. Don't mix AWT & Swing components without good reason (this use is not good reason). Instead of the Canvas you might use a JComponent or JPanel instead.
  2. There is no use-case here for detecting resize. If the UI is resized, the paint() or paintComponent() of the custom rendered component will be called, and you can simply getWidth()/getHeight() to discover the size of the rendering area.
四叶草在未来唯美盛开 2025-01-12 12:20:18

根据我的经验,当 AWT Canvas 嵌套在 JPanel 中时,Canvas 的 Paint() 方法会在窗口展开时调用,但在窗口收缩时不会调用。因此画布可以增长但永远不会缩小。我用 JComponent 的子类化来重构子类化的 Canvas。

In my experience, when an AWT Canvas is nested within a JPanel, the paint() method of the Canvas is called when the window is expanded but not when it is contracted. Thus the Canvas can grow but never shrink back down. I refactored the subclassed Canvas with subclassing from JComponent instead.

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