在 Java Swing 中处理用户驱动的大小调整、位置变化

发布于 2024-09-16 06:59:59 字数 187 浏览 5 评论 0原文

我目前正在构建一个 Java Swing GUI,我想知道如何处理用户(例如鼠标)驱动的大小调整。

我的问题是,当我尝试调整主窗口的大小时,或者当在我的应用程序上打开其他一些窗口时,我的应用程序的窗口会变形 - 它的所有部分都不能很好地响应调整大小或其他窗口的变化。有人知道从哪里开始看这个吗?后续问题、资源也值得赞赏。

干杯。

I'm currently building a Java Swing GUI and I was wondering how user (mouse, say) driven resizing is handled.

My problem is that when I try and resize my main window, or when some other window opens over my application, then my application's window gets distorted - all it's parts don't change well in response to the resizing or the other window. Does anyone have a diagnostic on where to start looking at this? Follow-ups questions, resources are also appreciated.

Cheers.

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

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

发布评论

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

评论(3

小兔几 2024-09-23 06:59:59

虽然 Rob 认为 ComponentListener 会在面板大小调整时为您提供回调,这一点是正确的,但您不必使用它来确保 GUI 看起来正确。相反,您应该确保已正确配置 LayoutManager 以处理调整大小。调整大小以适应标准布局可能是一件非常痛苦的事情;我强烈建议您查看 MigLayout

While Rob is correct that a ComponentListener will give you a callback for when the panel is resized, you should not have to use that in order to make sure your GUI looks correct. Instead you should ensure that you have configured your LayoutManager appropriately to handle being resized. It can be a real pain in the neck to get resizing to work appropriately with the standard Layouts; I would highly recommend you check out MigLayout.

颜漓半夏 2024-09-23 06:59:59

添加一个 ComponentListener 来处理 componentResized() 事件,即

frame.addComponentListener(new ComponentListener() {
    public void componentResized(ComponentEvent e) {       
        // resize other components, refresh, etc 
    }
}

来自 JavaDoc:

当组件的大小、位置或可见性发生变化时,将调用侦听器对象中的相关方法,并将 ComponentEvent 传递给它。
提供组件事件仅用于通知目的; AWT 将在内部自动处理组件移动和调整大小,以便无论程序是否注册 ComponentListener,GUI 布局都能正常工作。

请参阅 http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ ComponentListener.html

Add a ComponentListener to handle the componentResized() event, ie

frame.addComponentListener(new ComponentListener() {
    public void componentResized(ComponentEvent e) {       
        // resize other components, refresh, etc 
    }
}

From JavaDoc:

When the component's size, location, or visibility changes, the relevant method in the listener object is invoked, and the ComponentEvent is passed to it.
Component events are provided for notification purposes ONLY; The AWT will automatically handle component moves and resizes internally so that GUI layout works properly regardless of whether a program registers a ComponentListener or not.

See http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ComponentListener.html

信仰 2024-09-23 06:59:59

我的问题是,当我尝试并
调整我的主窗口的大小,或者当某些
另一个窗口在我的上方打开
应用程序,然后是我的应用程序
窗口变形

当另一个应用程序在您的应用程序之上打开时,您的窗口不应变形。 Swing 足够智能,可以根据需要重新绘制框架。如果它没有正确重新绘制,那么我猜您已经添加了自定义绘制代码并且没有正确执行。我唯一可以建议的是永远不要重写框架的 Paint() 方法。

如需更多帮助,您需要发布说明问题的 SSCCE

My problem is that when I try and
resize my main window, or when some
other window opens over my
application, then my application's
window gets distorted

Your window should NOT get distorted when another application opens over top of your application. Swing is smart enough to repaint the frame as required. If it is not repainting properly then I would guess you have added custom painting code and are not doing it properly. The only think I can suggest is to NEVER override the paint() method of a frame.

For more help you need to post your SSCCE that demonstrates the problem.

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