简体   繁体   English

Java UI:在调整大小时设置摆动组件行为

[英]Java UI: set swing components behavior on resize

I am having a hard time to properly set the way my swing components behave on resize. 我很难正确设置我的挥杆组件在调整大小时的行为方式。

I have two problems with that interface: 该接口存在两个问题: 预期

A: The toggle button at the beginning of each row is here to collapse/expand the text. 答:每行开头的切换按钮用于折叠/展开文本。 All the elements are contained in a JLayeredPane. 所有元素都包含在JLayeredPane中。 On the button click, I edit the pane's height to expand or collapse the content (either 31 or 310). 在按钮上单击,我编辑窗格的高度以展开或折叠内容(31或310)。 Expand works fine an pushes the elements below. Expand效果很好,可将以下元素推入。 On the other hand, collapse does hide the text but leaves all the elements in position. 另一方面, collapse确实会隐藏文本,但会将所有元素保留在适当的位置。 Here is my code: 这是我的代码:

private void expandText(java.awt.event.ActionEvent evt) {
    JToggleButton button = (JToggleButton) evt.getSource();
    Container parent = button.getParent();
    Dimension size = parent.getSize();
    String icon;

    if (button.isSelected()) {
        size.height = 310;
        icon = "/org/cytoscape/ocsana/resources/images/minus.png";
    } else {
        size.height = 31;
        icon = "/org/cytoscape/ocsana/resources/images/plus.png";
    }

    parent.setSize(size);
    try {
        button.setIcon(new ImageIcon(ImageIO.read(getClass().getResource(icon)).getScaledInstance(-1, 15, Image.SCALE_SMOOTH)));
    } catch (IOException ex) {
    }

    backgroundPane.revalidate();
    backgroundPane.repaint();
}

B: The screenshot above is the minimum size of the window. B:上面的截图是窗口的最小尺寸。 When I resize the window horizontally, the inner pane only resize to the value of min + (frame.width - min) / 2 meaning my right scrollbar does not stick to the right side of the frame. 当我水平调整窗口大小时,内部窗格仅将大小调整为min + (frame.width - min) / 2的值,这意味着我的右滚动条不粘在框架的右侧。

See below a demonstration of the both problems: 参见下面两个问题的演示: 结果

How does your expand/collapse code work? 您的展开/折叠代码如何工作? Do you just make component visible/invisible, or do you add remove components from the panel? 您只是使组件可见/不可见,还是从面板中添加删除组件?

On the other hand, collapse does hide the text but leaves all the elements in position. 另一方面,折叠确实会隐藏文本,但会将所有元素保留在适当的位置。

If you add/remove components then the basic code is: 如果添加/删除组件,则基本代码为:

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();

meaning my right scrollbar does not stick to the right side of the frame. 表示我的右侧滚动条不粘在框架的右侧。

It depends on the layout manager you are using. 这取决于您使用的布局管理器。 I would guess the easiest would be to use a GrigBagLayout. 我想最简单的方法是使用GrigBagLayout。 It allows you to "fill" the space available. 它允许您“填充”可用空间。 Read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples. 阅读Swing教程中有关如何使用GridBagLayout的部分, 获取更多信息和示例。

All the elements are contained in a JLayeredPane. 所有元素都包含在JLayeredPane中。

Not sure why you are using a layered pane. 不知道为什么要使用分层窗格。 By default a layered pane doesn't use a layout manager. 默认情况下,分层窗格不使用布局管理器。

Well, you could add a listener to the frame so have an action on event when the frame is being resized. 好吧,您可以在框架中添加一个侦听器,以便在调整框架大小时对事件进行操作。 And then pack() the frame. 然后打包()框架。

public final class TestFrame extends JFrame {

(...)

    this.getRootPane().addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent e) {

             this.pack();
             this.revalidate();

        }
    });

}

It you are using the paint graphic method, you should as well repaint() your frame. 如果您使用的是绘制图形方法,则还应该重新绘制框架。

In that method you can also manyally set the preferred size of the window by computing it based on e.getWidth() 在该方法中,您还可以通过基于e.getWidth()进行计算来设置窗口的首选大小

According to camickr answer and comments, see how I solved it: 根据camickr的回答和评论,看看我是如何解决的:


Point A is due to my free layout used in NetBeans. 点A是由于我在NetBeans中使用了自由布局。 I did not succeed to fix my code so I changed the structure of my elements. 我没有成功修复代码,因此更改了元素的结构。 It is probably not optimal and does not use all the swing concepts right, but it works the way I want. 它可能不是最佳的,并且没有正确使用所有的挥杆概念,但是可以按照我想要的方式工作。

I have a JLayeredPane in the background that uses a GridBagLayout . 我在后台使用GridBagLayoutJLayeredPane This background pane contains one column of JPanel of height 30 and 260, one for the summary line , the other one for the details . 此背景窗格包含一列高度为30和260的JPanel ,一列为摘要行 ,另一列为详细信息

在此处输入图片说明

The expand/collapse function controlled by the JToggleButton works by hiding the below panel belowPanel.setVisibility(false) . JToggleButton控制的展开/折叠功能通过隐藏以下面板下面的面板belowPanel.setVisibility(false) No need for repack or anything, just that. 不需要重新包装或其他任何东西。 Here is how the code looks like without changing the button's icon: 这是不更改按钮图标的代码的样子:

private void inverseVisibility(JToggleButton expand, JPanel target) {
    if (expand.isSelected()) {
        target.setVisible(true);
    } else {
        target.setVisible(false);
    }
}

As I only wanted the elements to resize horizontally, all my panels have Horizontal as Fill value and Northwest as Anchor . 因为我只希望元素水平调整大小,所以我所有面板的“ Horizontal为“ Fill值,“ Northwest为“ Anchor I've set the weightX = 1; weightY = 0 我将weightX = 1; weightY = 0设置为weightX = 1; weightY = 0 weightX = 1; weightY = 0 . weightX = 1; weightY = 0 Finally I added a panel in the bottom with a Southwest anchor and fill both along with both weights to 1 (not sure it changes anything but this way I am certain that it will fill all the blank space at the bottom it the window is resized at a bigger size than its content). 最后,我在底部添加了一个带有Southwest锚的面板,并将两个权重both填充为1 (不确定它是否会改变任何东西,但是通过这种方式,我确定它将填充窗口调整大小时底部的所有空白。大于其内容的大小)。


Point B has been solved by taking my background panel, that fit in my Frame , and putting it into a JScrollPane . 通过将适合我Frame背景面板放入JScrollPane ,解决了B点的问题。 The error I had was due to the Netbeans editor that did not properly stick the scroll pane to the side of the frame, due to incoherences in the sizes defined in both the frame and the scroll pane. 我的错误是由于Netbeans编辑器没有正确地将滚动窗格粘贴到框架的侧面,这是由于框架和滚动窗格中定义的大小不一致。 My advise to you if you are using this tool is to set the fewest values as possible as a lot of values are heavily interconnected by the gui designer. 如果您正在使用此工具,我的建议是设置尽可能少的值,因为gui设计器会大量互连很多值。

Get the full code (95,864 bytes) 获取完整的代码 (95,864字节)

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

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