Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: why cannot I change the size of the TextField in Java for the calculator application?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default why cannot I change the size of the TextField in Java for the calculator application?

    package name;
     
    //swing library found in javax called to use the graphical contents.
    import javax.swing.*;
     
     
    //creating the contents for the calculator
    public class calc {
    	JButton btn1= new JButton("1 ");
    	JButton btn2= new JButton("2");
    	JButton btn3= new JButton("3");
    	JButton btn4= new JButton("4");
    	JButton btn5= new JButton("5");
    	JButton btn6= new JButton("6");
    	JButton btn7= new JButton("7");
    	JButton btn8= new JButton("8");
    	JButton btn9= new JButton("9");
    	JButton btn0= new JButton("0");
    	JButton btnAdd= new JButton("+");
    	JButton btnMinus= new JButton("-");
    	JButton btnDivide= new JButton("/");
    	JButton btnMultiply= new JButton("x");
    	JButton btnBulletPoint= new JButton(".");
    	JButton btnEqual= new JButton("=");
    	JButton btnC= new JButton("C");
    	JButton btnCE= new JButton("CE");
    	JButton btnPercent= new JButton("%");
    	JButton btnSqrt= new JButton("sqrt");
    	JButton btn1X= new JButton("1/X");
    	JButton btn1X2= new JButton("1/X");
    	JButton btnBack= new JButton("Backspace");
    	JTextField Text= new JTextField();
     
    	//public class to layout all the buttons in individual panels
    	public JPanel ContentButton(){
     
    		//parent panel to add the sub (child) panels
    		JPanel ButtonLayout= new JPanel();
     
     
    		//allows me to manually set the layout
    		ButtonLayout.setLayout(null);
     
     
    		//panel for the number,T/X and bullet point
    		JPanel NumberPanel = new JPanel();
    		NumberPanel.setSize(150, 150);
    		NumberPanel.setLocation(10,100);
     
    		NumberPanel.add(btn7);
    		NumberPanel.add(btn8);
    		NumberPanel.add(btn9);
    		NumberPanel.add(btn4);
    		NumberPanel.add(btn5);
    		NumberPanel.add(btn6);
    		NumberPanel.add(btn1);
    		NumberPanel.add(btn2);
    		NumberPanel.add(btn3);
    		NumberPanel.add(btn1X);
    		NumberPanel.add(btn0);
    		NumberPanel.add(btnBulletPoint);
    		ButtonLayout.add(NumberPanel);
     
    		//panel for the math symbols
    		JPanel mathsSymbol= new JPanel();
    		mathsSymbol.setSize(110,160);
    		mathsSymbol.setLocation(200, 70);
     
    		mathsSymbol.add(btnC);
    		mathsSymbol.add(btnCE);
    		mathsSymbol.add(btnDivide);
    		mathsSymbol.add(btnSqrt);
    		mathsSymbol.add(btnMultiply);
    		mathsSymbol.add(btn1X2);
    		mathsSymbol.add(btnMinus);
    		mathsSymbol.add(btnPercent);
    		mathsSymbol.add(btnAdd);
    		mathsSymbol.add(btnEqual);
    		ButtonLayout.add(mathsSymbol);
     
     
     
    		//panel for the Backspace
    JPanel backspace = new JPanel();
     
    		backspace.setSize(100, 50);
    		backspace.setLocation(26,65);
    		backspace.add(btnBack);
    		ButtonLayout.add(backspace);
     
     
    	//panel for the TextField.Y AXIS (up or down) is 40 and X AXIS (left or right) is -5.
    	JPanel text = new JPanel();
    	text.setSize(200, 200);
    	text.setLocation(140,30);
    	text.add(Text);
    	ButtonLayout.add(text);
     
     
     
    		return ButtonLayout;
    	}
     
    	private static void TheFrame(){
     
    		//creating the frame to be assigned to the panel
    		JFrame TitleFrame = new JFrame("Calculator");
     
    		//to be run when the application is required to be run
    		calc calc = new calc();
     
    		/*The panels created in the ContentButton class is
    		 * assigned with the Frame created in this class */
    		TitleFrame.setContentPane(calc.ContentButton());
     
    		TitleFrame.setSize(400, 300);
            TitleFrame.setVisible(true);
    	}
     
     
     
    	 public static void main(String[] args) {
    	        //Schedule a job for the event-dispatching thread:
    	        //creating and showing this application's GUI.
    	        SwingUtilities.invokeLater(new Runnable() {
    	            public void run() {
    	            	TheFrame();
    	            }});
    		//AdvancedCalculator end
    	}
    }

    I have tried different sizes for the 'TextField' but when I change the size, the position changes instead of the size itself. can someone explain why this is and how can I resolve this issue. btw I am currently a beginner at Java programming and I am a learner who needs explanation of how the code works by a experienced Java programmer while coding and using Java books to understand the language. So plz be patient with me while explaining the solution to me.

    thanks in advancement.

    here is how the application currently looks like.

    advanced calc print screen.jpg


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: why cannot I change the size of the TextField in Java for the calculator application?

    First off, you need to use standard naming conventions. Classes start with an upper-case letter, methods and variables start with a lower-case letter. Your code is very confusing to read as-is.

    Secondly, why are you using a null layout? Use a layout manager and let it handle everything for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why cannot I change the size of the TextField in Java for the calculator application?

    i have used a null layout because according to about.com, using null will allow me to lay the contents manually.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: why cannot I change the size of the TextField in Java for the calculator application?

    True. But have you considered the question: why do all the components need to be on the same panel with the same layout?
    For example, you have a grid of number keys. Ideally, you would use something like GridLayout for this. However, GridLayout doesn't fit your need for your entire GUI, so your solution could be to break up the sections of your GUI. Create a JPanel which contains your number keys and uses GridLayout. Then create a JPanel which contains your special keys (like plus, minus, ect.) and also use GridLayout to align them in the grid you want. Then create a third JPanel and add the JTextField result with the layout which makes sense.
    Then, add all of those JPanels to your frame's content pane (using whichever layout works for you).

    This is an alternative solution to using null layout. Divide and conquer.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Indonesia
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why cannot I change the size of the TextField in Java for the calculator application?

    I think your code in JTextField Text = new JTextField();
    you can write on brackets. for example you want 15 number in your textfield. So, JTextField Text = new JTextField(15);
    and then, You must make JPanel. You can write JPanel on
    public JPanel ContentButton(){
    JPanel ButtonLayout= new JPanel();
    JPanel pane = new JPanel();
    pane.add(Text);

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: why cannot I change the size of the TextField in Java for the calculator application?

    I think your code in JTextField Text = new JTextField();
    you can write on brackets. for example you want 15 number in your textfield. So, JTextField Text = new JTextField(15);
    That will set the column width of the JTextField to 15. If you wanted to put the number 15 in the text field, you have to do it as a String:
    JTextField Text = new JTextField("15");
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 7
    Last Post: March 19th, 2013, 09:45 AM
  2. Replies: 7
    Last Post: March 19th, 2013, 09:45 AM
  3. Replies: 18
    Last Post: February 15th, 2013, 06:30 AM
  4. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  5. [SOLVED] Change the size of an image
    By subhvi in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 23rd, 2009, 11:44 PM

Tags for this Thread