Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

non-static method cannot be referenced from a static context

807598Oct 4 2006 — edited Oct 4 2006
Hi all,

I'm relatively new to Java and I'm trying to write a program to generate a random integer when the user clicks a button. The code is as follows:

public class RandomNameGen implements ActionListener {

JTextArea textArea = new JTextArea();
Random generator = new Random();
String newLine = new String("\n");

public JPanel makeContentPane(){
JPanel thePane = new JPanel();
thePane.setLayout(new BorderLayout());
JButton button = new JButton("Generate");
button.setMnemonic('g');
button.addActionListener(this);
thePane.add(button, BorderLayout.SOUTH);
thePane.add(textArea, BorderLayout.CENTER);
return thePane;
}

public void actionPerformed(ActionListener event){
int numCharName = generator.nextInt(14);
String str = Integer.toString(numCharName);
textArea.append(str + newLine);
}

public static void main(String[]args){
JFrame frame = new JFrame("Random Number Generator");
JPanel contentPane = makeContentPane();
frame.setVisible(true);
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(300,300);
}
}

When I try to compile it, I get "non static method makeContentPane() cannot be referenced from a static context."

I tried making the "makeContentPane()" method static but the compiler complained about that too because there are non static elements in it.

If anyone can help me get this working it would be greatly appreciated.

Thanks,

Erik
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 1 2006
Added on Oct 4 2006
5 comments
806 views