在 Jframe 中,为什么我的 "getScreenSize()" 只显示为 19201080 而不是实际的 Jframe 边框大小?
In Jframe, why does my "getScreenSize()" only show as 19201080 and not the actual Jframe border size?
我对此很陌生。当我手动更改 Jframe 边框大小时,它仍然更新为“19201080”,我正在尝试获取实际的 Jframe 宽度和高度,以便我能够根据 jframe 屏幕大小更改我的 Circle 组件大小,
圆形组件class
package advancedjava;
import java.awt.*;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
/**
*
* @author eeu8c6
*/
public class CircleComponent extends JComponent{
@Override
public void paintComponent(Graphics g){
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
System.out.println("test: " + width + height);
double swidth = width * 0.8;
double sheight = height * 0.8;
Graphics2D g2 = (Graphics2D) g;
Circle c1 = new Circle(50, 50);
g2.setPaint(Color.RED);
c1.draw(g2);
Ellipse2D ellipse = new Ellipse2D.Double();
g2.fill(ellipse);
}
}
圆Class:
package advancedjava;
import java.awt.*;
import java.awt.geom.*;
/**
*
* @author eeu8c6
*/
public class Circle {
private double xLeft;
private double xRight;
public Circle(double x, double y){
xLeft = x;
xRight = y;
}
public void draw(Graphics2D g2){
Ellipse2D.Double circle = new Ellipse2D.Double(xLeft, xRight, 400, 400);
g2.draw(circle);
}
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
描述为Gets the size of the screen. On systems with multiple displays, the primary display is used. Multi-screen aware display dimensions are available from GraphicsConfiguration and GraphicsDevice.
工具包为您提供实际屏幕尺寸。获取 JFrame
调用 frame.getSize();
的大小
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
这是一种糟糕的方法,它没有考虑其他屏幕元素,例如任务栏和停靠栏,这些元素随后可能会显示在应用程序的顶部
I'm trying to get the actual Jframe width and height so I'm able to have my Circle component change size based on jframe screen size
然后在组件上调用 getWidth
和 getHeight
。该组件代表您可用的可见绘画范围,任何更大的都不会显示(会被裁剪)
public class CircleComponent extends JComponent{
@Override
public void paintComponent(Graphics g){
int width = getWidth().width;
int height = getHeight().height;
//....
请记住,组件 "contained" 在另一个容器内,容器有自己的大小,组件的大小也可能受到布局约束的影响。
框架还由其他元素(框架边框和标题栏)组成,这些元素会影响您的应用 space 可用的可见性
如果你想window显示器的最大可用尺寸,你可以简单地使用Frame#setExtendedState
并传递它JFrame#MAXIMIZED_BOTH
,这将简单地最大化window根据OS
的要求
我对此很陌生。当我手动更改 Jframe 边框大小时,它仍然更新为“19201080”,我正在尝试获取实际的 Jframe 宽度和高度,以便我能够根据 jframe 屏幕大小更改我的 Circle 组件大小,
圆形组件class
package advancedjava;
import java.awt.*;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
/**
*
* @author eeu8c6
*/
public class CircleComponent extends JComponent{
@Override
public void paintComponent(Graphics g){
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
System.out.println("test: " + width + height);
double swidth = width * 0.8;
double sheight = height * 0.8;
Graphics2D g2 = (Graphics2D) g;
Circle c1 = new Circle(50, 50);
g2.setPaint(Color.RED);
c1.draw(g2);
Ellipse2D ellipse = new Ellipse2D.Double();
g2.fill(ellipse);
}
}
圆Class:
package advancedjava;
import java.awt.*;
import java.awt.geom.*;
/**
*
* @author eeu8c6
*/
public class Circle {
private double xLeft;
private double xRight;
public Circle(double x, double y){
xLeft = x;
xRight = y;
}
public void draw(Graphics2D g2){
Ellipse2D.Double circle = new Ellipse2D.Double(xLeft, xRight, 400, 400);
g2.draw(circle);
}
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
描述为Gets the size of the screen. On systems with multiple displays, the primary display is used. Multi-screen aware display dimensions are available from GraphicsConfiguration and GraphicsDevice.
工具包为您提供实际屏幕尺寸。获取 JFrame
调用 frame.getSize();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
这是一种糟糕的方法,它没有考虑其他屏幕元素,例如任务栏和停靠栏,这些元素随后可能会显示在应用程序的顶部
I'm trying to get the actual Jframe width and height so I'm able to have my Circle component change size based on jframe screen size
然后在组件上调用 getWidth
和 getHeight
。该组件代表您可用的可见绘画范围,任何更大的都不会显示(会被裁剪)
public class CircleComponent extends JComponent{
@Override
public void paintComponent(Graphics g){
int width = getWidth().width;
int height = getHeight().height;
//....
请记住,组件 "contained" 在另一个容器内,容器有自己的大小,组件的大小也可能受到布局约束的影响。
框架还由其他元素(框架边框和标题栏)组成,这些元素会影响您的应用 space 可用的可见性
如果你想window显示器的最大可用尺寸,你可以简单地使用Frame#setExtendedState
并传递它JFrame#MAXIMIZED_BOTH
,这将简单地最大化window根据OS