#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Creating a number pad for a calculator


    Im having trouble creating a number pad for a calculator coding in java. I hava NumberPanel class where I instantiate everything but am having difficulties setting the location of each individual JButton and then am lost on how to insert them into the JFrame....

    Any help would be greatly appreciated.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What layout manager have you looked at?
    If the components are to go in a rectangular grid, look at the GridLayout class.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Thank you Norm, Yes they will be going into a rectangular grid, so GridLayout class it is. I also wanted to know how to postion them in my CalculatorApp class. I understand that I will be using Gridlayout however Im not quite sure on how to use the CENTER, EAST,WEST, etc. positioning inside of the JFrame.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how to use the CENTER, EAST,WEST, etc. positioning inside of the JFrame.
    Those qualifiers/constraints are used by the BorderLayout manager, not the GridLayout manager.
    If there is only one component in the JFrame you can put it in the center.

    You need to take a piece of paper and design where on the window you want the components of the app to be displayed. Then decide which layout manager will help you position them where you want them.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Ok thanks again
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Everything is working with this code now, (was wondering if JLabel was the best choice to use for my output) also what modifications would you think should be made to this class if I were to use a separate class NumberPanel, where I could add action listeners to enable the numbers that are pushed to appear on the input field?


    Code:
    public class CalculatorApp extends JFrame
    {
    
    
    	private LineBuffer _myBuffer;  // a useful instance variable
    	
    	public CalculatorApp(String title){
    		super(title);  
            this.setSize(600, 300);
            this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    		
    	
    	JPanel button ; 
    	button =  new JPanel(new GridLayout(4,3));
    	
    	button.add(new JButton("7"));
    	button.add(new JButton("8"));
    	button.add(new JButton("9"));
    	button.add(new JButton("4"));
    	button.add(new JButton("5"));
    	button.add(new JButton("6"));
    	button.add(new JButton("1"));
    	button.add(new JButton("2"));
    	button.add(new JButton("3"));
    	button.add(new JButton("0"));
    	button.add(new JButton("C"));
    	button.add(new JButton("ENTER"));
    	this.add(button, BorderLayout.SOUTH);
    	
    	JPanel outP;
    	
    	outP = new JPanel();
    	outP.add(new JLabel("0"));
    	outP.setBackground(java.awt.Color.WHITE);
    	outP.setBorder(BorderFactory.createEtchedBorder());
    	this.add(outP, BorderLayout.NORTH);
    	
    	this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		CalculatorApp app = new CalculatorApp ("Calculator");
    	
    	}
    }
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit your post and wrap the code in code wrappers to preserve its formatting.

    where I could add action listeners
    Define each button with a variable name that can be used to call its addListener() method.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Code:
    public class NumberPanel extends JPanel
    {
    	
    	
    	private JButton b1, b2, b3, b4, b5, b6 ,b7, b8, b9, b0, bpi, bEnter; 
    	private JLabel empty; 
    	private JTextField _textField; 
    	
    	public NumberPanel()
    	{
    		super();
    		
    		
    		
    		ButtonListener listener = new ButtonListener();
    		
    		b1 = new JButton ("1");
    		b1.addActionListener(listener);
    		
    		b2 = new JButton("2");
    		b2.addActionListener(listener);
    		
    		b3 = new JButton("3");
    		b3.addActionListener(listener);
    		
    		b4 = new JButton("4");
    		b4.addActionListener(listener);
    		
    		b5 = new JButton("5");
    		b5.addActionListener(listener);
    		
    		b6 = new JButton("6");
    		b6.addActionListener(listener);
    		
    		b7 = new JButton("7");
    		b7.addActionListener(listener);
    		
    		b8 = new JButton("8");
    		b8.addActionListener(listener);
    		
    		b9 = new JButton("9");
    		b9.addActionListener(listener);
    		
    		b0 = new JButton("0");
    		b0.addActionListener(listener);
    		
    		bpi = new JButton("π");
    		bpi.addActionListener(listener);
    		
    		bEnter = new JButton("ENTER");
    		bEnter.addActionListener(listener);
    		
    		JPanel button = new JPanel(new java.awt.GridLayout(4,3));
    		
    		
    		button.add(b7);
    		button.add(b8);
    		button.add(b9);
    		button.add(b4);
    		button.add(b5);
    		button.add(b6);
    		button.add(b1);
    		button.add(b2);
    		button.add(b3);
    		button.add(b0);
    		button.add(bpi);
    		button.add(bEnter);
    		
    		this.add(button);
    		
    		
    		
    			
    		
    	}
    	
    	
    	
    	
    //this is now in my CalculatorApp class
    
     NumberPanel theNumberPanel = new NumberPanel();
    	
    	this.add(theNumberPanel, BorderLayout.SOUTH);
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Does it compile, execute and display what you are looking for?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Compiles and displays. Still lost when it comes to how the numbers will be displayed in the JLabel after they are pushed.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Originally Posted by kd324
    Compiles and displays. Still lost when it comes to how the numbers will be displayed in the JLabel after they are pushed.
    Look at the methods for the JLabel class to see what is available for controlling what it displays.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Code:
    public class CalculatorApp extends JFrame
    {
    
    
    	private LineBuffer _myBuffer;  // a useful instance variable
    	private JTextField _outputField;
    	private NumberPanel _theNumberPanel;
    	private FunctionButton _thefunctionButtons;
    
    
    	public CalculatorApp(String title)
    	{
    		super(title);  
    		this.setSize(400, 250);
    		this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    
    
    
    		_theNumberPanel = new NumberPanel();
    
    		this.add(_theNumberPanel, BorderLayout.SOUTH);
    		
    		_thefunctionButtons = new FunctionButton();
    		this.add(_thefunctionButtons, BorderLayout.CENTER);
    
    		_outputField = new JTextField(20);
    		_outputField.setHorizontalAlignment(JTextField.RIGHT);
    		_outputField.setEditable(false);
    		 new Font("SansSerif", Font.BOLD, 16);
    		 _outputField.setFont(new Font("SansSerif", Font.BOLD, 16));
    		
    
    		JPanel outputPanel;
    
    		outputPanel = new JPanel();
    		outputPanel.setSize(600,75);
    		outputPanel.add(_outputField);
    		
    		
    		
    		this.add(outputPanel, BorderLayout.NORTH);
    
    		this.setVisible(true);
    	}
    
    	public static void main(String[] args) 
    	{
    		CalculatorApp app = new CalculatorApp ("Calculator");
    
    	}
    }
    Have the JButtons all set, just working on how I can use an action listener to display the button pushed into the JTextField?
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    use an action listener to display the button pushed into the JTextField
    A reference to the component that had the event is passed to the listener in the object passed to the listener. Get that reference, cast it to be a button and you can call the button's methods to get the text shown on the button.
    It be useful to have different listeners for the numbers and for the operators to allow isolation of code.

IMN logo majestic logo threadwatch logo seochat tools logo