Swing setResizable和setBounds [英] Swing setResizable and setBounds

查看:182
本文介绍了Swing setResizable和setBounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用基于我的swing项目的gui部分,我需要将setBounds JFrame的方法与setResizable调用同步,所以我这样做:

Hello I am working on my swing based gui part of my project and I need to synchronize the setBounds JFrame's method with the setResizable call so I do this:

    setResizable(true);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
    Rectangle rect = new Rectangle(screenSize.width/2 - screenSize.width/4,  screenSize.height/2 - screenSize.height/4, screenSize.width/2, screenSize.height/2);
    setBounds(rect);
    setResizable(false);

但结果是我的框架没有得到给定的边界它只是禁用了调整大小功能。如果我删除最后一个setResizable调用,那么表单将获得正确的边界。实际上我有一个基于框架和许多面板的向导,我更改框架上的面板状态。只有在一个面板中,我想给用户提供调整框架大小的机会,否则它应该是固定大小的。所以在我的主框架中我实现了两个方法,enableWizardMode和enablePlayingGameMode:

but the result is that my frame doesn't get the given bounds it just disable the resize functionality. If I remove the last setResizable call then the form will get the right bounds. Actually I have an wizard which is based on an frame and many panels, I change the panels on the frame like states. Only in one panel I would like to give to user the opportunity to resize the frame otherwise it should be in fixed size. So in my main frame I implemented two methods, enableWizardMode and enablePlayingGameMode :

    @Override
    public void enablePlayingGameMode() {
setResizable(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
        setBounds(new Rectangle(screenSize.width/10, screenSize.height/10, screenSize.width - screenSize.width/5, screenSize.height- screenSize.height/5));
    }

    @Override
    public void enableWizardMode() {
setResizable(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
        setBounds(new Rectangle(screenSize.width/2 - screenSize.width/4, screenSize.height/2 - screenSize.height/4, screenSize.width/2, screenSize.height/2));
setResizable(false);
    }

我尝试使用IComponentListener,但它没有帮助我调整框架大小随机。
我也尝试使用SwingUtilities.invokeLater将resizable标志设置为false,但它也没有帮助。
有人知道这个问题的解决方案吗?我运行Linux和oracle jdk7。
谢谢。

I tried with the IComponentListener but it doesn't help me it resized my frame randomly. Also I tried with SwingUtilities.invokeLater in order to set the resizable flag to false, but it doesn't help too. Does anybody know the solution of this problem?? I running Linux and oracle jdk7. Thanks.

推荐答案

设置preferredSize以及调用 pack()对我来说一直都很好。

Setting preferredSize along with calling pack() has always worked well for me.

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class SizeFoo {

   public static void main(String[] args) {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle rect = new Rectangle(screenSize.width / 2 - screenSize.width
            / 4, screenSize.height / 2 - screenSize.height / 4,
            screenSize.width / 2, screenSize.height / 2);
      System.out.println("rect: " + rect);
      JFrame frame = new JFrame("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setPreferredSize(new Dimension(rect.width, rect.height));
      frame.setResizable(false);
      frame.pack();
      frame.setLocation(rect.x, rect.y);
      //frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      System.out.println("JFrame size:" + frame.getSize());
   }
}

这篇关于Swing setResizable和setBounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆