First, have a look at the code:
package javaapplication1;
import javax.swing.*;
import java.awt.event.*;
class Elem implements ActionListener{
void perform(){
JFrame frame = new JFrame();
JButton button;
button = new JButton("Button");
frame.getContentPane().add(button) ;
frame.setSize(30,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent ev){
button.setText("Clicked");
}
}
public class JavaApplication1 {
public static void main(String[] args) {
Elem obj = new Elem();
obj.perform();
}
}
I just finished my first event-based java GUI, but I'm not sure where I went wrong. When there was no event handling, the code functioned perfectly.