获取Jframe面板尺寸Java
很简单,我试图找出Jframe面板尺寸,但是每当我获得尺寸时,它都会为我提供屏幕尺寸,而不是窗口尺寸。我尝试了多种选项,并尝试搜索一段时间,但是我找不到可以与此处提供的程序一起使用的任何真正解决方案。
源代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame{
JButton button;
boolean on = false;
public Window(){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(screenWidth,screenHeight);
this.setLayout(null);
Dimension size = this.getSize();
int frameHeight = size.height;
int frameWidth = size.width;
System.out.printf("Frame Height: %d\nFrame Width: %d\n", frameHeight, frameWidth);
button = new JButton();
if (on){
configButton(button, Color.GREEN, "On");
} else {
configButton(button, Color.RED, "Off");
}
button.setBounds(frameWidth/2, frameHeight/2, 100, 100);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
on = !on;
System.out.printf("On is: %b\n", on);
if (on){
configButton(button, Color.GREEN, "On");
} else {
configButton(button, Color.RED, "Off");
}
button.setLocation(frameWidth/2, frameHeight/2);
System.out.printf("Frame Height: %d\nFrame Width: %d\n", frameHeight, frameWidth);
}
});
this.add(button);
this.setVisible(true);
}
private void configButton(JButton b, Color c, String s){
b.setBackground(c);
b.setOpaque(true);
b.setText(s);
}
}
然后执行文件(如果需要)
public class test{
public static void main(String[] args){
new Window();
}
}
Pretty simple, I'm trying to figure out the jframe panel dimensions but whenever I get the dimensions it provides me with my screen dimensions, not the window dimensions. I have tried a multitude of options and have tried searching for a while yet I haven't been able to find any real solution that would work with the program provided here.
Source code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame{
JButton button;
boolean on = false;
public Window(){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(screenWidth,screenHeight);
this.setLayout(null);
Dimension size = this.getSize();
int frameHeight = size.height;
int frameWidth = size.width;
System.out.printf("Frame Height: %d\nFrame Width: %d\n", frameHeight, frameWidth);
button = new JButton();
if (on){
configButton(button, Color.GREEN, "On");
} else {
configButton(button, Color.RED, "Off");
}
button.setBounds(frameWidth/2, frameHeight/2, 100, 100);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
on = !on;
System.out.printf("On is: %b\n", on);
if (on){
configButton(button, Color.GREEN, "On");
} else {
configButton(button, Color.RED, "Off");
}
button.setLocation(frameWidth/2, frameHeight/2);
System.out.printf("Frame Height: %d\nFrame Width: %d\n", frameHeight, frameWidth);
}
});
this.add(button);
this.setVisible(true);
}
private void configButton(JButton b, Color c, String s){
b.setBackground(c);
b.setOpaque(true);
b.setText(s);
}
}
And then the executing file if that's needed
public class test{
public static void main(String[] args){
new Window();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您总是打印出GUI首次发布的大小。
我希望这能解决您的问题:
you are always printing out the size of the first time your GUI launched.
i hope this would solve your problem: