简体   繁体   English

调整JFrame大小时调整Jpanel大小

[英]Resizing Jpanel when resizing JFrame

I need to draw a xy axis coordinate system on a JPanel. 我需要在JPanel上绘制xy轴坐标系。 I want to implement a function that when resize JFrame, the x,y axis coordinate can resize automatically. 我想实现一个功能,当调整JFrame的大小时,x,y轴坐标可以自动调整大小。

public void paintComponent(Graphics gl) {

 Graphics2D g = (Graphics2D) gl;
 g.setColor(new Color(222, 222, 222));
 g.fillRect(0, 0, this.getWidth(), this.getHeight());
 g.setColor(new Color(0, 0, 0));
 int x=15;
 int y=15;
 g.drawString("20", 0, 10);
 for(int i=1;i<=20;i++) {
     g.drawLine(x, y+(35*(i-1)), x, y+(35*i));
     g.drawString(""+(20-i), 0, y+(35*i));
 }
 for(int i=1;i<=10;i++) {
     g.drawLine(x+(70*(i-1)),715, x+(70*i), 715);
     g.drawString(""+i,  x+(70*i),730);
 }
}

This is how I draw the xy coordinate system. 这就是我绘制xy坐标系的方式。
Could you give me hint for resizing it ? 您能给我提示调整大小吗?

Use an ComponentListener and listen to the resize Events fired up. 使用ComponentListener并监听触发的调整大小事件。 Invalidate your UI to force an repaint which cause your paintComponent will be called. 使您的UI无效以强制重新绘制,这将导致您的paintComponent被调用。

ComponentListener ComponentListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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