Hello,
I created a program from a book I am reading on java. The program is using swing to make a frame that shows buttons. It compiled without any error, but I ran into an error while trying to run the program.
here is the 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("ButtonFrame");
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();
}
}
Here is the error when I try to run it:
run:
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:19)
at ButtonFrame.main(ButtonFrame.java:25)
Exception in thread "main"
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Thanks in advance