Hi there,
Every time i am making a program in Java(Advance). I found that it can many errors in it.
For example I am Giving u a program written by me

//a push button with all features

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class Mybuttons1 extends Frame
JButton b;
//goto content pane
Container c = this.getContentPane();
//set the flow layout to c
c.setLayout(new FlowLayout());
//load the image into ImageIcon class object

ImageIcon obj;
ImageIcon ii = new ImageIcon("India_by_mr_curry.jpg");
//create the button with image on it
b = new JButton("Click Me",ii);
//set colors
//set a font
Font f = new Font("Arial",Font.BOLD,32);
//set bevel border
//Border bd = BorderFactory.createBevelBoreder(BevelBorder.RAISED);
//set a tool tip text
b.setToolTipText("I am Button");
//set a  short cut key
//add button to C
//close the application
this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );

public static void main(String a[])
//create the frame
Mybuttons1 mb = new Mybuttons1();
mb.setTitle("My Push Button");

After compiling it I am getting three errors can....Your reply will be appreciated

Recommended Answers

Okay? Nobody writes error free programs in a first draft (or even 100th, many times), so what's the problem?

Jump to Post

non static method ... cannot be referenced from a static context

click me

Jump to Post

All 7 Replies

Okay? Nobody writes error free programs in a first draft (or even 100th, many times), so what's the problem?

In this program the error is "cannot find symbol in
Container c = this.getContentPane(); method,
same error in

also in
this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );

this is error i am getting and I am unable to rum my program

1-- > Extend your class with JFrame (not with Frame)
2-- > Uncomment //Border bd = BorderFactory.createBevelBoreder(BevelBorder.RAISED);
and replace Border bd = BorderFactory.createBevelBoreder(BevelBorder.RAISED); with Border bd = BorderFactory.createBevelBorder(BevelBorder.RAISED);
then run ur app

Thanks alot ....its working Fine now....there are some other question which i want to ask..there is another program in which i have created there is one error...
non static method get content pane cannot be referenced from a static context
Container content = getContentPane();

this is the source code

import java.awt.*;
import javax.swing.*;

class HelloWorld extends JFrame
public static void main(String args[])
HelloWorld hw = new HelloWorld();

JLabel jl = new JLabel("Hello World");
hw.setSize(100, 100);

hw.setTitle("My Message to the JFrame");
hw.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );

Container content = getContentPane();
    content.setLayout(new FlowLayout());
    JButton button1 = new JButton("Java");
    ImageIcon cup = new ImageIcon("images/cup.gif");
    JButton button2 = new JButton(cup);
    JButton button3 = new JButton("Java", cup);
    JButton button4 = new JButton("Java", cup);


this is the problem i faced earlier also. So can u tell me what is the bug in brief, and what mistake i am doing...

non static method ... cannot be referenced from a static context

click me

So what should be done....in this program to make it run....

Something like this:

class Main {
    public static void main(String args[]) {
        new Main();
    Main() {
        //place JFrame logic here
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.