使JButton不可见但尊重其原始空间

时间:2023-01-17 13:39:07

I am using a GridBagLayout and I want to set some buttons as invisible (i.e. they cannot be seen nor clicked), but when I do so using wdButton.setVisible(false) the rest of the buttons move. I would like to leave the space originally occupied by the invisibilized button blank and keep the rest unaltered (similar problem to the one presented here). The solutions proposed there are not useful to me, since I do not want to change the layout.

我使用的是GridBagLayout的,我想设置一些为不可见(即它们不能被看到或点击)按钮,但是当我这样做使用wdButton.setVisible(假)的按钮移动的其余部分。我想留下最初被无限制按钮占据的空间并保持其余部分不变(与此处提供的问题类似)。那里提出的解决方案对我没用,因为我不想改变布局。

For the moment, I have been able to do so by using the following lines of code:

目前,我已经能够通过使用以下代码行来实现:

wdButton.setText("");
wdButton.setOpaque(false);
wdButton.setContentAreaFilled(false);
wdButton.setBorderPainted(false);
wdButton.setEnabled(false);

Is there a shorter way of achieving this?

有没有更短的方法来实现这一目标?

1 个解决方案

#1


4  

Is there a shorter way of achieving this?

有没有更短的方法来实现这一目标?

Create a method and pass in the button as a parameter.

创建方法并将按钮作为参数传递。

The solutions proposed there are not useful to me, since I do not want to change the layout.

那里提出的解决方案对我没用,因为我不想改变布局。

The solution did not involve changing the layout manager for then entire panel. It involved adding a panel using a CardLayout containing your button and an empty panel to your main panel.

该解决方案不涉及更改整个面板的布局管理器。它涉及使用包含按钮和空面板的CardLayout添加面板到主面板。

I want to set some buttons as invisible

我想将一些按钮设置为不可见

You could replace the button with another component:

您可以将该按钮替换为另一个组件:

GridBagConstraints gbc = layout.getConstraints( button );
panel.remove( button );
panel.add( new JLabel(" ");
panel.revalidate();

So as you can see whatever you do it will require multiple lines of code, so pick the easiest solution and just move the code to a method so that you only need to use a single statement for each component you want to hide.

因此,当您可以看到任何操作时,它将需要多行代码,因此选择最简单的解决方案并将代码移动到方法,这样您只需要为要隐藏的每个组件使用单个语句。

#1


4  

Is there a shorter way of achieving this?

有没有更短的方法来实现这一目标?

Create a method and pass in the button as a parameter.

创建方法并将按钮作为参数传递。

The solutions proposed there are not useful to me, since I do not want to change the layout.

那里提出的解决方案对我没用,因为我不想改变布局。

The solution did not involve changing the layout manager for then entire panel. It involved adding a panel using a CardLayout containing your button and an empty panel to your main panel.

该解决方案不涉及更改整个面板的布局管理器。它涉及使用包含按钮和空面板的CardLayout添加面板到主面板。

I want to set some buttons as invisible

我想将一些按钮设置为不可见

You could replace the button with another component:

您可以将该按钮替换为另一个组件:

GridBagConstraints gbc = layout.getConstraints( button );
panel.remove( button );
panel.add( new JLabel(" ");
panel.revalidate();

So as you can see whatever you do it will require multiple lines of code, so pick the easiest solution and just move the code to a method so that you only need to use a single statement for each component you want to hide.

因此,当您可以看到任何操作时,它将需要多行代码,因此选择最简单的解决方案并将代码移动到方法,这样您只需要为要隐藏的每个组件使用单个语句。