用按钮显示/隐藏 JLabel? [英] Show/Hide JLabel with button?

查看:59
本文介绍了用按钮显示/隐藏 JLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前代码只隐藏了 JLabel.我不确定为什么当我再次单击按钮时它不可见.希望这是一个简单的修复

Currently the code only hides the 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.

为什么要这样做,因为 ActionListener 所做的(按下按钮时调用的代码)就是不断地将标签设置为不可见?

Why should it, since all the ActionListener does (the code that's called on button press) is to continually set the label invisible?

解决方案是简单地切换其可见性:

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();
    }
}); 

因为这将消除因添加特别是删除可见组件而可能出现的脏"像素的 GUI.

as this will rid the GUI of "dirty" pixels that can occur from adding and especially from removing visible components.

关于这个:

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

这表明您正在使用带有 setBounds(...) 的空布局.虽然这在新手 Swing 程序员看来通常是创建复杂 GUI 的最佳方式,但它以后会再次困扰他们,因为这意味着 GUI 在一个平台和一个平台上看起来都不错,如果以后你想增强或改进 GUI,它只能以很大的难度和错误的风险来完成.更好的是学习和使用布局管理器.

This suggests that you're using null layouts with 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. Much better is to learn and use the layout managers.

另一个建议:

如果您希望在按下按钮时更改 GUI 的外观,还可以查看 CardLayout(请查看 CardLayout 教程),因为这可以是一种干净、轻松地交换视图

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

还有更好的推荐:

由于 JLabel 仅显示其文本或其图标或两者,因此使其不可见"的最佳方法是删除其文本及其图标,如下面 Andrew Thompson 所述:

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:

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

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

这不适用于文本组件,例如 JTextFields 和 JTextAreas,或其他比 JLabel 具有更多重量"的组件,包括几乎所有其他用户交互组件.

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.

这篇关于用按钮显示/隐藏 JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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