问题描述
我试图让JLabel在单击JButton时出现。我添加了一个动作监听器,并将组件添加到了布局中。当在actionPerformed中单击JButton时,我正在使用label1.setVisible(true)。我仍然无法正常工作。可以看看我的代码吗?
I am trying to get a JLabel to appear when a JButton is clicked. I have added an action listener and added the component to the layout. I am using the label1.setVisible(true) when the JButton is clicked in actionPerformed. I still can't get it work. Can some look at my code?
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");
}
}
}
I have my main method in another class file. The error I get leads me to the label1.setVisible(true);
每个问题我都看到他们说要这样做,但是我想知道是否有
Every question I've seen they say to do this, but I'm wondering if there is something else that needs to be added.
推荐答案
这里有几个问题:
- 您的
label1
通过在构造函数中执行JLabel标签
隐藏。您基本上在构造函数中声明了另一个名为label1
的变量,该变量将其隐藏在类本身中。 - 您的标签在启动时可见-我使用了
label.setVisible(false)
进行测试,但您可能希望以其他方式
- Your
label1
was hidden by doingJLabel label
in the constructor. You basically declared another variable calledlabel1
in your constructor that hid the one in the class itself. - Your label was visible on the startup - I used
label.setVisible(false)
for the test, but you might want otherwise
我还没有创建 Image
,因为我没有图像,所以请取消注释并适当更改。
I also put the creation of Image
aside as I did not have an image, so uncomment that and change as appropriate.
这是一个完整的工作版本:
Here's a complete working version:
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
实例。参见例如如果您对此感兴趣,可以使用类似的示例:
I'd suggest using separate (usually inner-class) ActionListener
instances instead of overriding actionPerformed
. See e.g. this for a similar example if you are interested:
- http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/events/BeeperProject/src/events/Beeper.java
此外,如果您在更大的应用程序中使用它(即不只是试验或用于原型设计),请确保所有Swing代码都在EDT上运行。
Also, if you are using this in a bigger application (i.e. not just experimenting or for prototyping), make sure all Swing code is run on EDT.
您通常使用。
希望这会有所帮助。
这篇关于在动作中单击JButton时将JLabel设置为可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!