【问题标题】:How to get maximized size of frame?如何获得最大化的框架尺寸?
【发布时间】:2011-11-02 22:14:58
【问题描述】:

我设置了最大化框架:setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

现在我怎样才能得到这个大小,因为当我调用 getSize()getPreferredSize 时,它会返回 0 0

【问题讨论】:

  • “如何获得……框架的大小?” 你到底为什么需要知道? 1) 使用组件的布局 2) 在JComponentJPanel 中进行自定义绘画,并且仅在绘画时查询大小。顺便说一句 - 为了尽快获得更好的帮助,请说明目标(例如“使组件适合”)而不是策略(例如“获取框架大小”)。
  • 我需要这个来设置这个框架中面板的大小,使其具有这个框架的大小
  • 只需将面板添加到框架中,就会使面板变成内容区域的大小。
  • 但我不想让这个面板有内容大小。我想要它更大,就像这个面板所在的框架一样
  • 虽然第一句话有点意思,但我不明白第二句话。你能换一种说法吗?

标签: java swing jframe


【解决方案1】:

setVisible(true); 执行后,您将正确获得最大化的大小。

public NewJFrame() {                            // Constructor
    initComponents();
    this.setExtendedState( getExtendedState() | JFrame.MAXIMIZED_VERT | Frame.MAXIMIZED_HORIZ);
    // height and width still prints the original values 
    System.out.println(this.getSize().height + " " + this.getSize().width); 
}

....

public static void main(String args[]) {        // main
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            NewJFrame foo = new NewJFrame();
            foo.setVisible(true);
            // after setVisible(true) actual maximized values
            System.out.println(foo.getSize().height + " " + foo.getSize().width);
        }
    });
}

【讨论】:

    【解决方案2】:

    我想要框架中的面板尺寸与框架一样大(大)。

    import java.awt.*;
    import javax.swing.*;
    
    class FullSizePanel {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // this is the panel we want to fill the entire content area
                    // of the  parent frame.
                    JPanel redFullSizePanel = new JPanel(new BorderLayout());
                    // make it true to the first part of the attribute name
                    redFullSizePanel.setBackground(Color.RED);
    
                    JFrame f = new JFrame("Full Size Panel");
                    // 1) The default layout for a content pane is BorderLayout
                    // 2) A component added to a BL with no layout constraint ends
                    //     up in the CENTER
                    // 3) A component in the CENTER of a BL takes the full space
                    //     (that is not used up by components in the EAST, WEST,
                    //     NORTH & SOUTH).
                    f.add( redFullSizePanel );
    
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.pack();
                    f.setSize(400,400);
                    f.setLocationByPlatform(true);
                    f.setVisible(true);
                }
            });
        }
    }
    

    什么布局有用?

    1 列 x 行的网格或框布局

    import java.awt.*;
    import javax.swing.*;
    
    class FullSizePanel {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // grid or box layout with 1 column and x rows
                    LayoutManager lm = new GridLayout(0,1,5,5);
                    // this is the panel we want to fill the entire content area
                    // of the  parent frame.
                    JPanel redFullSizePanel = new JPanel(lm);
                    // make it true to the first part of the attribute name
                    redFullSizePanel.setBackground(Color.RED);
    
                    for (int ii=1; ii<6; ii++) {
                        redFullSizePanel.add(new JLabel("Label " + ii));
                        redFullSizePanel.add(new JButton("Button " + ii));
                    }
    
                    JFrame f = new JFrame("Full Size Panel");
                    // 1) The default layout for a content pane is BorderLayout
                    // 2) A component added to a BL with no layout constraint ends
                    //     up in the CENTER
                    // 3) A component in the CENTER of a BL takes the full space
                    //     (that is not used up by components in the EAST, WEST,
                    //     NORTH & SOUTH.
                    f.add( redFullSizePanel );
    
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.pack();
                    f.setSize(400,400);
                    f.setLocationByPlatform(true);
                    f.setVisible(true);
                }
            });
        }
    }
    

    【讨论】:

    • 但是边框布局在这个框架中没有用
    • 要具体!!特别是当您不确定语言时。在这种情况下,为什么它没有用?什么布局有用?
    • 1 列 x 行的网格或框布局
    • 你指的是redFullSizePanel的布局吗?再次,成为 S-P-E-C-I-F-I-C。它与语言无关,与简单逻辑有关。想象一下,我无法读懂你的想法,也无法坐在你的电脑前。 (根据您的意思猜测查看编辑 - 并注意我不喜欢猜测。)
    • 原来的问题是:“想获得最大化的框架大小?” @AndrewThompson,您没有将帧状态设置为最大化,也没有展示如何获得最大化的帧大小。您的帖子没有帮助,至少对我没有帮助。
    【解决方案3】:

    您可以尝试获得最大的“通用”窗口大小。由于您的应用程序已最大化,它应该会产生相同的结果:

    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Dimension screenDimension = env.getMaximumWindowBounds().getSize();
    

    别忘了你的窗口也有所谓的插图:

    Insets insets = frame.getInsets();
    final int left = insets.left;
    final int right = insets.right;
    final int top = insets.top;
    final int bottom = insets.bottom;
    
    final int width = screenDimension.width - left - right;
    final int height = screenDimension.height - top - bottom;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多