在 Java Swing 中隐藏布局中的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1917365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide a button from Layout in Java Swing
提问by Tigraine
I am trying something very basic: I have a list of 5 buttons. They are in a FlowLayout and the general idea should be that once I click one it should disappear and the others should reorder themselves accordingly.
我正在尝试一些非常基本的东西:我有一个包含 5 个按钮的列表。它们在 FlowLayout 中,一般的想法应该是,一旦我单击一个它就会消失,而其他的应该相应地重新排序。
Now, if I call setVisible(false) the button becomes invisible, but it still occupies it's space in the Layoutmanager.
现在,如果我调用 setVisible(false) 按钮变得不可见,但它仍然占据布局管理器中的空间。
Is there any way to keep the Button in the JPanel while hiding it so it doesn't get picked up by Layout?
有没有办法在隐藏它的同时将 Button 保留在 JPanel 中,这样它就不会被 Layout 拾取?
Update:: Thanks for all the answers, the problem with removing the buttons is that the order is important. The problem I was trying to solve was a find as you type szenario where a very long list of buttons gets filtered down to only the ones matching the characters entered so users can easily click them. Since users can delete characters from the search field ordering is important and buttons have to pop back in once they match again.
更新::感谢所有答案,删除按钮的问题在于顺序很重要。我试图解决的问题是在您键入 szenario 时发现一个很长的按钮列表,其中筛选出与输入的字符匹配的按钮列表,以便用户可以轻松单击它们。由于用户可以从搜索字段中删除字符,因此排序很重要,一旦再次匹配,按钮必须重新弹出。
采纳答案by camickr
Works fine for me.
对我来说很好用。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutInvisible extends JFrame
implements ActionListener
{
JPanel north;
int i;
public FlowLayoutInvisible()
{
north = new JPanel();
for (int i = 0; i < 5; i++)
{
JButton button = new JButton("North - " + i);
button.addActionListener(this);
north.add(button);
}
getContentPane().add(north, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
Component c = (Component)e.getSource();
c.setVisible(false);
((JPanel)c.getParent()).revalidate();
}
public static void main(String[] args)
{
FlowLayoutInvisible frame = new FlowLayoutInvisible();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
If you need more help post your SSCCE.
如果您需要更多帮助,请发布您的SSCCE.
Update: I don't know if the revalidate() is required. I seemed to have a problem once but now I can't duplicate the problem.
更新:我不知道是否需要 revalidate()。我似乎曾经遇到过问题,但现在我无法复制该问题。
回答by Carl Smotricz
You could override each button's getPreferredSize()
methods (and possibly getMinimumSize()
as well to return 0,0 when the component is invisible; and you need to call, I think, invalidate()
(or revalidate
or validate
, I can never keep them straight) on the container.
您可以覆盖每个按钮的getPreferredSize()
方法(也可能getMinimumSize()
在组件不可见时返回 0,0;我认为,您需要在容器上调用invalidate()
(或revalidate
或validate
,我永远无法保持它们直立)。
回答by Michael Borgwardt
I see two possibilities:
我看到两种可能性:
- Write your own layout manager that listens for changes to its children's
visible
property - shouldn't be too hard, you can probably subclassFlowLayout
to do it. - actually remove the clicked-button from the panel and, if necessary, re-add it later.
- 编写您自己的布局管理器来侦听其子项
visible
属性的更改- 应该不会太难,您可以子类化FlowLayout
来执行此操作。 - 实际上从面板中删除单击的按钮,如有必要,稍后重新添加它。
回答by OscarRyz
Just remove it:
只需删除它:
panel.remove( button );
What's wrong with this option?
这个选项有什么问题?
Layout managers are thought precisely to avoid having the "user" to make tricks in order to have each component it the right place ( although it seems to provoke the opposite effect )
布局管理器被认为是为了避免让“用户”制造技巧,以便将每个组件放在正确的位置(尽管它似乎会引起相反的效果)
Removing the button from the panel will have the effect of laying out again all the remaining components. That's why it's name is "Layout manager" it manages to layout the components for you.
从面板上移除按钮将具有重新布置所有剩余组件的效果。这就是为什么它的名字是“布局管理器”,它管理为您布局组件。