Results 1 to 11 of 11
Thread: Jbutton problem
- 11-30-2010, 08:33 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
- 11-30-2010, 08:36 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
- 11-30-2010, 09:44 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 01:43 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
So where should I put it?
- 11-30-2010, 02:30 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 02:33 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
- 11-30-2010, 02:44 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
- 11-30-2010, 02:51 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-30-2010, 07:05 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
Thanks again for the helpJava 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
sorry about the comment tabs this thing does not like those they look bad in here.
- 11-30-2010, 07:24 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
- 12-01-2010, 04:43 AM #11
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
-
JButton click problem
By mine0926 in forum NetBeansReplies: 7Last Post: 06-11-2010, 03:00 AM -
JButton.setBackground() Problem
By ellias2007 in forum AWT / SwingReplies: 2Last Post: 02-24-2010, 09:49 PM -
A problem with JScrollBar and the Jbutton
By cowboy in forum New To JavaReplies: 5Last Post: 12-19-2009, 09:17 PM -
JButton Problem
By wassim in forum AWT / SwingReplies: 6Last Post: 02-18-2009, 10:29 PM -
Problem with JButton
By Marcus in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 05:56 AM


LinkBack URL
About LinkBacks


Bookmarks