简体   繁体   English

用按钮显示/隐藏 JLabel?

[英]Show/Hide JLabel with button?

Currently the code only hides the JLabel.目前该代码仅隐藏 JLabel。 I'm not sure why it's not making it visible when I click the button again.我不确定为什么当我再次单击按钮时它不可见。 Hopefully this is an easy fix希望这是一个简单的修复

        contentPane.add(btnSwap);   
    btnHide.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            lblHello.setVisible(false);
        }
    }); 
    contentPane.add(btnHide);
    btnHide.setBounds(185, 199, 89, 23);
    lblHello.setVisible(true);

}

I'm not sure why it's not making it visible when I click the button again.我不确定为什么当我再次单击按钮时它不可见。

Why should it, since all the ActionListener does (the code that's called on button press) is to continually set the label invisible?为什么要这样做,因为 ActionListener 所做的(按下按钮时调用的代码)是不断地将标签设置为不可见?

A solution is to simply toggle its visibility:一种解决方案是简单地切换其可见性:

lblHello.setVisible(!lblHello.isVisible());

Note that to be safe, it's best to revalidate and repaint the container after making such changes, so:请注意,为了安全起见,最好在进行此类更改后重新验证并重新绘制容器,因此:

btnHide.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        lblHello.setVisible(!lblHello.isVisible());
        revalidate();
        repaint();
    }
}); 

as this will rid the GUI of "dirty" pixels that can occur from adding and especially from removing visible components.因为这将消除 GUI 中的“脏”像素,这些像素可能因添加尤其是删除可见组件而出现。


A word on this:关于这句话:

btnHide.setBounds(185, 199, 89, 23);

This suggests that you're using null layouts with setBounds(...) .这表明您将空布局与setBounds(...) While this often seems to newbie Swing coders the best way to create complex GUI's, it will come back to haunt them later, since this will mean that the GUI will look OK on one platform and one platform only, and that if later you want to enhance or improve the GUI, it can only be done with much difficulty and risk of bugs.虽然这在新手 Swing 程序员看来通常是创建复杂 GUI 的最佳方式,但它以后会再次困扰他们,因为这意味着 GUI 在一个平台和一个平台上看起来都不错,如果以后你想增强或改进 GUI,它只能以很大的难度和错误的风险来完成。 Much better is to learn and use the layout managers.更好的是学习和使用布局管理器。


Another recommendation:另一个建议:

If your desire is to change the appearance of the GUI on button press, then also have a look at the CardLayout (please check the CardLayout Tutorial ) as this can be a way to cleanly and easily swap views如果您希望在按下按钮时更改 GUI 的外观,那么还可以查看 CardLayout(请查看CardLayout 教程),因为这可以是一种干净且轻松地交换视图的方法


And a better recommendation:还有一个更好的建议:

Since a JLabel only shows its text or its icon or both, the best way to make it "invisible" is to remove its text and its icon, as noted by Andrew Thompson below:由于 JLabel 仅显示其文本或其图标或两者,因此使其“不可见”的最佳方法是删除其文本及其图标,如下面的 Andrew Thompson 所述:

// get rid of its text
lblHello.setText("");

// and if needed
lblHello.setIcon(null);

This won't work for text components such as JTextFields and JTextAreas or other components that have more "heft" than a JLabel including pretty much all other user-interaction components.这不适用于诸如 JTextFields 和 JTextAreas 之类的文本组件或其他比 JLabel 具有更多“重量”的组件,包括几乎所有其他用户交互组件。

Try :尝试 :

btnChangeLabelVisibilityButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        lblHello.setVisible(!lblHello.isVisible());
    }
}); 

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

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