• Post Reply Bookmark Topic Watch Topic
  • New Topic

stuck on gui still

 
Mike Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me before I jump off a bridge or something. Just kidding, but I am frustrated. This is related to my last post. I am trying to figure out how to paint to the applet when the user clicks the button. I can't get it to compile. Here is my code so far followed by the errors. Thanks for your help.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class practice1 extends JApplet implements ActionListener
{
String mystring = new String("Practice message");
JButton pushit = new JButton("Push");
public void init()
{
Container con = getContentPane();
con.setLayout(new FlowLayout() );
con.add(pushit);
pushit.addActionListener(this);
}

public void ActionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == pushit)
{
Graphics msg = getGraphics();
msg.drawString(mystring);
}
}
}

*******************************************************************
C:\j2sdk1.4.2\bin\practice1.java:5: practice1 should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in practice1
class practice1 extends JApplet implements ActionListener
^
C:\j2sdk1.4.2\bin\practice1.java:26: cannot resolve symbol
symbol : method drawString (java.lang.String)
location: class java.awt.Graphics
msg.drawString(mystring);
^
2 errors
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Java is case sensitive..
it should be
public void actionPerformed(ActionEvent e)
because practice1 implements ActionListener, it should override the abstract method, actionPerformed(ActionEvent e) of ActionListener.
2)There is no such method like drawString() with single argument in Graphics.
You should provide the (x,y) coordinate also - where to display the string.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of Vijay's points are correct, but I want to add another: you can't just call getGraphics() and then call painting methods on that Graphics object and expect to see something on the screen, especially on a component that's being used as a container for other components. You're trying to draw into an area that's being used to contain the button right now, and although you might get something to flash onscreen for a split second it'd be gone before you could read it.
If you want an applet which just draw something, with no buttons, then here's what you do: override the paintComponent() method in the applet like this:

This applet will display your string.
Now, if you want the applet to display a string only aftr a button is pushed, do the following:
  • Move that paintComponent() method into another class -- a subclass of JPanel. Give this JPanel class a member variable mystring and initialize it to "".
  • In the applet, create an instance of the JPanel class and store a reference to in in a member variable of the applet called myPanel.
  • [list] Add the panel to the applet -- i.e., con.add(myPanel). I would recommend not using a FlowLayout, but the default BorderLayout instead, and adding the two components like this:
    [list] Make the actionPerformed() method look like this:


    And there you go!
    [ October 25, 2003: Message edited by: Ernest Friedman-Hill ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!