Results 1 to 11 of 11

Thread: Jbutton problem

  1. #1
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Jbutton problem

    I cannot get a JButton to resize...can anyone tell me what is wrong with using Button1.setBounds(1,1,2,2);...below is the class file...it is called from another file...it gives and error that I posted after the code

    Java Code:
    import java.awt.*;
    import javax.swing.*;											//this is required for gui layout
    import java.awt.event.ActionListener;						//this is required for the buttons to work
    import java.awt.event.ActionEvent;							//this is required for the buttons to work
    
    public class UserInterfaceSetup implements ActionListener	//This creates a new class that generates a calculator
    {
    //Initialize objects to be placed on the screen...and initialize the screen and panel to put stuff on
    	JFrame frame = new JFrame("UserInterface");				//This creates the window that the calculator is in and puts the text "calculater" in the top bar of the window (still must be placed later)
    	JPanel Panel1 = new JPanel();			//This creates a new panel for putting things on that is inside of the JFrame or window whatever you want to call it (still must be placed later)
       //Panel1.setLayoutManager(null);
    	JTextArea TextEntry1 = new JTextArea(1,20);					//This creates a text entry area for entering numbers for the calculator (still must be placed later) 
    	
    	JButton Button1 = new JButton("1");							//This creates a button with a label of 1 (still must be placed later) 
    	Button1.setBounds(1,1,2,2);
    	double number1,number2,result;
    	int addc=0,subc=0,multic=0,divc=0;
    	
    	public void ui()
    	{
    		frame.setVisible(true);									//this makes the frame(window) visible on the desktop
    		frame.setSize(250,200);									//this sets the size of the frame on the desktop in pixels
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//this tells frame what to do when close button is clicked
    		
    		frame.add(Panel1);											//This adds the panel to the frame on the desktop
    			
    	   Panel1.add(TextEntry1);										//this places the text entry box on the panel
    		
    	   Panel1.add(Button1);											//this places the button on the panel
    		
    		Button1.addActionListener(this);							//adds button to the list of items to watch...in "this" instance of the class
    	
    
    
    } //end of ui method
    
    	public void actionPerformed(ActionEvent e) 			//this method occurs if an action occurs
    		{
    			Object source = e.getSource();					//this line checks to see what the source of the action was
    		
    			
    			if(source==Button1)										//if the source was button 1 then
    			{												
    				TextEntry1.setText(Double.toString(result));
    				TextEntry1.append("1");									//append a 1 to the text field value
    			}
    			
    			number2 = number_reader();
    								
    			
    		}// end of actionPerformed method
    				
    		public double number_reader()							//create a method that converts strings to integers
    		{
    			Double num1;											//create a double precision number
    			String s;												//create a string variable
    			s=TextEntry1.getText();										//get the text from the text entry box
    			num1=Double.valueOf(s);								//convert the text to a double precision number and store it to num1
    			
    			return num1;											//pass num1 back to whatever calls this method
    		}
    
    }//end of calculater_ui Class


    UserInterfaceSetup.java:15: <identifier> expected
    Button1.setBounds(1,1,2,2);
    ^
    UserInterfaceSetup.java:15: illegal start of type
    Button1.setBounds(1,1,2,2);
    ^
    UserInterfaceSetup.java:15: illegal start of type
    Button1.setBounds(1,1,2,2);
    ^
    UserInterfaceSetup.java:15: illegal start of type
    Button1.setBounds(1,1,2,2);
    ^
    UserInterfaceSetup.java:15: illegal start of type
    Button1.setBounds(1,1,2,2);
    ^
    UserInterfaceSetup.java:15: package Button1 does not exist
    Button1.setBounds(1,1,2,2);
    ^
    6 errors

  2. #2
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    supposedly I am supposed to set the layout manager to null before that will work...but that code also failed with a similar error so I commented it out

    Panel1.setLayoutManager(null);

    So Any help is appreciated

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by blue50 View Post
    Java Code:
    UserInterfaceSetup.java:15: <identifier> expected
    	Button1.setBounds(1,1,2,2);
    	                 ^
    UserInterfaceSetup.java:15: illegal start of type
    	Button1.setBounds(1,1,2,2);
    	                  ^
    UserInterfaceSetup.java:15: illegal start of type
    	Button1.setBounds(1,1,2,2);
    	                    ^
    UserInterfaceSetup.java:15: illegal start of type
    	Button1.setBounds(1,1,2,2);
    	                      ^
    UserInterfaceSetup.java:15: illegal start of type
    	Button1.setBounds(1,1,2,2);
    	                        ^
    UserInterfaceSetup.java:15: package Button1 does not exist
    	Button1.setBounds(1,1,2,2);
    	       ^
    6 errors
    You can't put ordinary statements outside the body of a method; only member variable definitions (and optional initialization expressions) can be outside a method body.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    So where should I put it?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by blue50 View Post
    So where should I put it?
    Inside a method or constructor body?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    ok I see what you are saying...I put the Button1.setBounds(30,50,50,20); in the ui method and now it compiles...but the button is still not being put at the coordinate...it still behaves like it is in flow layout...any help is appreciated.

    Java Code:
    import java.awt.*;
    import javax.swing.*;											//this is required for gui layout
    import java.awt.event.ActionListener;						//this is required for the buttons to work
    import java.awt.event.ActionEvent;							//this is required for the buttons to work
    
    public class UserInterfaceSetup implements ActionListener	//This creates a new class that generates a calculator
    {
    			//Initialize objects to be placed on the screen...and initialize the screen and panel to put stuff on
    	JFrame frame = new JFrame("UserInterface");				//This creates the window that the calculator is in and puts the text "calculater" in the top bar of the window (still must be placed later)
    	JPanel Panel1 = new JPanel();			//This creates a new panel for putting things on that is inside of the JFrame or window whatever you want to call it (still must be placed later)
       
    	JTextArea TextEntry1 = new JTextArea(1,20);					//This creates a text entry area for entering numbers for the calculator (still must be placed later) 
    	
    	JButton Button1 = new JButton("1");							//This creates a button with a label of 1 (still must be placed later) 
    
    	
    	double number1,number2,result;
    	int addc=0,subc=0,multic=0,divc=0;
    	
    	public void ui()
    	{
    		frame.setVisible(true);									//this makes the frame(window) visible on the desktop
    		frame.setSize(250,200);									//this sets the size of the frame on the desktop in pixels
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//this tells frame what to do when close button is clicked
    		
    		frame.add(Panel1);											//This adds the panel to the frame on the desktop
    	   Panel1.add(TextEntry1);										//this places the text entry box on the panel
    		
    	   Panel1.add(Button1);											//this places the button on the panel
    		Button1.setBounds(30,50,50,20);
    		Button1.addActionListener(this);							//adds button to the list of items to watch...in "this" instance of the class
    
       } //end of ui method
    
    	public void actionPerformed(ActionEvent e) 			//this method occurs if an action occurs
    		{
    			Object source = e.getSource();					//this line checks to see what the source of the action was
    		
    			
    			if(source==Button1)										//if the source was button 1 then
    			{												
    				TextEntry1.setText(Double.toString(result));
    				TextEntry1.append("1");									//append a 1 to the text field value
    			}
    			
    			number2 = number_reader();
    								
    			
    		}// end of actionPerformed method
    				
    		public double number_reader()							//create a method that converts strings to integers
    		{
    			Double num1;											//create a double precision number
    			String s;												//create a string variable
    			s=TextEntry1.getText();										//get the text from the text entry box
    			num1=Double.valueOf(s);								//convert the text to a double precision number and store it to num1
    			
    			return num1;											//pass num1 back to whatever calls this method
    		}
    
    }//end of calculater_ui Class

  7. #7
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    putting Panel1.setLayoutManager(null); still gives an error even when I put it in the method

    UserInterfaceSetup.java:22: cannot find symbol
    symbol : method setLayoutManager(<nulltype>)
    location: class javax.swing.JPanel
    Panel1.setLayoutManager(null);
    ^
    1 error

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by blue50 View Post
    putting Panel1.setLayoutManager(null); still gives an error even when I put it in the method

    UserInterfaceSetup.java:22: cannot find symbol
    symbol : method setLayoutManager(<nulltype>)
    location: class javax.swing.JPanel
    Panel1.setLayoutManager(null);
    ^
    1 error
    That error is caused by something else completely; the compiler is trying to tell you that it can't find a method 'setLayoutManager'. Check your API documentation for the correct name.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    yea you are right it should have been
    Panel1.setLayout(null);
    now it works....Thanks! sorry for the stupid questions

    Just in case someone else has these problems here is the code...

    there are two files
    UserInterface.java ...this is where main is that calls the user interface class
    UserInterfaceSetup.java ...this is where the class is that builds the ui


    UserInterface.java

    Java Code:
    public class UserInterface extends UserInterfaceSetup
    {
    	public static void main(String[] args)
    	{
    		UserInterfaceSetup n = new UserInterfaceSetup();
    		n.ui();
    
    	}
    
    }

    UserInterfaceSetup.java


    Java Code:
    import java.awt.*;														//for some of the ui layout parts
    import javax.swing.*;													//this is required for gui layout
    //import java.awt.event.ActionListener;							//this is included in java.awt.* I am just being verbose.
    //import java.awt.event.ActionEvent;								//this is included in java.awt.* I am just being verbose.
    
    public class UserInterfaceSetup implements ActionListener	//This creates is used to create a userinterface...that uses a listener for user input.
    {
    	/////////Initialize objects to be placed on the screen...and initialize the screen and panel to put stuff on
    	JFrame frame = new JFrame("UserInterface");					//This creates the window that the calculator is in and puts the text "calculater" in the top bar of the window (still must be placed later)
    	JPanel Panel1 = new JPanel();		                     	//This creates a new panel for putting things on that is inside of the JFrame or window whatever you want to call it (still must be placed later)
       JTextArea TextEntry1 = new JTextArea(1,20);					//This creates a text entry area for entering numbers for the calculator (still must be placed later) 
    	JButton Button1 = new JButton("1");								//This creates a button with a label of 1 (still must be placed later) 
    
    	////////////Initialize global variables for use
    	double number1,number2,result;
    	int someinteger1 = 0;
    
    ////ui method is for defining the look of the ui////////////////////////////////////////////////////////////////////	
    	public void ui() 		//this method holds the ui placement and visibility information
    	{
    		Panel1.setLayout(null);										//set the Panel1 layout type to null so we can type coordinates of buttons and other objects
    		frame.setVisible(true);										//this makes the frame(window) visible on the desktop
    		frame.setSize(250,200);										//this sets the size of the frame on the desktop in pixels
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//this tells frame what to do when close button is clicked
    		frame.add(Panel1);											//This adds the panel to the frame on the desktop
    	   Panel1.add(TextEntry1);										//this places the text entry box on the panel
    		TextEntry1.setBounds(30,30,50,20);
    	   Panel1.add(Button1);											//this places the button on the panel
    		Button1.setBounds(30,50,50,20);
    		
    		//list actions to be listened for by actionpferformed
    		Button1.addActionListener(this);							//adds button to the list of items to watch...in "this" instance of the class
    																			//meaning add an actionlistner to the button that is generated by this method
       } //end of ui method
    //////////////That is the end of the description on how the ui should look/////////////////////////////////////////////
    
    /////This is where we tell the program what to do if an even occurs///////////////////////////////////////////////////
    	public void actionPerformed(ActionEvent e) 				//this method occurs if an action occurs
    		{
    			Object source = e.getSource();						//this line checks to see what the source of the action was
    			
    			if(source==Button1)										//if the source was button 1 then
    			{												
    				TextEntry1.setText(Double.toString(result)); //set the text in the text box to whatever 
    				TextEntry1.append("1");								//append a 1 to the text field value
    			}
    			
    			number2 = number_reader();								//this gets a number returned by the number_reader() function
    		}// end of actionPerformed method
    /////////That was where we told the program what to do if there is an event///////////////////////////////////////////			
    
    /////////This is a method that shows how to return a varibale//////////////////////////////////////////////////////////
    		public double number_reader()							//create a method that converts strings to integers
    		{
    			Double num1;											//create a double precision number
    			String s;												//create a string variable
    			s=TextEntry1.getText();								//get the text from the text entry box
    			num1=Double.valueOf(s);								//convert the text to a double precision number and store it to num1
    			
    			return num1;											//pass num1 back to whatever calls this method
    		}
    
    }//end of UserInterfaceSetup Class
    Thanks again for the help

    sorry about the comment tabs this thing does not like those they look bad in here.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by blue50 View Post
    yea you are right it should have been
    Panel1.setLayout(null);
    now it works....Thanks! sorry for the stupid questions
    You're welcome and no need to say sorry; the lesson was: always keep the API documenation handy, it'll save you from guesswork. The facts are in that documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    1. Unless you are dragging components at runtime, there's never a need to use a null layout. Use a layout manager that suits your need, or even a combination of layout managers in nested panels.
    2. Get rid of those godawful ugle comment lines. Learn the conventions.
    Code Conventions for the Java(TM) Programming Language: Contents

    db

Similar Threads

  1. JButton click problem
    By mine0926 in forum NetBeans
    Replies: 7
    Last Post: 06-11-2010, 03:00 AM
  2. JButton.setBackground() Problem
    By ellias2007 in forum AWT / Swing
    Replies: 2
    Last Post: 02-24-2010, 09:49 PM
  3. A problem with JScrollBar and the Jbutton
    By cowboy in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 09:17 PM
  4. JButton Problem
    By wassim in forum AWT / Swing
    Replies: 6
    Last Post: 02-18-2009, 10:29 PM
  5. Problem with JButton
    By Marcus in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 05:56 AM

Posting Permissions

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