I have created simple GUI in java to test the working of the click button. But when I press the button, button is not working.
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class rigotechnology implements ActionListener{
JFrame test;
JButton Try;
public void main(){
JFrame test = new JFrame("TEST FILE");
test.setLayout(null);
JButton Try = new JButton("TRY");
Try.setBounds(50,50,80,80);
test.add(Try);
test.setSize(200,200);
test.setVisible(true);
Try.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Try){
JOptionPane.showMessageDialog(test,"hello");
}
}
public static void main(String args[]){
rigotechnology o = new rigotechnology();
o.main();
}
}
In your method main() , you redefine a local JButton Try, so Try in actionPerformed is always null.
public void main(){
test = new JFrame("TEST FILE");
test.setLayout(null);
Try = new JButton("TRY");
Try.setBounds(50,50,80,80);
test.add(Try);
test.setSize(200,200);
test.setVisible(true);
Try.addActionListener(this);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.