I cannot figure out how to add Actionlisteners
to the JButton
s, any help would be much appreciated.
public class Translator extends
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//To-Do
//Button clicked
});
Hope this helps, it's relatively simple! You just need to add an ActionListener to your desired JButton
To give you a broader idea of how to implement it in a case-scenario where I would like to run a new GUI frame after pressing a button:
startNewFrame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Starting new frame");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
NewFrame newFrame = new NewFrame();
newFrame.setVisible(true);
dispose();//Disposes of current frame
}
});
}
});