【问题标题】:display a label after the Button is clicked in Java swing在Java swing中单击按钮后显示标签
【发布时间】:2014-09-19 10:33:12
【问题描述】:

我想在单击按钮时显示标签。我正在使用 Eclipse Juno。我添加了标签并设置了可见部分...

wLabel = new JLabel("YOu and Me");
    wLabel .setVisible(false);
    wLabel .setBounds(80, 35, 100, 25);
    wLabel .setFont(new Font("Meiryo", Font.PLAIN, 9));
    wLabel .setForeground(new Color(255, 102, 21));
    add(wLabel);

按钮

wButton = new JButton("W");
    wButton .setActionCommand("myButton");
    wButton .addActionListener(this);
    wButton .setFont(new Font("Meiryo UI", Font.PLAIN, 11));
    wButton .setBounds(10, 33, 70, 35);
    wButton .setBackground(new Color(102, 51, 20));
    add(wButton);

这里是actionPerformed。我已经实现了 ActionListener

public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getActionCommand().equals("myButton")) {
        wLabel.setVisible(true);

    }
}

【问题讨论】:

  • wButton = new JButton("W"); wButton.setActionCommand("myButton"); wButton.setFont(new Font("Meiryo UI", Font.PLAIN, 11)); wButton.setBounds(10, 32, 80, 30); wButton.setBackground(新颜色(102, 51, 0));添加(w按钮); wButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("myButton")) { wLabel.setVisible(true); } }});
  • 这样操作还是不行
  • 试着把例如System.out.println() 检查方法是否被调用。如果它不起作用,请尝试删除 if(..) 声明
  • 每当出现“我的组件未显示且我不知道为什么”形式的问题时,您必须向我们展示您为 (1) 创建组件 (2 ) 将组件添加到面板,以及 (3) 将面板添加到顶级容器。到目前为止,你还没有这样做。为了尽快获得更好的帮助,请发布 Minimal, Complete, Verifiable Example 来证明问题。很多时候,仅仅创建示例就会暴露问题。

标签: java eclipse swing jbutton jlabel


【解决方案1】:

最初,您可以将标签的可见性设置为 false,然后单击按钮设置可见性,例如 label.setVisible(true)

例如像这样,注意我使用的是 java 8 的 Lamba 语法

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BtnDisabled {

    public static void main(String[] args) {

        JFrame frame = new JFrame("");
        JLabel label = new JLabel("You and Me");
        label.setVisible(false);
        JPanel panel = new JPanel();
        panel.add(label);

        JButton btn = new JButton("W");
    btn.addActionListener(e -> {
        if (!label.isVisible()) {
            label.setVisible(true);
        }
    });
        panel.add(btn);
        frame.add(panel);
        frame.setSize(new Dimension(500, 500));

        frame.setVisible(true);
    }
}

【讨论】:

  • 我不使用 JFrame。我还使用 maven 和 WindowBuilder 编辑器。我正在 WindowBuilder 中进行测试。我对 WBe 很陌生。
  • 替换代码中的行“wButton .addActionListener(this);” with wButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (!wlabel.isVisible()) { wlabel.setVisible(true); } } });
  • I do not use JFrame. 那么,你用的是什么? JLabel 必须放在 JPanel 上,而 JPanel 必须放在顶级容器(例如 JFrame)上。
【解决方案2】:

将 ActionListener 添加到您的按钮:

wButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         //Execute when button is pressed
         wLabel.setVisible(true);
    }
});

或者更好的做法(我认为)是创建实现 ActionListener 的单独类。但结果会是一样的。我不知道您的应用程序有多大,但我建议将 ActionListeners 分开(就像我提到的单独的类),只是为了让您的代码清晰。

【讨论】:

  • 很可能问题不在于按钮,使用此 setVisible。即使在我开始添加标签并编写 setVisible(false) 时,我仍然看到它。我扩展了 JPanel,所以我没有使用单独的 JFrame
  • 请发布更多代码,因为我为测试编写的示例应用程序对我有用。我想肯定是其他地方有问题。
【解决方案3】:
public NewJFrame() {
    initComponents();
    jLabel1.setVisible(false);
}

private void initComponents(){
     jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("jLabel1");

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });}




   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)   {                                         
    jLabel1.setVisible(true);
}  

这段代码对我有用,这里NewJFrame()是构造函数

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多