Results 1 to 11 of 11

Thread: couple of questions on this simple calculator

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    couple of questions on this simple calculator

    Right now, I've just got the interface of the calculator. It's not too great and that's one thing I would like help with, any improvements will do. Also, the biggest thing I need help with, is the textfield. If you look at other calculators, you see they go right to left, instead of left to right. How can I do this? Another thing, I tried the setMaximizable(false) method, and it didn't work.

    Hope I'm not asking to much.

  2. #2

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: couple of questions on this simple calculator

    I guess it would help if you had the code!

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator extends JFrame implements ActionListener
    {
    	JButton[] btnNums;
    	JButton btnBack;
    	JButton btnClear;
    	JButton btnCalculate;
    	
    	JTextField txtDisplay;
    	
    	public Calculator()
    	{
    		Container content = getContentPane();
    		setSize(210,250);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		/*  construct a JPanel and the textfield that 
    		 *  will show the inputed values and outputed 
    		 *  results
    		 */
    		JPanel jpDisplay = new JPanel();
    		jpDisplay.setLayout(new BorderLayout());
    		txtDisplay = new JTextField(15);
    		jpDisplay.add(txtDisplay);
    		
    		/*  contstruct a JPanel that will contain a back
    		 *  button and a clear button.
    		 */
    		JPanel jpRow2 = new JPanel();
    		btnBack = new JButton("Backspace");
    		btnClear = new JButton("Clear");
    		jpRow2.add(btnBack);
    		jpRow2.add(btnClear);
    		
    		/*  construct a string array with all the names of the
    		 *  buttons, then in a for loop create the new buttons
    		 *  and add their name and an actionListener to them.
    		 */
    		String[] strNames = {"7","8", "9","/", "4", "5", "6","*", "1", "2", "3","+", "0", "+/-", ".", "-"};
    		btnNums = new JButton[16];
    		JPanel jpButtons = new JPanel();
    		jpButtons.setLayout(new GridLayout(4,4));
    		
    		  for (int i = 0; i < 16; i++)
    		  {
    			  btnNums[i] = new JButton(strNames[i]);
    			  btnNums[i].addActionListener(this);
    			  jpButtons.add(btnNums[i]);
    		  }
    		  
    		  /*  construct the final JPanel and add a 
    		   *  calculate button to it.
    		   */
    		  JPanel jpLastRow = new JPanel();
    		  btnCalculate = new JButton("Calculate");
    		  jpLastRow.add(btnCalculate);
    		  
    		  /*  set the contentPane and create the layout
    		   *  add the panels to the container
    		   */
    		  setContentPane(content);
    		  setLayout(new FlowLayout());
    		  content.add(jpDisplay, BorderLayout.NORTH);
    		  content.add(jpRow2);
    		  content.add(jpButtons);
    		  content.add(jpLastRow);
    		  setTitle("Mini Calculator");
    		  setVisible(true); 
    		  
    	}
    	
    	public void actionPerformed(ActionEvent ae)
    	{
    	}
    	
    	public static void main(String[] args)
    	{
    		Calculator calc = new Calculator();
    	}
    }

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: couple of questions on this simple calculator

    For the alignment, this came up in my search : http://www.it.bton.ac.uk/staff/je/ja...TextField.html


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: couple of questions on this simple calculator

    Never mind with that...

    Code:
    TF.setHorizontalAlignment(JTextField.RIGHT);


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: couple of questions on this simple calculator

    To disable the maximizing you can do this :

    Code:
    setResizable(false);
    that will disable all resizing (including maximizing). But I'm not sure it disabling all resizing is what you want


    Has someone helped you? Then you can Rate their helpful post.

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: couple of questions on this simple calculator

    ahhh thank you manova!!

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: couple of questions on this simple calculator

    Quote Originally Posted by System_Error
    ahhh thank you manova!!
    You're welcome

    *manavo


    Has someone helped you? Then you can Rate their helpful post.

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: couple of questions on this simple calculator

    Hey, one more thing! How does the layout/interface look? Does anything need to be changed or added?

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: couple of questions on this simple calculator

    Quote Originally Posted by System_Error
    Hey, one more thing! How does the layout/interface look? Does anything need to be changed or added?
    Something that might be useful is a square root button, it's common

    Also, as a side note, when I tried to run it I got this note :

    Exception in thread "main" java.lang.Error: Do not use Calculator.setLayout() us
    e Calculator.getContentPane().setLayout() instead
    at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
    at javax.swing.JFrame.setLayout(JFrame.java:531)
    at Calculator.<init>(Calculator.java:65)
    at Calculator.main(Calculator.java:81)
    Which of course was fixed by changing this line :

    Code:
    setLayout(new FlowLayout());
    to :

    Code:
    getContentPane().setLayout(new FlowLayout());
    as it suggested


    Has someone helped you? Then you can Rate their helpful post.

  10. #10

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: couple of questions on this simple calculator

    hmm I didn't get that error. Im running 1.5, so that's probably why.

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: couple of questions on this simple calculator

    Quote Originally Posted by System_Error
    hmm I didn't get that error. Im running 1.5, so that's probably why.
    That would probably explain it, I have 1.4.2


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

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



Click Here to Expand Forum to Full Width