I'm writing code for a fairly simple GUI, but I'm having some trouble getting my buttons to be functional.
Working on Eclipse IDE.
I have my frame, and all items I wish to add to it (i.e. buttons, labels). The buttons each have an action listener. I followed some examples, which I have been able to run sucessfully and have no errors. The examples all are the same as my implementation.
My issue is that when it comes to having my code recognise the input/action, and I use an "if" to check the source (as I have multiple buttons and need to check source), it seems to interpret the button name as a variable, causing an error. Changing the names to be in quotes does nothing. I'm not sure where I'm going wrong, seeing that I have identical code to my examples, but it just doesn't seem to work for me.
Example of code (tried to make it straight to the point):
//Imports:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main extends JFrame implements ActionListener{
//GUI size:
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
//GUI location (approx. monitor centre):
private static final int FRAME_X = 700;
private static final int FRAME_Y = 300;
public static void main(String[] args) {
Main frame = new Main();
frame.setTitle("Play Analyser");
frame.setLocation(FRAME_X, FRAME_Y);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.createGUI();
frame.setVisible(true);
frame.setResizable(false);
}
public void createGUI() {
JButton analyse1 = new JButton("button1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
//Adding elements to GUI
window.add(button1);
analyse1.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == button1) {
System.out.println("HELP!!");
}
if(event.getSource() == "button1") {
System.out.println("Also doesnt work :(( ");
}
}
}
Am I doing something wrong?
Need to repeat myself to fill this mandatory box.
Tried changing the button name to be in quotes, didn't work. Removed error but button is unfunctional still. Tried doing button1.addActionListener(new ActionListener());, just an error since this doesnt seem to be correct syntax for Eclipse (or I don't know, just doesn't work).
I'm writing code for a fairly simple GUI, but I'm having some trouble getting my buttons to be functional.
Working on Eclipse IDE.
I have my frame, and all items I wish to add to it (i.e. buttons, labels). The buttons each have an action listener. I followed some examples, which I have been able to run sucessfully and have no errors. The examples all are the same as my implementation.
My issue is that when it comes to having my code recognise the input/action, and I use an "if" to check the source (as I have multiple buttons and need to check source), it seems to interpret the button name as a variable, causing an error. Changing the names to be in quotes does nothing. I'm not sure where I'm going wrong, seeing that I have identical code to my examples, but it just doesn't seem to work for me.
Example of code (tried to make it straight to the point):
//Imports:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main extends JFrame implements ActionListener{
//GUI size:
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
//GUI location (approx. monitor centre):
private static final int FRAME_X = 700;
private static final int FRAME_Y = 300;
public static void main(String[] args) {
Main frame = new Main();
frame.setTitle("Play Analyser");
frame.setLocation(FRAME_X, FRAME_Y);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.createGUI();
frame.setVisible(true);
frame.setResizable(false);
}
public void createGUI() {
JButton analyse1 = new JButton("button1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
//Adding elements to GUI
window.add(button1);
analyse1.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == button1) {
System.out.println("HELP!!");
}
if(event.getSource() == "button1") {
System.out.println("Also doesnt work :(( ");
}
}
}
Am I doing something wrong?
Need to repeat myself to fill this mandatory box.
Tried changing the button name to be in quotes, didn't work. Removed error but button is unfunctional still. Tried doing button1.addActionListener(new ActionListener());, just an error since this doesnt seem to be correct syntax for Eclipse (or I don't know, just doesn't work).
Share Improve this question asked Mar 24 at 23:45 RRSRRS 12 bronze badges 1- Sorted it, realised I never declared my buttons properly... lol – RRS Commented Mar 26 at 0:18
1 Answer
Reset to default 0I fot to declare my buttons at the top before my main class.
...
import javax.swing.*;
private JButton button1 = new JButton();
public class Main extends JFrame implements ActionListener{
...