gpt4 book ai didi

java - 在 actionPerformed 中单击 JButton 时设置 JLabel 可见

转载 作者:搜寻专家 更新时间:2023-11-01 02:52:01 24 4
gpt4 key购买 nike

我试图让 JLabel 在单击 JButton 时出现。我添加了一个 Action 监听器并将组件添加到布局中。当在 actionPerformed 中单击 JButton 时,我正在使用 label1.setVisible(true)。我仍然无法正常工作。有人可以看看我的代码吗?

public class LearnAppMain extends JFrame implements ActionListener {

// Define variables
public JButton button1;
public JLabel label1;
public JTextField field1;

private Image image1;
private String apple = "apple.jpg";

public LearnAppMain() {

ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
JLabel label1 = new JLabel(image1);

button1 = new JButton("A");
button1.addActionListener(this);

field1 = new JTextField(10);

// Create layout
setLayout(new FlowLayout());

// create Container
final Container cn = getContentPane();

cn.add(button1);
cn.add(field1);
cn.add(label1);

// setLayout(new FlowLayout());
setSize(250, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if (e.getSource() == button1) {
label1.setVisible(true);
field1.setText("Apple");
}

}

}

我的主要方法在另一个类文件中。我得到的错误将我引向 label1.setVisible(true);

我看到的每个问题都说要这样做,但我想知道是否还有其他需要添加的内容。

最佳答案

这里有几个问题:

  • 通过在构造函数中执行 JLabel label 隐藏了您的 label1。您基本上在构造函数中声明了另一个名为 label1 的变量,该变量将变量隐藏在类本身中。
  • 您的标签在启动时可见 - 我使用 label.setVisible(false) 进行测试,但您可能需要其他方式

我还把 Image 的创建搁置一旁,因为我没有图像,所以取消注释并进行适当的更改。

这是一个完整的工作版本:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LearnAppMain extends JFrame implements ActionListener {

// Define variables
public JButton button1;
public JLabel label1;
public JTextField field1;

private Image image1;
private String apple = "apple.jpg";

public LearnAppMain() {

//ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
//JLabel label1 = new JLabel(image1);
label1 = new JLabel("hello");
label1.setVisible(false);

button1 = new JButton("A");
button1.addActionListener(this);

field1 = new JTextField(10);

// Create layout
setLayout(new FlowLayout());

// create Container
final Container cn = getContentPane();

cn.add(button1);
cn.add(field1);
cn.add(label1);

// setLayout(new FlowLayout());
setSize(250, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if (e.getSource() == button1) {
label1.setVisible(true);
field1.setText("Apple");
}

}
public static void main(String[] args) {
new LearnAppMain();
}
}

我建议使用单独的(通常是内部类)ActionListener 实例而不是重写 actionPerformed。参见例如如果您有兴趣,这是一个类似的例子:

此外,如果您在更大的应用程序中使用它(即不仅仅是试验或原型(prototype)设计),请确保所有 Swing 代码都在 EDT 上运行。

您通常使用 SwingUtilities.invokeLater为此目的。

希望这对您有所帮助。

关于java - 在 actionPerformed 中单击 JButton 时设置 JLabel 可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9371050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com