LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2005, 02:53 PM   #1
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Rep: Reputation: 32
Java problem


I am learning Java and I am having a problem with this code
Code:
import java.awt.*;
import javax.swing.*;

public class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
                g.setColor(Color.orange);
                g.fillRect(20,50,100,100);
        }}
It compiles but does not run it gives the error :-
Code:
Exception in thread "main" java.lang.NoSuchMethodError: main
How do I put a main method here ?
 
Old 09-25-2005, 03:18 PM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 52
You need to understand java basics before programming in java to avoid waste of your time.
To be "runnable" a class needs a main method which is static .

You need to add a "panel" in a window("a frame") to display it.

Here is a working code, not tested, but should work without problems.

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

public class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
                g.setColor(Color.orange);
                g.fillRect(20,50,100,100);
        }

// if a main method the code inside the main method will be executed
public static void main(String args[]){

// create a window with a title
JFrame frame = new JFrame("MyApplication");

// if the window is closed, the program should exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add our beautiful panel inside the window
frame.getContentPane.add(new MyDrawPanel());

// prepare the gui and display it
frame.pack();
frame.setLocation(200,200);
frame.setSize(400,400);
frame.setVisible(true);
}
}
 
Old 09-26-2005, 02:38 AM   #3
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Original Poster
Rep: Reputation: 32
thank you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
x-java-vm problem phamtranquocvie Linux - Software 0 06-15-2005 05:42 PM
Java problem shortman1984 Linux - Software 3 03-23-2004 05:42 PM
Java problem CragStar Programming 11 03-11-2004 10:02 AM
java problem bm1 Linux - Software 4 06-01-2003 08:27 AM
Java problem $$$ XillusionsX Programming 5 12-11-2001 10:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration