获取Jframe面板尺寸Java

发布于 2025-02-02 05:11:28 字数 2137 浏览 3 评论 0原文

很简单,我试图找出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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2025-02-09 05:11:28

您总是打印出GUI首次发布的大小。
我希望这能解决您的问题:

System.out.printf("Frame Height: %d\nFrame Width: %d\n", getContentPane().getHeight(),getContentPane().getWidth());

you are always printing out the size of the first time your GUI launched.
i hope this would solve your problem:

System.out.printf("Frame Height: %d\nFrame Width: %d\n", getContentPane().getHeight(),getContentPane().getWidth());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文