• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorry - Newbie question...

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic