简体   繁体   English

Jave Swing Eclipse-JPanel和组件自动调整大小

[英]Jave Swing Eclipse - JPanel and components auto resize

I am maintaining a system which requires me to make components in the dialog resizable, the dialog box calls out a java class Panel. 我正在维护一个系统,该系统需要使对话框中的组件可调整大小,该对话框调出Java类Panel。

What is supposed to happen: 应该发生什么: 在此处输入图片说明

What is currently happening: 当前正在发生什么: 在此处输入图片说明

Note: The image on the bottom layer represents the resized one. 注意:底层上的图像表示已调整大小的图像。 While the image at the top layer is the dialog box which is not yet resized. 顶层的图像是尚未调整大小的对话框。

As you can see, the component JPanel(the one with the black border) is not resized. 如您所见,组件JPanel(带有黑色边框的组件)未调整大小。 I am trying to achieve what happend to the bottom layer image of the first attachment. 我正在尝试实现第一个附件的底层图像发生了什么。

I tried to apply the answer in How to dynamically control auto-resize components in Java Swing and pattern it in current code but since my panel is only called in a dialog box so there are limitations. 我试图在“ 如何在Java Swing中动态控制自动调整大小的组件并在当前代码中对其进行模式化”中应用答案,但是由于我的面板仅在对话框中被调用,因此存在局限性。 The problem is that the components and its hierarchy have been already implemented, I just have to make it auto-resize. 问题是组件及其层次结构已经实现,我只需要使其自动调整大小即可。

Here is my current outline: 这是我当前的轮廓:

在此处输入图片说明

If you want a simple solution you can use a layout manager as described here . 如果您想要一个简单的解决方案,则可以使用此处所述的布局管理器。 Or, if you wish to avoid a layout manager(like me) then you can have your program resize your elements every time there is a resize event. 或者,如果您希望避免使用布局管理器(例如我),则可以在每次发生调整大小事件时让程序调整元素的大小。 Here is some sudo-code 这是一些sudo代码

frameOrPanel.addComponentListener(new ComponentAdapter() {
   public void componentResized(ComponentEvent componentEvent) {
    element.setLocation(frameOrPanel.getWidth()*1/4, frameOrPanel.getHeight*1/4);
    element.setSize(frameOrPanel.getWidth()*1/2, frameOrPanel.getHeight()*1/2);
   }
});

You will have to add unimplemented methods. 您将不得不添加未实现的方法。

Note: the 1/4 and 1/2 is merely a ratio you can change those to fit your application. 注意:1/4和1/2只是一个比率,您可以更改它们以适合您的应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM