Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorry - Newbie question...

 
Mark Owens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a hard start here. I am trying to write my first applets and I cannot even get the System.out.println method to work. Since I cannot even print "hello world" this is not very fun. Here is an example below that I typed in out of Roberts/Heller about event handling. I think the event handling would work if I could get the text to print...
It compiles but I can not get any simple strings to print. Maybe they are printing the same color as the background?
All help appreciated. There are tufts of hair on my desk I am so frustrated!
Here is my short program:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Action Performed");
}
}
public class ListenerTest extends Applet
{
public void init()
{
Button btn = new Button("OK");
MyActionListener listener = new MyActionListener();
btn.addActionListener(listener);
add(btn);
}

}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops!
u cant use system.out.println inside applets. u got to define another method called public void paint(Graphics g) and pass the string "Action performed" through a string. u also got to use drawString(msg,x,y) to print the string msg at (x,y) inside ur appletwindow.
U better check it up in some standard text. Hope this helps.
Ganesh
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you use Netscape (also IE?) it allows you to see Java console which displays the outputs.
Alternatively, you can convert your applet into an application by defining main method in ListenerTest class. Try adding this:
 
Mark Owens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers. I have made considerable progress. Inside the java console, my "listener" applet was indeed handling the button press event. I can also print out a line of text in an applet now. It would have been nice if one of these fancy books would summarize the differences between applets and applications. I am reading "thinking in Java" next, perhaps it will make it clear.
I still can not make my applet work yet. I cannot put the Paint() method into the MyActionListener class where the System.outprintln statement was before. I get some errors that do not seem like syntax errors. Can I put the paint method inside another method?
Thanks again,
Mark
 
Theresa Duick
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since paint(){ } is a method, it stands alone & is overridden by whatever commands you put within it's brackets. I have not seen any examples where paint() is used inside another method. That may not be possible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic