JFrame 仅可调整高度

发布于 2024-09-18 07:46:04 字数 520 浏览 5 评论 0原文

JFrame.setResizable(true) 允许用户调整窗口的宽度和高度。是否存在允许用户仅调整高度大小的方法?

谢谢。

编辑:以下解决方案似乎不起作用。在 360x600 JFrame 上,

setResizable(true);
pack();
setMaximizedBounds(new java.awt.Rectangle(0, 0, 360, 1200));
setMaximumSize(new java.awt.Dimension(360, 1200));
setMinimumSize(new java.awt.Dimension(360, 600));
setPreferredSize(new java.awt.Dimension(360, 600));
setVisible(true);

Still 允许完全拉伸 JFrame 的宽度,并且设置 setResizes(false) 不允许拉伸任何内容。

JFrame.setResizable(true) lets the user resize both the width and height of a window. Does a method exist which allows the user to ONLY resize the height?

Thanks.

Edit: The solutions below do NOT seem to work. On a 360x600 JFrame,

setResizable(true);
pack();
setMaximizedBounds(new java.awt.Rectangle(0, 0, 360, 1200));
setMaximumSize(new java.awt.Dimension(360, 1200));
setMinimumSize(new java.awt.Dimension(360, 600));
setPreferredSize(new java.awt.Dimension(360, 600));
setVisible(true);

Still allows fully stretching the width of the JFrame, and setting setResizable(false) allows nothing to be stretched.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

屋顶上的小猫咪 2024-09-25 07:46:04

下面的代码可以正确完成工作。

addComponentListener(new ComponentAdapter() {

    @Override
    public void componentResized(ComponentEvent e) {
        setSize(new Dimension(preferredWidth, getHeight()));
        super.componentResized(e);
    }

});

The code below does the job right.

addComponentListener(new ComponentAdapter() {

    @Override
    public void componentResized(ComponentEvent e) {
        setSize(new Dimension(preferredWidth, getHeight()));
        super.componentResized(e);
    }

});
小傻瓜 2024-09-25 07:46:04

如果您有 JNI 经验,那么这是一个可能的解决方案。

在 Windows 和 Oracle Hotspot 中,此代码将允许在最小和最大尺寸之间调整窗口大小,而不会闪烁或导致任何令人讨厌的 JFrame 调整大小后副作用。如果在创建 JFrame 并使其可见后调用此代码,则可以完全取消上面在 componentResized() 中用于捕获调整大小事件的代码,因为 Windows 具有防止用户能够调整上方或下方大小的功能指定的最小/最大尺寸。

// Java 代码:

static {
    if (System.getProperty("sun.arch.data.model").equals("32"))
    {   // 32-bit JVM
        System.loadLibrary("my32bitdll");
        System.out.println("Running 32-bit JVM");

    } else {
        // 64-bit JVM
        System.loadLibrary("my64bitdll");
        System.out.println("Running 64-bit JVM");
    }
}
// Sets a window to never be resized above or below these minimum widths/heights
public static native int setMinMaxResizeBoundaries(int hwnd, int minWidth, int minHeight, int maxWidth, int maxHeight);

// C++ 代码(包括标准 windows.h、winbase.h 等)

// Global variables defined in DllMain.cpp
// Used for setMinMaxResizeBoundaries()
struct SHwndMinMax
{
    HWND    hwnd;
    int     minWidth;
    int     minHeight;
    int     maxWidth;
    int     maxHeight;
    WNDPROC prefWndProc;
};
SHwndMinMax gsHwndMinMax[2048];
int gsHwndMinMaxCount = 0;
LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


// Code added somwhere:
// setMinMaxResizeBoundaries()
// Sets the resize boundary window sizes, so the window will not be resized above/below that size
JNIEXPORT jint JNICALL Java_your_class_here_setMinMaxResizeBoundaries(JNIEnv* env, jclass cls,
    jint hwnd,
    jint minWidth, jint minHeight,
    jint maxWidth, jint maxHeight)
{
    // We create a hook for the window, and intercept the WM_GETMINMAXINFO message occurs, and update the info
    if (IsWindow((HWND)hwnd))
    {   // Let's add it
        if (gsHwndMinMaxCount < 2048)
        {   // We're good
            // Can add code here to check if this option is valid or not--so it can later be "unhooked" by a separate function call
            gsHwndMinMax[gsHwndMinMaxCount].hwnd        = (HWND)hwnd;
            gsHwndMinMax[gsHwndMinMaxCount].minWidth    = minWidth;
            gsHwndMinMax[gsHwndMinMaxCount].minHeight   = minHeight;
            gsHwndMinMax[gsHwndMinMaxCount].maxWidth    = maxWidth;
            gsHwndMinMax[gsHwndMinMaxCount].maxHeight   = maxHeight;
            gsHwndMinMax[gsHwndMinMaxCount].prefWndProc = (WNDPROC)SetWindowLongPtr((HWND)hwnd, GWLP_WNDPROC, (LONG_PTR)&MinMaxWindowProc);
            // Success
            ++gsHwndMinMaxCount;
            return(0);

        } else {
            // Failuire, too many hooks
            return(-2);
        }

    } else {
        // Failure, HWND is not valid
        return(-1);
    }
}

LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int i;
    MINMAXINFO* mmi;

    for (i = 0; i < gsHwndMinMaxCount; i++)
    {
        if (hwnd == gsHwndMinMax[i].hwnd)
        {   // This is our man, see if it's our message
            if (msg == WM_GETMINMAXINFO)
            {   // It is
                // When maximized, window is at upper-left
                mmi = (MINMAXINFO*)lParam;
                mmi->ptMaxSize.x        = gsHwndMinMax[i].maxWidth;
                mmi->ptMaxSize.y        = gsHwndMinMax[i].maxHeight;
                mmi->ptMaxPosition.x    = 0;  // Can add code here to properly position the window centered in the screen, etc.
                mmi->ptMaxPosition.y    = 0;  // Same here
                // Set the minimum and maximum tracking size (when the user is resizing, what's the smallest and biggest window they see)
                mmi->ptMinTrackSize.x   = gsHwndMinMax[i].minWidth;
                mmi->ptMinTrackSize.y   = gsHwndMinMax[i].minHeight;
                mmi->ptMaxTrackSize.x   = gsHwndMinMax[i].maxWidth;
                mmi->ptMaxTrackSize.y   = gsHwndMinMax[i].maxHeight;
                return(DefWindowProc(hwnd, msg, wParam, lParam));

            } else {
                // Nope, pass it on
                return(CallWindowProc(gsHwndMinMax[i].prefWndProc, hwnd, msg, wParam, lParam));
            }
        }
    }
    return(0);
}

// 以下是准确获取 HWND 的代码:

// Java 代码(添加到上面的 Java 代码

// Returns the HWND for the specified component, or -1 if does not exist
public static native int getComponentHWND(Component c);

中) // C++ 代码

// getComponentHWND()
// Called to return the HWND of the component, if it has one.
JNIEXPORT jint JNICALL Java_your_class_here_getComponentHWND(JNIEnv* env, jclass cls, jobject obj)
{
    HWND hWnd = 0;
    typedef jboolean (JNICALL *PJAWT_GETAWT)(JNIEnv*, JAWT*);
    JAWT awt;
    JAWT_DrawingSurface* ds;
    JAWT_DrawingSurfaceInfo* dsi;
    JAWT_Win32DrawingSurfaceInfo* dsi_win;
    jboolean result;
    jint lock;
    HMODULE _hAWT = 0;

    // Load AWT Library
    if (!_hAWT)
        _hAWT = LoadLibrary(L"jawt.dll");   // for Java 1.4+

    if (!_hAWT)
        _hAWT = LoadLibrary(L"awt.dll");    // for Java 1.3

    if (_hAWT)
    {
        PJAWT_GETAWT JAWT_GetAWT = (PJAWT_GETAWT)GetProcAddress(_hAWT, "JAWT_GetAWT");
        if (JAWT_GetAWT)
        {
            awt.version = JAWT_VERSION_1_4;     // Init here with JAWT_VERSION_1_3 or JAWT_VERSION_1_4
            // Get AWT API Interface
            result = JAWT_GetAWT(env, &awt);
            if (result != JNI_FALSE)
            {
                ds = awt.GetDrawingSurface(env, obj);
                if (ds != NULL)
                {
                    lock = ds->Lock(ds);
                    if ((lock & JAWT_LOCK_ERROR) == 0)
                    {
                        dsi = ds->GetDrawingSurfaceInfo(ds);
                        if (dsi)
                        {
                            dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
                            if (dsi_win)
                                hWnd = dsi_win->hwnd;
                            else
                                hWnd = (HWND) -1;   // Failed to obtain the handle (not running on Windows)

                            ds->FreeDrawingSurfaceInfo(dsi);

                        } else {
                            hWnd = (HWND)-2;    // Failed to get the drawing surface info block
                        }
                        ds->Unlock(ds);

                    } else {
                        hWnd = (HWND)-3;    // Failed to lock the drawing surface to obtain information about it
                    }
                    awt.FreeDrawingSurface(ds);

                } else {
                    hWnd = (HWND)-4;    // Failed to get the drawing surface from the compoment
                }
            } else {
                hWnd = (HWND)-5;    // Failed to obtain a proper result from _JAWT_GetAWT()
            }
        } else {
            hWnd = (HWND)-6;    // Failed to find "_JAWT_GetAWT()" function
        }
    } else {
        hWnd = (HWND)-7;    // Failed to load awt.dll
    }
    return (jint)hWnd;
}

If you have experience with JNI, then this is a possible solution.

In Windows and Oracle Hotspot, this code will allow a window to be resized between minimum and maximum dimensions without flickering or causing any of the nasty JFrame-post-resize side-effects. If this code is called once the JFrame is created and made visible, then the code used above in componentResized() to catch resize events can be completely done away with, as Windows has facilities to keep the user from being able to resize above or below the specified min/max sizes.

// Java code:

static {
    if (System.getProperty("sun.arch.data.model").equals("32"))
    {   // 32-bit JVM
        System.loadLibrary("my32bitdll");
        System.out.println("Running 32-bit JVM");

    } else {
        // 64-bit JVM
        System.loadLibrary("my64bitdll");
        System.out.println("Running 64-bit JVM");
    }
}
// Sets a window to never be resized above or below these minimum widths/heights
public static native int setMinMaxResizeBoundaries(int hwnd, int minWidth, int minHeight, int maxWidth, int maxHeight);

// C++ code (include standard windows.h, winbase.h, etc)

// Global variables defined in DllMain.cpp
// Used for setMinMaxResizeBoundaries()
struct SHwndMinMax
{
    HWND    hwnd;
    int     minWidth;
    int     minHeight;
    int     maxWidth;
    int     maxHeight;
    WNDPROC prefWndProc;
};
SHwndMinMax gsHwndMinMax[2048];
int gsHwndMinMaxCount = 0;
LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


// Code added somwhere:
// setMinMaxResizeBoundaries()
// Sets the resize boundary window sizes, so the window will not be resized above/below that size
JNIEXPORT jint JNICALL Java_your_class_here_setMinMaxResizeBoundaries(JNIEnv* env, jclass cls,
    jint hwnd,
    jint minWidth, jint minHeight,
    jint maxWidth, jint maxHeight)
{
    // We create a hook for the window, and intercept the WM_GETMINMAXINFO message occurs, and update the info
    if (IsWindow((HWND)hwnd))
    {   // Let's add it
        if (gsHwndMinMaxCount < 2048)
        {   // We're good
            // Can add code here to check if this option is valid or not--so it can later be "unhooked" by a separate function call
            gsHwndMinMax[gsHwndMinMaxCount].hwnd        = (HWND)hwnd;
            gsHwndMinMax[gsHwndMinMaxCount].minWidth    = minWidth;
            gsHwndMinMax[gsHwndMinMaxCount].minHeight   = minHeight;
            gsHwndMinMax[gsHwndMinMaxCount].maxWidth    = maxWidth;
            gsHwndMinMax[gsHwndMinMaxCount].maxHeight   = maxHeight;
            gsHwndMinMax[gsHwndMinMaxCount].prefWndProc = (WNDPROC)SetWindowLongPtr((HWND)hwnd, GWLP_WNDPROC, (LONG_PTR)&MinMaxWindowProc);
            // Success
            ++gsHwndMinMaxCount;
            return(0);

        } else {
            // Failuire, too many hooks
            return(-2);
        }

    } else {
        // Failure, HWND is not valid
        return(-1);
    }
}

LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int i;
    MINMAXINFO* mmi;

    for (i = 0; i < gsHwndMinMaxCount; i++)
    {
        if (hwnd == gsHwndMinMax[i].hwnd)
        {   // This is our man, see if it's our message
            if (msg == WM_GETMINMAXINFO)
            {   // It is
                // When maximized, window is at upper-left
                mmi = (MINMAXINFO*)lParam;
                mmi->ptMaxSize.x        = gsHwndMinMax[i].maxWidth;
                mmi->ptMaxSize.y        = gsHwndMinMax[i].maxHeight;
                mmi->ptMaxPosition.x    = 0;  // Can add code here to properly position the window centered in the screen, etc.
                mmi->ptMaxPosition.y    = 0;  // Same here
                // Set the minimum and maximum tracking size (when the user is resizing, what's the smallest and biggest window they see)
                mmi->ptMinTrackSize.x   = gsHwndMinMax[i].minWidth;
                mmi->ptMinTrackSize.y   = gsHwndMinMax[i].minHeight;
                mmi->ptMaxTrackSize.x   = gsHwndMinMax[i].maxWidth;
                mmi->ptMaxTrackSize.y   = gsHwndMinMax[i].maxHeight;
                return(DefWindowProc(hwnd, msg, wParam, lParam));

            } else {
                // Nope, pass it on
                return(CallWindowProc(gsHwndMinMax[i].prefWndProc, hwnd, msg, wParam, lParam));
            }
        }
    }
    return(0);
}

// The following is code to get the HWND accurately:

// Java Code (add to Java code above)

// Returns the HWND for the specified component, or -1 if does not exist
public static native int getComponentHWND(Component c);

// Code in C++

// getComponentHWND()
// Called to return the HWND of the component, if it has one.
JNIEXPORT jint JNICALL Java_your_class_here_getComponentHWND(JNIEnv* env, jclass cls, jobject obj)
{
    HWND hWnd = 0;
    typedef jboolean (JNICALL *PJAWT_GETAWT)(JNIEnv*, JAWT*);
    JAWT awt;
    JAWT_DrawingSurface* ds;
    JAWT_DrawingSurfaceInfo* dsi;
    JAWT_Win32DrawingSurfaceInfo* dsi_win;
    jboolean result;
    jint lock;
    HMODULE _hAWT = 0;

    // Load AWT Library
    if (!_hAWT)
        _hAWT = LoadLibrary(L"jawt.dll");   // for Java 1.4+

    if (!_hAWT)
        _hAWT = LoadLibrary(L"awt.dll");    // for Java 1.3

    if (_hAWT)
    {
        PJAWT_GETAWT JAWT_GetAWT = (PJAWT_GETAWT)GetProcAddress(_hAWT, "JAWT_GetAWT");
        if (JAWT_GetAWT)
        {
            awt.version = JAWT_VERSION_1_4;     // Init here with JAWT_VERSION_1_3 or JAWT_VERSION_1_4
            // Get AWT API Interface
            result = JAWT_GetAWT(env, &awt);
            if (result != JNI_FALSE)
            {
                ds = awt.GetDrawingSurface(env, obj);
                if (ds != NULL)
                {
                    lock = ds->Lock(ds);
                    if ((lock & JAWT_LOCK_ERROR) == 0)
                    {
                        dsi = ds->GetDrawingSurfaceInfo(ds);
                        if (dsi)
                        {
                            dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
                            if (dsi_win)
                                hWnd = dsi_win->hwnd;
                            else
                                hWnd = (HWND) -1;   // Failed to obtain the handle (not running on Windows)

                            ds->FreeDrawingSurfaceInfo(dsi);

                        } else {
                            hWnd = (HWND)-2;    // Failed to get the drawing surface info block
                        }
                        ds->Unlock(ds);

                    } else {
                        hWnd = (HWND)-3;    // Failed to lock the drawing surface to obtain information about it
                    }
                    awt.FreeDrawingSurface(ds);

                } else {
                    hWnd = (HWND)-4;    // Failed to get the drawing surface from the compoment
                }
            } else {
                hWnd = (HWND)-5;    // Failed to obtain a proper result from _JAWT_GetAWT()
            }
        } else {
            hWnd = (HWND)-6;    // Failed to find "_JAWT_GetAWT()" function
        }
    } else {
        hWnd = (HWND)-7;    // Failed to load awt.dll
    }
    return (jint)hWnd;
}
断念 2024-09-25 07:46:04

我认为没有一种方法明确用于此目的。但是,您可以预设 JFrame 的首选尺寸、最小尺寸和最大尺寸,以使宽度全部相等。

Dimension dimPreferred = frame.getPreferedSize();
Dimension dimMinimum = frame.getMinimumSize();
Dimension dimMaximum = frame.getMaximumSize();
dimPreferred.setWidth( FIXED_WIDTH );
dimMinimum.setWidth( FIXED_WIDTH );
dimMaximum.setWidth( FIXED_WIDTH );
frame.setPreferredSize( dimPreferred );
frame.setMinimumSize( dimMinimum );
frame.setMaximumSize( dimMaximum );

您可能希望在frame.pack()之后frame.setVisible(true)之前执行此操作。

I do not think there is a method expressly for that purpose. However, you could preset the JFrame's preferred, minimum, and maximum size such that the widths are all equal.

Dimension dimPreferred = frame.getPreferedSize();
Dimension dimMinimum = frame.getMinimumSize();
Dimension dimMaximum = frame.getMaximumSize();
dimPreferred.setWidth( FIXED_WIDTH );
dimMinimum.setWidth( FIXED_WIDTH );
dimMaximum.setWidth( FIXED_WIDTH );
frame.setPreferredSize( dimPreferred );
frame.setMinimumSize( dimMinimum );
frame.setMaximumSize( dimMaximum );

You will probably want to do this after frame.pack() and before frame.setVisible(true).

不美如何 2024-09-25 07:46:04

我相信大多数平台都会尊重 setMaximumSizesetMinimumSize。还有setMaximizedBounds

不起作用的是添加一个侦听器来重置宽度。线程问题使它看起来不愉快。如果您有 PL&F 装饰的窗口(Windows PL&F 不支持),那么它们可能会被黑客攻击。

I believe most platforms will honour setMaximumSize and setMinimumSize. There is also setMaximizedBounds.

What doesn't work is adding a listener to reset the width. Threading issues makes it look unpleasant. If you have PL&F decorated windows (not supported by the Windows PL&F), then they can be hacked about with.

久夏青 2024-09-25 07:46:04

使用 setResizing(false) 而不是 setResizable(true)
事实上 setResizable(false) 为我解决了这个问题。 它甚至禁用了最大化(使图标变暗)

好吧,我应该更好地解释自己。我也想让可调整大小的东西发挥作用。这就是我提出这个问题的原因。但我最终还是同意了,认为这会对OP有所帮助。

我也想做可调整大小的操作,但我想如果我无法调整大小来工作,禁用会更好,因为它不会弄乱您的内容放置。我想我会建议将其作为解决方案。

Instead of setResizable(true) use setResizable(false)
Infact setResizable(false) did the trick for me. It even disabled maximization (dimmed the icon)

Okay, I should have explained myself better. I wanted to get the resizeable thing working too. That's why I landed on this question. But I finally went with this, thought that would help the OP.

I wanted to do resizable too, but thought if I can't get resizable to work, disabling is better because it won't mess up your content placement. Thought I'd suggest that as a solution.

纵山崖 2024-09-25 07:46:04

为了避免闪烁,您可以重写 JFrame 中的 #setBounds 方法。似乎每次调整大小都会调用它。类似的东西

@Override
public void setBounds(int x, int y, int width, int height) {
    if (width == FIXED_WIDTH) {
        super.setBounds(x, y, width, height);
    }
}

对我有用。

To avoid flickering, you can override #setBounds method in your JFrame. Seems like every resize will call it. Something like

@Override
public void setBounds(int x, int y, int width, int height) {
    if (width == FIXED_WIDTH) {
        super.setBounds(x, y, width, height);
    }
}

did the trick for me.

凉月流沐 2024-09-25 07:46:04

我正是出于此目的使用此代码并且它有效(它实际上是编辑的@Hoazhun解决方案,因为他的原始代码将锁定宽度和高度,但在我的版本中,您可以手动设置高度,但具有宽度固定的):

int defWindowW = 300;
int defWindowH = 600;
JFrame MAIN_WINDOW = new JFrame();

MAIN_WINDOW.addComponentListener(new ComponentAdapter() {

    public void componentResized(ComponentEvent e) {

        int W = Integer.parseInt(e.paramString().split(",")[1].split(" ")[1].split("x")[0].replace(")", ""));
        int H = Integer.parseInt(e.paramString().split(",")[1].split(" ")[1].split("x")[1].replace(")", ""));

        /* FOR FIXED WIDTH - ACTIVE NOW, COMMENT IT OUT FOR DISABLING*/ MAIN_WINDOW.setSize(new Dimension(defWindowW, H));
        
        /* FOR FIXED HEIGHT - UNCOMMENT TO MAKE IT ACTIVE */
        //MAIN_WINDOW.setSize(new Dimension(W, defWindowH));

        super.componentResized(e);
    }
});

I am using this code exactly for this purpose and it works (it is actually edited @Hoazhun solution as his original code will lock both width and height, but in my version you can set height manually yet having width fixed):

int defWindowW = 300;
int defWindowH = 600;
JFrame MAIN_WINDOW = new JFrame();

MAIN_WINDOW.addComponentListener(new ComponentAdapter() {

    public void componentResized(ComponentEvent e) {

        int W = Integer.parseInt(e.paramString().split(",")[1].split(" ")[1].split("x")[0].replace(")", ""));
        int H = Integer.parseInt(e.paramString().split(",")[1].split(" ")[1].split("x")[1].replace(")", ""));

        /* FOR FIXED WIDTH - ACTIVE NOW, COMMENT IT OUT FOR DISABLING*/ MAIN_WINDOW.setSize(new Dimension(defWindowW, H));
        
        /* FOR FIXED HEIGHT - UNCOMMENT TO MAKE IT ACTIVE */
        //MAIN_WINDOW.setSize(new Dimension(W, defWindowH));

        super.componentResized(e);
    }
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文