Reputation: 155
I'm very new to Java and being the GUI obsessed freak I am, got drawn to Swing. I have not much experience with Java (really basic knowledge) and I've started making a game. I want to use Swing for it and have started with some basic code for the login/register system under the guidance of a website.
However, they don't tell me how to make a button run a command, so, I scoured the internet and found ActionListener but I'm not sure how to use it. I've tried implementing it in my code but I get an error for it. I'm don't know what is wrong, I can't find anything useful on how to fix this error:
Apiary is not abstract and does not override abstract method
actionPerformed(java.awt.event.ActionListener) in
java.awt.event.ActionListener
Here is my code:
import javax.swing.*;
import java.awt.event.ActionListener;
public class Apiary implements ActionListener {
public static void main(String[]args) {
JFrame frame = new JFrame("Apiary");
frame.setSize(350, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
panel.addActionListener(placeComponents);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel usernameLabel = new JLabel("Username");
usernameLabel.setBounds(10,20,80,25);
panel.add(usernameLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
}
}
Upvotes: 0
Views: 1325
Reputation: 168795
How can I fix the Java error: myClass is not abstract and does not override abstract method?
Add an actionPerformed(ActionEvent)
method. Be sure to add the @Override
notation. See other changes as noted in this working example.
import javax.swing.*;
import java.awt.event.*;
public class Apiary implements ActionListener {
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel usernameLabel = new JLabel("Username");
usernameLabel.setBounds(10,20,80,25);
panel.add(usernameLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
Apiary apiary = new Apiary();
loginButton.addActionListener(apiary);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ToDo!");
}
public static void main(String[]args) {
JFrame frame = new JFrame("Apiary");
frame.setSize(350, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
}
JDialog
or a JOptionPane
instead of a JFrame
to display it. See The Use of Multiple JFrames, Good/Bad Practice?Upvotes: 2