I’m having some trouble with the below Java and keep on getting these errors. This is the first time I have ever tried swing in Java and have only recently started using Java, any help/resources are very welcome.
java.lang.Error: Do not use ButtonFrame.add() use ButtonFrame.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:307)
at ButtonFrame.<init>(ButtonFrame.java:16)
at ButtonFrame.main(ButtonFrame.java:21)
Exception in thread "main"
My code -----------------------------------
import javax.swing.*;
public class ButtonFrame extends JFrame {
JButton load = new JButton("Load");
JButton save = new JButton("Save");
JButton unsubscribe = new JButton("Unsubscribe");
public ButtonFrame() {
super("Button Frame");
setSize(80, 170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(load);
pane.add(save);
pane.add(unsubscribe);
add(pane);
setVisible(true);
}
public static void main(String[] arguments) {
ButtonFrame bf = new ButtonFrame();
}
}
Thanks in advance for your time