我有一个程序可以创建 2 个面板,然后在其中放置一个标签和两个按钮。将标签设置为不可见setVisible(false),然后添加两个按钮并打包框架。当我单击第一个按钮时,显示标签,setVisible(true),第二个按钮再次隐藏它,setVisible(false)。当我单击每个按钮时,它们会移动以填充标签隐藏的空间,然后再次移动以避开显示的标签。我想阻止这种情况发生,即使标签被隐藏,按钮也保持在同一个位置。
这是代码:
public class MainFrame extends JFrame{
public JLabel statusLabel;
public JButton show;
public JButton hide;
public MainFrame(){
super("MagicLabel");
JPanel topPanel = new JPanel(); //Create Top Panel
statusLabel = new JLabel(""); //Init label
statusLabel.setVisible(false); //Hide label at startup
topPanel.setSize(400, 150); //Set the size of the panel, Doesn't work
topPanel.add(statusLabel); //Add label to panel
JPanel middlePanel = new JPanel(); //Create Middle Panel
show= new JButton("Show"); //Create show button
hide= new JButton("Hide"); //Create hide …Run Code Online (Sandbox Code Playgroud)