简体   繁体   中英

How can I listen for when this Java app is resized?

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
    }
}

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   
    }
}
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM