在actionPerformed中单击JButton时设置JLabel Visible

我试图在单击JButton时显示JLabel。 我添加了一个动作侦听器并将组件添加到布局中。 当在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"); } } } 

我在另一个类文件中有我的main方法。 我得到的错误引导我到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 。 如果您感兴趣,请参阅以下类似示例:

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

您通常使用SwingUtilities.invokeLater来实现此目的。

希望这可以帮助。

首先,您不要将图像首先添加到JLabel。

只需创建对象并让它像..

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

然后在动作中执行修改public void actionPerformed(ActionEvent e){

 if (e.getSource() == button1) { field1.seticon(image1); field1.revalidate(); } 

它肯定会奏效

 clientDetail.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ d.getContentPane().removeAll(); g = new GridBagLayout(); gc = new GridBagConstraints(); d.setLayout(g); JLabel message= new JLabel(" Message"); addComponent(message,5,1,1,2); JTextArea Message = new JTextArea(); addComponent(Message,5,1,1,2); d.setVisible(true); d.setVisible(true); d.pack(); } private void addComponent(Component component, int i, int i0, int i1, int i2) { gc.gridx=i; gc.gridy=i0; gc.gridheight=i1; gc.gridwidth=i2; g.setConstraints(component, gc); add(component); } }); Recep.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ } });