Here is my code I have written thus far, along with the errors I’m receiving. I’m sure I’ve done similar code before with no errors. I’m pretty sure I’m missing something stupid but I can’t figure it out or find anything on the web.
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JPanel mainPanel = new JPanel();
JButton editButton = new JButton("Edit");
JPanel.add(editButton);
}
Error:
Syntax error on token(s), misplaced construct(s) - for the underlined '.'
on the last line
Syntax error on token "editButton", VariableDeclaratorId expected after this
token - for the underlined parameter within the brackets on the last line.
You’re trying to use it as if it’s a static method – which panel would you expect to add the edit button to? You need to call it on the
mainPanel:However, you can’t do that in a class declaration – a statement like that has to be in a method or constructor. So you probably want:
Or possibly put all the initialization into the constructor, and also make the variables final and private: