Page 1 of 2 12 LastLast
Results 1 to 20 of 35
Like Tree1Likes

Thread: help to checking the error!!

  1. #1
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default help to checking the error!!

    the question is You can change the size of a JFrame by using the setSize(int h, int v) method, where h and v give the horizontal and vertical dimensions of the applet’s window in pixels. Write a GUI application that contains two JButtons, labeled “Big” and “Small.” Whenever the user clicks on Small, set the applets dimensions to 200 x 100, and whenever the user clicks on Big, set the dimensions to 300 x 200.
    my program on below:

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

    public class tma2 extends JFrame implements ActionListener
    {

    public tma2()
    {
    JButton startButton = new JButton("BIG");
    JButton startButton = new JButton("SMALL");
    JPanel panel = new JPanel();
    panel.add(startButton);
    }
    public void actionPerformed(ActionEvent e)
    {
    Object src = evt.getSource();
    if (src == button1) {
    setSize(200,100);
    }
    else if (src == button2)
    {
    setSize(300,200);
    }
    }
    }
    please let me know the error of this question!!!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: help to checking the error!!

    You reassign startButton in the constructor.

    You are comparing src with a variable that has not been declared

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: help to checking the error!!

    A JFrame isn't an Applet. Learn the difference.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    so how to modify?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    As as_Marshy suggests, don't declare your JButton variables inside of the constructor, since those variables are only visible within the constructor. For instance:

    Java Code:
    public class MyFoo {
      private MyBar myBar1;  // is visible throughout the class
    
      public MyFoo() {
         myBar1 = new myBar();  // again, this is visible throughout the class
         MyBar myBar2 = new MyBar();  // this is visible only inside of this constructor
      }
    }

  6. #6
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    Quote Originally Posted by Fubarable View Post
    As as_Marshy suggests, don't declare your JButton variables inside of the constructor, since those variables are only visible within the constructor. For instance:

    Java Code:
    public class MyFoo {
      private MyBar myBar1;  // is visible throughout the class
    
      public MyFoo() {
         myBar1 = new myBar();  // again, this is visible throughout the class
         MyBar myBar2 = new MyBar();  // this is visible only inside of this constructor
      }
    }
    XML Code:
       public tma2()
            	{
                    JButton startButton = new JButton("BIG");
                    JButton startButton = new JButton("SMALL");
    this not same with the visible only inside of this constructor?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Yes, your code is having the same situation since your JButton variables are declared in the constructor and thus are only visible within the constructor, a situation you don't want. But that code snippet won't compile since you appear to be declaring the same variable, startButton twice.
    Last edited by Fubarable; 04-22-2012 at 03:34 AM.

  8. #8
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    can you show how to comply the program?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    It's hard to know where exactly you're stuck right now. Based on our advice so far, what have you tried, and how hasn't it worked?

  10. #10
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    Quote Originally Posted by Fubarable View Post
    It's hard to know where exactly you're stuck right now. Based on our advice so far, what have you tried, and how hasn't it worked?
    i try to run the program,but cannot comply the program ,so i need to help!!!

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    i try to run the program,but cannot comply the program ,so i need to help!!!
    Then please post the latest code attempt and any errors reported by your compiler.

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: help to checking the error!!

    What is your exact problem. Programming is not an opinion. Like Poetry or language. In order to get help describe your errors. If not errors describe your output compared with your expected output. Don't assume anything. Assumption is like garlic to a programmers vampire certain problems lol
    .

  13. #13
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    Quote Originally Posted by Fubarable View Post
    Then please post the latest code attempt and any errors reported by your compiler.
    this is my comply program and get the error!
    help to checking the error!!-11.jpg

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: help to checking the error!!

    You posted this code hours ago and were told its problems. Do you mean to tell me you have not changed anything yet expect different results?

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: help to checking the error!!

    12 hours ago!!! Have you changed any code based on advice in twelve hours? Einstein said insanity is doing the same thing over and over and expecting new results
    .
    Fubarable likes this.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    There are many many of mistakes in that code, some of which have been pointed out, but others as well, including
    • adding a semicolon `;` after your if statement -- this short-circuits the if statement. Check the tutorials to see how they are written and try to emulate this.
    • same for your else statement -- get rid of the trailing semicolon.
    • Re-declaring src as a String. It isn't a String but rather a JButton.
    • Trying to compare src to a non-existing variable. button1 doesn't exist in the code above.
    • You still declare your JButton variables in the constructor despite our recommending against this.
    • You still are using the same variable name for both JButtons despite our recommending against this.
    • Code indentation that appears randomly produced making your code very difficult to read, understand and debug. Indent 3 spaces for each block and be consistent and precise with your indentation.


    I hate to suggest this, but I think that you should start over, that you should compile your code often and correct any and all compilation errors before adding any new code.

  17. #17
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: help to checking the error!!

    Fubarable is being way too nice so I'm going to tell you out straight you need to learn the basics of java I.e how to write a class. I don't mean to be cruel but you are trying to sprint before you can crawl. And don't be discouraged we all started with the basics.,
    Last edited by al_Marshy_1981; 04-22-2012 at 06:08 AM.

  18. #18
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    Quote Originally Posted by Fubarable View Post
    There are many many of mistakes in that code, some of which have been pointed out, but others as well, including
    • adding a semicolon `;` after your if statement -- this short-circuits the if statement. Check the tutorials to see how they are written and try to emulate this.
    • same for your else statement -- get rid of the trailing semicolon.
    • Re-declaring src as a String. It isn't a String but rather a JButton.
    • Trying to compare src to a non-existing variable. button1 doesn't exist in the code above.
    • You still declare your JButton variables in the constructor despite our recommending against this.
    • You still are using the same variable name for both JButtons despite our recommending against this.
    • Code indentation that appears randomly produced making your code very difficult to read, understand and debug. Indent 3 spaces for each block and be consistent and precise with your indentation.


    I hate to suggest this, but I think that you should start over, that you should compile your code often and correct any and all compilation errors before adding any new code.
    can do like this?
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class tma2 extends JFrame {
    
    	private JButton myFirstButton;
    	private JButton mySecondButton;
    
    	// Constructor for a new frame
    
    	public tma2() {
    
    		super("4a");
    
    		myFirstButton = new JButton("BIG");
    	//	myFirstButton.setFont(new Font( "Arial", Font.BOLD, 18));
    	//	myFirstButton.setBackground(Color.red);
    
    		mySecondButton = new JButton("Small");
    	//	mySecondButton.setFont(new Font( "Arial", Font.BOLD, 18));
    	//	mySecondButton.setBackground(Color.green);
    
    		Container c = getContentPane();
    		FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
    		c.setLayout(fl);
    
    		c.add (myFirstButton);
    		c.add (mySecondButton);
    
    		ButtonHandler handler = new ButtonHandler();	//creation of a new Object
    		myFirstButton.addActionListener(handler);	   // Attach/register handler to myFirstButton
    		mySecondButton.addActionListener(handler);	  //Attach/register handler to mySecondButton
    
    		setSize(200, 100);
    		show();
    	}
    
    
    	public static void main(String [] args) {
    
    		// Make frame
    		tma2 f = new tma2();
    
    		f.addWindowListener(
    			new WindowAdapter() {
    				public void windowClosing(WindowEvent e) {
    
    					// This closes the window and terminates the
    					// Java Virtual Machine in the event that the
    					// Frame is closed by clicking on X.
    					System.out.println("Exit via windowClosing.");
    					System.exit(0);
    				}
    			}
    		);
    	} // end of main
    
    	// inner class for button event handling
    	private class ButtonHandler implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			if (e.getSource() == myFirstButton) {
    				setSize(300, 200);//System.out.println("Left Button has been pressed.");
    			}
    			if (e.getSource() == mySecondButton) {
    			setSize(200, 100);//	System.out.println("Right Button has been pressed.");
    			}
    		}
    	} // end of inner class
    } // end of outer class
    if i want to change when i click the button hide the big and small button ,how to do?

  19. #19
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: help to checking the error!!

    Well done. If that is your code and you understand it. I'm not sure about your language so can't be sure what you mean by hide.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    can do like this?
    ...
    <code>
    Yes you can, and that code is *much* better!

    if i want to change when i click the button hide the big and small button ,how to do?
    Consider using only one JButton, and in your single JButton's ActionListener code, change the text of the JButton via setText. You'll need to check the text too to see what state the button is in (whether the text is "Big" or "Small"), and for this call getText() on the JButton. Note that when using an if block to check if two Strings are equal, don't compare the Strings with the == operator, but instead you'll want to use the equals(...) or better for you, the equalsIgnoreCase(...) method for Strings.

Page 1 of 2 12 LastLast

Similar Threads

  1. Checking if a Bit is Set.
    By Hypnos in forum New To Java
    Replies: 11
    Last Post: 01-18-2012, 09:27 PM
  2. Checking DTD syntax...
    By xlomo in forum XML
    Replies: 0
    Last Post: 10-01-2011, 02:14 AM
  3. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 01:54 PM
  4. how to do checking value
    By madhuks in forum Java Servlet
    Replies: 3
    Last Post: 07-21-2010, 09:35 AM
  5. Checking if a file name already has .txt in it
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 12:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •