sysntax error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nu b

    sysntax error?

    Hello all. I am new and I am having a tough time spotting my error here. The
    idea is to produce an applet that will have a JButton which I can push to
    display the string myName. Later I will make it so that it will chage the
    font size and style, but for now I can't even get it to display the String.
    It compiles fine but when I launch it in the browser or with the
    appletviewer, it shows the button and nothing happens when I click it. Can
    you see anything wrong with the way i set this up? Thanks in advance.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event. *;

    public class JBlueGrey extends JApplet implements ActionListener
    {
    String myName = new String("Randy") ;
    JButton btnPush = new JButton("Push") ;
    Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

    public void init()
    {
    Container con = getContentPane( );
    con.setLayout(n ew FlowLayout());
    con.add(btnPush );
    btnPush.addActi onListener(this );
    }

    public void actionPerformed (ActionEvent e)
    {
    Object source = e.getSource();
    if (source == btnPush)
    {
    Graphics draw = getGraphics();
    draw.setFont(fi rstFont);
    draw.drawString (myName, 10, 100);
    }

    }

    }


  • Folke Bengtsson

    #2
    Re: sysntax error?

    The modified code works.
    Added setSize in the JApplets init method.
    Added a main method for test, and call init from that method.

    Your approach for drawing could be better.
    implement a method called
    public void paint(Graphics g){
    g.drawString("r andy", x, y);
    }

    and do all your drawing in that method (it is called from the
    awt-thread, for all components that are to be drawn).

    By implementing paint(..), we are telling awt to do the painting of the
    component. paint is overridden to be handled correctly by the
    swing-thread.

    Don't mix awt and swing.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event. *;

    public class JBlueGrey extends JApplet implements ActionListener
    {
    String myName = new String("Randy") ;
    JButton btnPush = new JButton("Push") ;
    Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

    public void init()
    {
    Container con = getContentPane( );
    con.setLayout(n ew FlowLayout());
    con.add(btnPush );
    btnPush.addActi onListener(this );
    //added
    setSize(100, 100);
    //to here
    }

    public void actionPerformed (ActionEvent e)
    {
    Object source = e.getSource();
    if (source == btnPush)
    {
    Graphics draw = getGraphics();
    draw.setFont(fi rstFont);
    draw.drawString (myName, 10, 100);
    }

    }
    //added
    public static void main(String[] args){
    JFrame jf = new JFrame("etst");

    JBlueGrey jApplet = new JBlueGrey();

    jApplet.init();

    jf.setContentPa ne(jApplet);
    jf.pack();
    jf.setVisible(t rue);
    }
    //to here
    }



    In article <aDHkb.30727$gi 2.28720@fed1rea d01>, nu b
    <yrnuthin@yahoo .com> wrote:
    [color=blue]
    > import javax.swing.*;
    > import java.awt.*;
    > import java.awt.event. *;
    >
    > public class JBlueGrey extends JApplet implements ActionListener
    > {
    > String myName = new String("Randy") ;
    > JButton btnPush = new JButton("Push") ;
    > Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);
    >
    > public void init()
    > {
    > Container con = getContentPane( );
    > con.setLayout(n ew FlowLayout());
    > con.add(btnPush );
    > btnPush.addActi onListener(this );
    > }
    >
    > public void actionPerformed (ActionEvent e)
    > {
    > Object source = e.getSource();
    > if (source == btnPush)
    > {
    > Graphics draw = getGraphics();
    > draw.setFont(fi rstFont);
    > draw.drawString (myName, 10, 100);
    > }
    >
    > }
    >
    > }[/color]

    Comment

    • Folke Bengtsson

      #3
      Re: sysntax error?

      In article <20102003105826 7530%Folke.Beng tsson@frobozz.s e>, Folke
      Bengtsson <Folke.Bengtsso n@frobozz.se> wrote:
      [color=blue]
      > The modified code works.
      > Added setSize in the JApplets init method.
      > Added a main method for test, and call init from that method.
      >
      > Your approach for drawing could be better.
      > implement a method called
      > public void paint(Graphics g){[/color]
      hmm...sorry.. should probably be
      public void paintComponent( Graphics g){..}

      I think I'm mixing awt and swing.
      [color=blue]
      > g.drawString("r andy", x, y);
      > }
      >
      > and do all your drawing in that method (it is called from the
      > awt-thread, for all components that are to be drawn).
      >
      > By implementing paint(..), we are telling awt to do the painting of the
      > component. paint is overridden to be handled correctly by the
      > swing-thread.
      >
      > Don't mix awt and swing.
      >
      >
      > import javax.swing.*;
      > import java.awt.*;
      > import java.awt.event. *;
      >
      > public class JBlueGrey extends JApplet implements ActionListener
      > {
      > String myName = new String("Randy") ;
      > JButton btnPush = new JButton("Push") ;
      > Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);
      >
      > public void init()
      > {
      > Container con = getContentPane( );
      > con.setLayout(n ew FlowLayout());
      > con.add(btnPush );
      > btnPush.addActi onListener(this );
      > //added
      > setSize(100, 100);
      > //to here
      > }
      >
      > public void actionPerformed (ActionEvent e)
      > {
      > Object source = e.getSource();
      > if (source == btnPush)
      > {
      > Graphics draw = getGraphics();
      > draw.setFont(fi rstFont);
      > draw.drawString (myName, 10, 100);
      > }
      >
      > }
      > //added
      > public static void main(String[] args){
      > JFrame jf = new JFrame("etst");
      >
      > JBlueGrey jApplet = new JBlueGrey();
      >
      > jApplet.init();
      >
      > jf.setContentPa ne(jApplet);
      > jf.pack();
      > jf.setVisible(t rue);
      > }
      > //to here
      > }
      >
      >
      >
      > In article <aDHkb.30727$gi 2.28720@fed1rea d01>, nu b
      > <yrnuthin@yahoo .com> wrote:
      >[color=green]
      > > import javax.swing.*;
      > > import java.awt.*;
      > > import java.awt.event. *;
      > >
      > > public class JBlueGrey extends JApplet implements ActionListener
      > > {
      > > String myName = new String("Randy") ;
      > > JButton btnPush = new JButton("Push") ;
      > > Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);
      > >
      > > public void init()
      > > {
      > > Container con = getContentPane( );
      > > con.setLayout(n ew FlowLayout());
      > > con.add(btnPush );
      > > btnPush.addActi onListener(this );
      > > }
      > >
      > > public void actionPerformed (ActionEvent e)
      > > {
      > > Object source = e.getSource();
      > > if (source == btnPush)
      > > {
      > > Graphics draw = getGraphics();
      > > draw.setFont(fi rstFont);
      > > draw.drawString (myName, 10, 100);
      > > }
      > >
      > > }
      > >
      > > }[/color][/color]

      Comment

      Working...