隐藏挥杆组件而不重新验证布局? [英] Hide a swing component without revalidating the layout?

查看:112
本文介绍了隐藏挥杆组件而不重新验证布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我设置一个带有一些组件和布局管理器的JFrame,它最初看起来非常好,然后由于某些条件(比如,点击一个按钮),我隐藏其中一个组件 - 布局管理器将所有组件洗牌周围的组件。

If I set up a JFrame with some components and a layout manager, which initially looks perfectly fine, and then later due to some condition (say, clicking a button) I hide one of those components - the layout manager shuffles all the components around again.

参见示例代码 - 最初出现3个按钮。单击隐藏按钮时,隐藏隐藏按钮 - 但两个外部按钮会挤压在一起。单击显示按钮时,它们会再次分开以腾出空间。我怎么能阻止这种情况发生,所以在我调用pack()之后,组件会保留在哪里,无论它们以后是否被隐藏?

See example code - initially 3 buttons appear. When you click the Hide button, the Hide button is hidden - but the two outer buttons then squash together. When you click the show button, they move apart again to make space. How can I stop that from happening, so that after I call pack (), components stay where they are no matter if they later become hidden?

在我的真实代码中我正在使用GridBagLayout,但在下面的示例中使用了FlowLayout,因为它更简单,代码更少,并且显示完全相同的行为。

In my real code I'm doing this with GridBagLayout, but used FlowLayout in the example below because its simpler and less code, and shows exactly the same behaviour.

我只能想到令人讨厌的方式这样做,比如使用.setEnabled(false)而不是.setVisible(false),然后覆盖组件的paintComponent()方法,以便在禁用时不绘制组件。

I can only think of nasty ways of doing this, like using .setEnabled (false) instead of .setVisible (false), and then overriding the component's paintComponent () method to not draw the component when it is disabled.

这似乎是完全相反的问题 - 隐藏按钮Java Swing中的布局 - 抱怨隐藏按钮仍占用空间:)但是那里没有示例代码来显示它以这种方式工作。

It seems the exact opposite problem to here - Hide a button from Layout in Java Swing - where is complaining that hidden buttons do still take up space :) But there's no sample code there to show it working in that way.

非常感谢任何建议:)

示例:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RevalidateWhenSetChildInvisibleExample
{
    private JButton button1;
    private JButton button2;
    private JButton button3;

    public void run ()
    {
        // Set up action
        Action hideButtonAction = new AbstractAction ()
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                button2.setVisible (false);
            }
        };
        hideButtonAction.putValue (Action.NAME, "Hide");

        Action showButtonAction = new AbstractAction ()
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                button2.setVisible (true);
            }
        };
        showButtonAction.putValue (Action.NAME, "Show");

        // Set up buttons
        button1 = new JButton ("Dummy");
        button2 = new JButton (hideButtonAction);
        button3 = new JButton (showButtonAction);

        // Set up content pane
        JPanel contentPane = new JPanel ();
        contentPane.setLayout (new FlowLayout ());
        contentPane.add (button1);
        contentPane.add (button2);
        contentPane.add (button3);

        // Set up frame
        JFrame frame = new JFrame ();
        frame.setContentPane (contentPane);
        frame.pack ();
        frame.setVisible (true);
    }

    public static void main (String args [])
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                new RevalidateWhenSetChildInvisibleExample ().run ();
            }
        });
    }
}


推荐答案

你可以使用 CardLayout ,然后用空的JPanel交换按钮。

You could use a CardLayout and then swap the button with an empty JPanel.

阅读Swing教程中的部分如何使用CardLayout 获取更多信息和示例。

Read the section from the Swing tutorial on How to Use CardLayout for more information and examples.

这篇关于隐藏挥杆组件而不重新验证布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆