我如何侦听此 Java 应用程序何时调整大小?
我有这个基本的 Java 应用程序,其中 dim_x
和 dim_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加组件侦听器,并实现
componentResized
。 看这里。Add a component listener, and implement
componentResized
. Look here.JComponent
或JPanel
代替Canvas
。paint()
或paintComponent()
,您只需getWidth()
>/getHeight()
来发现渲染区域的大小。Canvas
you might use aJComponent
orJPanel
instead.paint()
orpaintComponent()
of the custom rendered component will be called, and you can simplygetWidth()
/getHeight()
to discover the size of the rendering area.根据我的经验,当 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.