2

有以下代码:

double gridWidth=columnNumber*(IMAGE_WIDTH_NORMAL+10)+(columnNumber+2)*10;
double screenWidth=Toolkit.getDefaultToolkit().getScreenSize().getWidth()*0.8;
screenWidth=(gridWidth>=screenWidth) ? screenWidth : gridWidth;

double gridHeight=(rules.size()+2)*(IMAGE_HEIGHT_NORMAL+10)+rules.size()*20+(rules.size()+2)*10;
double screenHeight=Toolkit.getDefaultToolkit().getScreenSize().getHeight()*0.8;
screenHeight=(gridHeight>=screenHeight) ? screenHeight : gridHeight;

setSize((int)screenWidth, (int)screenHeight);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
setLocation(screenSize.width/2-getWidth()/2, screenSize.height/2-getHeight()/2);
setMaximumSize(new Dimension((int)gridWeight, (int)gridHeight));
setVisible(true);

此代码执行以下操作:计算框架的首选宽度和高度,如果尺寸大于我想要的尺寸(宽度和高度的 80%),则应用程序设置另一个尺寸;但我想允许用户单击 jframe 上的“全屏”图标以最大化 jframe 以获得首选大小(setMaximumSize() 方法),但它的代码不起作用!总是一个窗口最大化到我的笔记本电脑的全屏!我该如何解决?

4

2 回答 2

4

我不确定我是否正确理解了您的问题,但我建议使用

java.awt.Window#setBounds(int, int, int, int)

设置 JFrame 的位置和大小。

 int x = screenSize.width/2-getWidth()/2;
 int y = screenSize.height/2-getHeight()/2;
 int width = (int)gridWeight;
 int height = (int)gridHeight);
 setBounds(x, y, width, height);

为了最大化 JFrame,我建议使用

java.awt.Frame#setExtendedState(int)

像这样

setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
于 2012-12-27T19:43:42.067 回答
0

你需要这样做:

_mainDialog.setBounds(矩形);

_mainDialog.setExtendedState(windowsState);

示例

        addWindowStateListener(new WindowStateListener() {

            @Override
            public void windowStateChanged(WindowEvent e) {
                int oldState = e.getOldState();
                int newState = e.getNewState();

                if ((oldState & JFrame.ICONIFIED) == 0 && (newState & JFrame.ICONIFIED) != 0) {
                    setMainDialogRectangle(minRectangle, JFrame.NORMAL);
                }
                else if ((oldState & JFrame.ICONIFIED) != 0 && (newState & JFrame.ICONIFIED) == 0) {
                    setMainDialogRectangle(minRectangle, JFrame.NORMAL);
                }
                else if ((e.getNewState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) {
                    setMainDialogRectangle(maxRectangle, JFrame.MAXIMIZED_BOTH);
                }
                else if ((oldState & JFrame.MAXIMIZED_BOTH) != 0 && (newState & JFrame.MAXIMIZED_BOTH) == 0) {
                    setMainDialogRectangle(medRectangle, JFrame.NORMAL);
                }
            }
        });
于 2018-07-19T07:32:22.813 回答