[英]In Jframe, why does my "getScreenSize()" only show as 19201080 and not the actual Jframe border size?
我對此很陌生。 當我手動更改 Jframe 邊框大小時,它仍會更新為 "19201080" ,我正在嘗試獲取實際的 Jframe 寬度和高度,以便我能夠根據 jframe 屏幕大小更改我的 Circle 組件大小,
CircleComponent 類
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);
}
}
圈類:
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();
這是一個糟糕的方法,它沒有考慮其他屏幕元素,如任務欄和停靠欄,然后可以在應用程序的頂部呈現
我正在嘗試獲取實際的 Jframe 寬度和高度,因此我可以根據 jframe 屏幕大小讓我的 Circle 組件更改大小
然后在組件上調用getWidth
和getHeight
。 該組件代表您可用的可見繪畫范圍,任何更大的都不會顯示(將被剪裁)
public class CircleComponent extends JComponent{
@Override
public void paintComponent(Graphics g){
int width = getWidth().width;
int height = getHeight().height;
//....
請記住,一個組件被“包含”在另一個容器中,該容器有自己的大小,並且組件的大小也可能受布局約束的影響。
框架還由其他元素(框架邊框和標題欄)組成,這些元素會減損您的應用程序可用的可視空間
如果要將窗口大小設置為監視器的最大可用大小,您可以簡單地使用Frame#setExtendedState
並將其傳遞給JFrame#MAXIMIZED_BOTH
,這將根據操作系統的要求簡單地最大化窗口
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.
來描述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.
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();
的大小frame.getSize();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.