LinuxQuestions.org
Visit Jeremy's Blog.
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 04-28-2006, 05:55 AM   #1
linuxfreak3
Member
 
Registered: Feb 2004
Location: UK
Distribution: Debian/Backtrack
Posts: 55

Rep: Reputation: 15
Simple java applet?


Hi all
I was wondering if someone can show me how to make a simple java applet with some buttons like submit and reset and maybe some others?

Thanks in Advance
 
Old 04-28-2006, 05:58 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
There're a few tutorials at Sun's homepage:

http://java.sun.com/applets/

Other tutorials:

http://www.dgp.toronto.edu/~mjmcguff/learn/java/

http://www.realapplets.com/tutorial/
 
Old 04-28-2006, 06:01 AM   #3
deroB
Member
 
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208

Rep: Reputation: 30
here's a really good place to start
http://java.sun.com/docs/books/tutor...etStarted.html

or maybe get a book.
I tought myself with "Sams teach youself java in 24 days" and can highly recommend it....if it's still available
 
Old 04-28-2006, 07:35 AM   #4
linuxfreak3
Member
 
Registered: Feb 2004
Location: UK
Distribution: Debian/Backtrack
Posts: 55

Original Poster
Rep: Reputation: 15
i´ve done a little code now and need some help fixing the errors i get when i try to compile the file.
Here is the code:
Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class cdapplet extends java.applet.Applet implements ActionListener
public void init ()
{
Color favvobla = new Color(51,102,153;
setBackground(favvobla);
makeTextFields();

makebuttons ();
makeTextArea ();
}
public void makeTextFields()
{
add(new Label ("Artist: "));
name = new TextField(40);
add(name);
}
public void make TextArea()
{
  display = new TextArea (50,50);
  add(display);
}
public void makeButtons()
{
laggtTillArtist = new Button ("Lägg till Cd");
laggtTillArtist.addActionListener (this);
add(laggTillArtist );
}
here is the errors:
E:\Documents and Settings\Iczer\My Documents\java\bin>javac cdapplet.java
cdapplet.java:5: illegal start of type
public void init()
^
cdapplet.java:19: '{' expected
}
^
error: interface expected here
3 errors

any ide how to fix it?
 
Old 04-28-2006, 07:39 AM   #5
qwerty
Member
 
Registered: Feb 2005
Location: England
Distribution: Ubuntu 5.10
Posts: 80

Rep: Reputation: 15
I think you need to add a '{' after the line
Code:
public class cdapplet extends java.applet.Applet implements ActionListener
and a '}' right at the end of the class

Last edited by qwerty; 04-28-2006 at 07:40 AM.
 
Old 04-28-2006, 07:42 AM   #6
deroB
Member
 
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208

Rep: Reputation: 30
a few typo's.

you need an "{" at the end of this line
Code:
public class cdapplet extends java.applet.Applet implements ActionListener {
and a closing bracket here:
Code:
Color favvobla = new Color(51,102,153);
also it is best to use Capitals for class names so you can easily tell them apart from from variables....
so CdApplet instead of cdapplet

and it looks like you will also need a "}" at the end of the file to close the class cdapplet

EDIT
you also need to do the following
Code:
TextField name = new TextField(40);
....and for same for 
TextArea display = new TextArea (50,50);
Button laggtTillAtrist = new Button ("Lägg till Cd");
You will also need the method "actionPerformed" to compile as you implement ActionListener.

Code:
public void actionPerformed(ActionEvent e){
    //add code here
}

Last edited by deroB; 04-28-2006 at 07:56 AM.
 
Old 04-28-2006, 07:46 AM   #7
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
for starters there is an open parenthesis on line 7.
Also I didn't see a constructor. YOu will need something like

public cdapplet(){
init();

}

right after the class begins "public class cdapplet extends java.applet.Applet implements ActionListener{...put it here
 
Old 04-28-2006, 08:43 AM   #8
linuxfreak3
Member
 
Registered: Feb 2004
Location: UK
Distribution: Debian/Backtrack
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks for the help, but its more errors that i cant fix now.
Any suggestions?
Code:
Cdapplet.java:10: illegal start of expression
public void actionPerformed(ActionEvent e){
^
Cdapplet.java:34: ';' expected
}
 ^
Cdapplet.java:4: cdapplet should be declared abstract; it does not define action
Performed(java.awt.event.ActionEvent) in cdapplet
public class cdapplet extends java.applet.Applet implements ActionListener{
       ^
3 error
I wasnt sure what code to but between
Code:
public void actionPerformed(ActionEvent e){
    //add code here
}
So i tried the following
Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class cdapplet extends java.applet.Applet implements ActionListener{
public void init ()
{

Color favvobla = new Color(51,102,153);
setBackground(favvobla);
public void actionPerformed(ActionEvent e){
    

makeTextFields();

makebuttons ();
makeTextArea ();
}
public void makeTextFields()
{
add(new Label ("Artist: "));
TextField name = new TextField(40);
add(name);
}
public void make TextArea()
{
  TextArea display = new TextArea (50,50);
  add(display);
}
public void makeButtons()
{
Button laggtTillAtrist = new Button ("Lägg till Cd");
laggtTillArtist.addActionListener (this);
add(laggTillArtist );
}
} 
}
 
Old 04-28-2006, 09:04 AM   #9
deroB
Member
 
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208

Rep: Reputation: 30
here
this compiles but I'll let you do the testing.

good luck

Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class CdApplet extends java.applet.Applet implements ActionListener{

public void init (){

    Color favvobla = new Color(51,102,153);
    setBackground(favvobla);
    makeTextFields();
    makeButtons ();
    makeTextArea ();

}

public void makeTextFields(){
    add(new Label ("Artist: "));
    TextField name = new TextField(40);
    add(name);
}

public void makeTextArea(){
    TextArea display = new TextArea (50,50);
    add(display);
}

public void makeButtons(){
    Button laggTillArtist = new Button ("Lägg till Cd");
    laggTillArtist.addActionListener (this);
    add(laggTillArtist);
}

public void actionPerformed(ActionEvent e){
    System.out.println("laggtTillArtist button pressed");
}

}//end class
 
Old 04-28-2006, 09:16 AM   #10
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Ah en svensk . Well, your code has more typos than other errors. Remember that Java is a case sensitive language. Button and button and BUTTON are three different variables. I've corrected your code and it should compile and run, but I'm sure the results will not be as pretty as you'd expect:

Code:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CDApplet extends JApplet implements ActionListener {
	
    public void init () {

	Color favvobla = new Color(51,102,153);
	setBackground(favvobla);
		
        makeButtons();
	makeTextArea();
	makeTextFields();
				
    }
		
    public void makeTextFields() {

	add(new Label ("Artist: "));
	TextField name = new TextField(40);
	add(name);
		
    }
		
    public void makeTextArea() {
	TextArea display = new TextArea (50,50);
	add(display);
    }
	
    public void makeButtons() {

	Button laggtTillArtist = new Button ("Lägg till Cd");

	laggtTillArtist.addActionListener (this);
	add(laggtTillArtist);
    }
	
    public void actionPerformed(ActionEvent e){
    
	makeTextFields();

	makeButtons ();
	
	makeTextArea ();
	
    }	
}
You may want to take a look in this thread where I tried to help another member with applet and listeners:

http://www.linuxquestions.org/questi...d.php?t=439390
http://www.linuxquestions.org/questi...d.php?t=439033

I'd highly recommend you to use an IDE too. It can be as simple as Dr.Java or BlueJ, up to a full-feature one as Eclipse. I recommend this because your coding style has a few problems with indentation that makes it hard to read and will eventually confuse you..

Regards!

Last edited by Mega Man X; 05-15-2006 at 12:16 AM.
 
Old 04-28-2006, 09:18 AM   #11
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Oops... deroB is a fast typer . Anyway, either take a look in those IDE's I've recommended or read a little about Java programming style here:

http://geosoft.no/development/javastyle.html
 
Old 04-28-2006, 09:36 AM   #12
linuxfreak3
Member
 
Registered: Feb 2004
Location: UK
Distribution: Debian/Backtrack
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks for all the help
I´l try all of this after i installed knoppix.
Im getting an error when i try to install Unbuntu to, its says it can t find /dev/hdd5, i dont have hdd5 :S
any ide´s how to fix it?
i dont know how to get inside the fstab because it says no premission or something to me when i try it :S
 
Old 04-28-2006, 10:04 AM   #13
deroB
Member
 
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208

Rep: Reputation: 30
you'll need to have root privillages to edit fstab.
type "su" at the command line to become root, then edit it.

do you know any non graphical text editors? you may have problems trying to open GUI editors as root.

Last edited by deroB; 04-28-2006 at 10:06 AM.
 
Old 04-28-2006, 11:38 AM   #14
linuxfreak3
Member
 
Registered: Feb 2004
Location: UK
Distribution: Debian/Backtrack
Posts: 55

Original Poster
Rep: Reputation: 15
im a newbie at linux. How do i open the GUI editor ?
 
Old 04-28-2006, 11:51 AM   #15
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
su -c 'gedit /etc/fstab'

or

su -c 'kedit /etc/fstab'

or you can replace the editor with any you chose.
if you want to be able to use the terminal while the editor is open do

$ su -

then
$ gedit /etc/fstab &

(The $ sign means the prompt the terminal is giving you, you do not need to type that yourself)
 
  


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
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
JAVA JRE installation to view java applet through browser dipenchaudhary Linux - Software 1 01-23-2006 09:20 AM
Java applet error: "Applet Failed" nro Programming 1 08-28-2004 05:52 PM
A simple question:how to compile java applet in linux? nickhx Programming 5 08-11-2004 03:11 AM

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

All times are GMT -5. The time now is 07:48 PM.

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