I made a simple java application for fun which I was planning to add on to and make alot better. I ran into an error with an action listener problem. The main point of the program was for me to click the button and it would show a label. I tried and tried but it never worked. So I decided to test whenever I clicked the button if it could output to the console window instead of displaying something on the gui. Of course it worked and now Im really puzzled on how and why one part is working and one is not.
Heres my code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Frame extends JFrame{ static JLabel label; static JButton button; static JFrame frame; public static void main(String[] args){ frame = new JFrame("Hello World!"); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 400); frame.setLayout(new FlowLayout()); button = new JButton("Click Me!"); frame.add(button); label = new JLabel("THIS BETTER WORK!"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { frame.add(label); System.out.println("BUTTON IS PRESSED!"); } }); } }