problems compiling a swing program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mak1084
    New Member
    • Jul 2007
    • 44

    problems compiling a swing program

    hi friends i'm not able to debug this code please tell me wht is wrong here...


    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.swing.*;
    class test extends JFrame implements ActionListener
    {
    	
    	JButton b1;
    	JTextField Tf1;
    	public test()
    	{
    		b1=new JButton("1");
    		Tf1=new TextField(10);
    		
    		setLayout(new Frlowlayout());
    		
    		add(b1);
    		add(Tf1);
    		
    		addActionListener(this);
    		
    		setSize(200,200);
    		setVisible(true);
    						
    	}
    	public void actionPerformed(ActionEvent e)
    	if (e.getSource() == b1)
    	{
    		Tf1.setText("1");
    	}
    	public static void main(String args[])
    	{
    		test t=new test();
    	}
    }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    [CODE=java]
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*; // Correction 1
    class test extends JFrame implements ActionListener
    {

    JButton b1;
    JTextField Tf1;
    public test()
    {
    b1=new JButton("1");
    Tf1=new JTextField(10); // Correction 2

    setLayout(new Frlowlayout()); // What should that be?

    add(b1);
    add(Tf1);

    addActionListen er(this); // This IS an ActionListener - why do you want to add one?

    setSize(200,200 );
    setVisible(true );

    }
    public void actionPerformed (ActionEvent e) // Correction 3
    {
    if (e.getSource() == b1)
    {
    Tf1.setText("1" );
    }
    }
    public static void main(String args[])
    {
    test t=new test();
    }
    }
    [/CODE]

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by mak1084
      hi friends i'm not able to debug this code please tell me wht is wrong here...


      Code:
      import java.awt.*;
      import java.awt.event.*;
      import java.swing.*;
      class test extends JFrame implements ActionListener
      {
      	
      	JButton b1;
      	JTextField Tf1;
      	public test()
      	{
      		b1=new JButton("1");
      		Tf1=new TextField(10);
      		
      		setLayout(new Frlowlayout());
      		
      		add(b1);
      		add(Tf1);
      		
      		addActionListener(this);
      		
      		setSize(200,200);
      		setVisible(true);
      						
      	}
      	public void actionPerformed(ActionEvent e)
      	if (e.getSource() == b1)
      	{
      		Tf1.setText("1");
      	}
      	public static void main(String args[])
      	{
      		test t=new test();
      	}
      }
      Frlowlayout should have been FloorLayout?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by r035198x
        Frlowlayout should have been FloorLayout?
        Probably FlowLayout. ^^

        Comment

        • praveen2gupta
          New Member
          • May 2007
          • 200

          #5
          Hi
          Line setLayout(new Frlowlayout());
          is wrong It should be written as
          setLayout(new Flowlayout());
          Rest is o.k.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by r035198x
            Frlowlayout should have been FloorLayout?
            Oh yeah, thanks.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Do not add JComponents to a JFrame directly. Get the JFrame's content pane
              (see the API docs) and add your components to that pane instead.

              kind regards,

              Jos

              Comment

              • mak1084
                New Member
                • Jul 2007
                • 44

                #8
                [PHP]import java.awt.*;
                import java.awt.event. *;
                import javax.swing.*;
                class test extends JFrame implements ActionListener
                {

                JButton b1;
                JTextField Tf1;
                public test()
                {
                b1=new JButton("1");
                Tf1=new JTextField(10);

                setLayout(new Flowlayout());

                add(b1);
                add(Tf1);

                addActionListen er(this);

                setSize(200,200 );
                setVisible(true );

                }
                public void actionPerformed (ActionEvent e)
                if (e.getSource() == b1)
                {
                Tf1.setText("1" );
                }
                public static void main(String args[])
                {
                test t=new test();
                }
                }[/PHP]

                after compiling geting this error...

                Code:
                C:\test.java:26: ';' expected
                    if (e.getSource() == b1)

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by mak1084
                  [PHP]import java.awt.*;
                  import java.awt.event. *;
                  import javax.swing.*;
                  class test extends JFrame implements ActionListener
                  {

                  JButton b1;
                  JTextField Tf1;
                  public test()
                  {
                  b1=new JButton("1");
                  Tf1=new JTextField(10);

                  setLayout(new Flowlayout());

                  add(b1);
                  add(Tf1);

                  addActionListen er(this);

                  setSize(200,200 );
                  setVisible(true );

                  }
                  public void actionPerformed (ActionEvent e)
                  if (e.getSource() == b1)
                  {
                  Tf1.setText("1" );
                  }
                  public static void main(String args[])
                  {
                  test t=new test();
                  }
                  }[/PHP]

                  after compiling geting this error...

                  Code:
                  C:\test.java:26: ';' expected
                      if (e.getSource() == b1)
                  See how you defined your, eg, main method; spot the diferences from your
                  actionPerformed method (don't mention 'static').

                  kind regards,

                  Jos

                  Comment

                  • mak1084
                    New Member
                    • Jul 2007
                    • 44

                    #10
                    Originally posted by JosAH
                    See how you defined your, eg, main method; spot the diferences from your
                    actionPerformed method (don't mention 'static').

                    kind regards,

                    Jos
                    Not geting wht u want to say i'm novice in java....

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by mak1084
                      Not geting wht u want to say i'm novice in java....
                      you have to defined a method like this:

                      [code=java]
                      <access rights and type> methodName(<opt ional params here ...> {

                      <statements>

                      }
                      [/code]

                      Note the two curly brackets and have another look at your actionPerformed method.

                      kind regards,

                      Jos

                      Comment

                      • mak1084
                        New Member
                        • Jul 2007
                        • 44

                        #12
                        Originally posted by JosAH
                        you have to defined a method like this:

                        [code=java]
                        <access rights and type> methodName(<opt ional params here ...> {

                        <statements>

                        }
                        [/code]

                        Note the two curly brackets and have another look at your actionPerformed method.

                        kind regards,

                        Jos
                        thnk u very much i think i really really not made for java this was silly mistake...and i was not able to debug shame on me :(
                        thnx a lot for yor help..

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by mak1084
                          thnk u very much i think i really really not made for java this was silly mistake...and i was not able to debug shame on me :(
                          thnx a lot for yor help..
                          No need to worry; you'll learn; all you have to do is practice and read.

                          kind regards,

                          Jos

                          Comment

                          • mak1084
                            New Member
                            • Jul 2007
                            • 44

                            #14
                            hi again i stuck in this code...can you please tell me how to add to values in Text field.

                            [CODE=java]import java.awt.*;
                            import java.awt.event. *;
                            import javax.swing.*;
                            class test extends JFrame implements ActionListener
                            {

                            JButton b1,b2,b3,b4;
                            JTextField Tf1;
                            String s,s1,k;

                            public test()
                            {
                            b1=new JButton("1");
                            b2=new JButton("2");
                            b3=new JButton("add");
                            b4=new JButton("=");
                            Tf1=new JTextField(10);

                            setLayout(new FlowLayout());

                            add(b1);
                            add(b2);
                            add(b3);
                            add(b4);
                            add(Tf1);

                            b1.addActionLis tener(this);
                            b2.addActionLis tener(this);
                            b3.addActionLis tener(this);
                            b4.addActionLis tener(this);

                            setSize(200,200 );
                            setVisible(true );

                            }
                            public void actionPerformed (ActionEvent e)
                            {
                            if (e.getSource() == b1)
                            {
                            Tf1.setText("1" );
                            }
                            else if (e.getSource() == b2)
                            {
                            s=Tf1.getText() ;
                            Tf1.setText("2" );
                            }
                            else if (e.getSource() == b3)
                            {
                            Tf1.setText("") ;

                            }
                            else if (e.getSource() == b4)
                            {
                            s1=Tf1.getText( );
                            k=s+s1;
                            Tf1.setText(k); //Its concanating instead of adding..
                            }
                            }
                            public static void main(String args[])
                            {
                            test t=new test();
                            }
                            }[/CODE]

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by mak1084
                              hi again i stuck in this code...can you please tell me how to add to values in Text field.
                              Please read my reply #7 again before you code any further.

                              kind regards,

                              Jos

                              Comment

                              Working...