简体   繁体   English

在Java swing中单击按钮后显示标签

[英]display a label after the Button is clicked in Java swing

I want to display a label, when a button is clicked. 我想在单击按钮时显示标签。 I am using Eclipse Juno. 我正在使用Eclipse Juno。 I have the label added and setting the visible part... 我添加了标签并设置了可见部分...

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

the button 按钮

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

and here is the actionPerformed. 这是actionPerformed。 I already implemented ActionListener 我已经实现了ActionListener

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

    }
}

Initially you can set the visibility to false of your label and after clicking the button set the visibility like label.setVisible(true) 最初,您可以将标签的可见性设置为false,然后单击按钮后,将可见性设置为label.setVisible(true)

for eg like this, note i am using lamba syntax for java 8 对于例如这样,请注意我正在为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);
    }
}

Add ActionListener to your button: 将ActionListener添加到您的按钮:

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

Or better practice (I think) is to create separate class that implements ActionListener. 或者更好的做法(我认为)是创建实现ActionListener的单独的类。 But result will be the same. 但是结果将是相同的。 I don't know how big your application is, but I suggest to separate ActionListeners (like separate class I mentioned), just to make your code clear. 我不知道您的应用程序有多大,但是我建议分离ActionListeners(就像我提到的单独的类),只是为了使您的代码清晰。

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

This piece of code worked for me, Here NewJFrame() is the constructor 这段代码对我NewJFrame() ,这里的NewJFrame()是构造函数

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

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