Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java swing中单击按钮后显示标签_Java_Eclipse_Swing_Jbutton_Jlabel - Fatal编程技术网

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

在Java swing中单击按钮后显示标签,java,eclipse,swing,jbutton,jlabel,Java,Eclipse,Swing,Jbutton,Jlabel,我想在单击按钮时显示标签。我正在使用EclipseJuno。我已添加标签并设置可见部分 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

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

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);
下面是执行的操作。我已经实现了ActionListener

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

    }
}

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

例如,像这样,请注意,我使用的是Java8的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);
    }
}

将ActionListener添加到按钮:

wButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         //Execute when button is pressed
         wLabel.setVisible(true);
    }
});
或者更好的做法(我认为)是创建实现ActionListener的单独类。但结果是一样的。我不知道你的应用程序有多大,但我建议分开ActionListener(就像我提到的分开的类),只是为了让你的代码更清晰

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()
是构造函数wButton=newjbutton(“W”);setActionCommand(“myButton”);setFont(新字体(“Meiryo UI”,Font.PLAIN,11));b按钮.立根(10,32,80,30);wButton.setBackground(新颜色(102,51,0));添加(wButton);wButton.addActionListener(新建ActionListener(){public void actionPerformed(ActionEvent e){if(e.getActionCommand().equals(“myButton”){wLabel.setVisible(true);}}});如果我这样做,它仍然不起作用。请尝试放置例如System.out.println()来检查是否调用了该方法。如果它不起作用,请尝试删除
if(…)
语句每当出现“我的组件未显示且我不知道为什么”的问题时,您必须向我们展示您所做的一切(1)创建组件(2)将组件添加到面板,以及(3)将面板添加到顶级容器。到目前为止你还没有这么做。为了更快地获得更好的帮助,请发布一篇演示问题的文章。很多时候,仅仅创建一个示例就可以揭示问题所在。我不使用JFrame。我还使用maven和WindowBuilder编辑器。我正在WindowBuilder中测试。我对WBe很陌生。用wButton.addActionListener(new ActionListener(){@Override public void actionPerformed(ActionEvent e){if(!wlabel.isVisible()){wlabel.setVisible(true);}}替换代码“wButton.addActionListener(this);”中的行<代码>我不使用JFrame。那么,您使用的是什么?JLabel必须放在JPanel上,而JPanel必须放在顶级容器(如JFrame)上。问题很可能不在按钮上,因为此设置可见。即使在开始添加标签并写入setVisible(false)时,我仍然可以看到它。我扩展了JPanel,所以我不使用单独的JFramePlease发布更多的代码,因为我为测试编写的示例应用程序对我很有用。我想一定是别的地方出了问题。