Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2012, 06:06 PM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation Power: 0
Homework - 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.

Reply With Quote
  #2  
Old November 19th, 2012, 07:03 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,041 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 50 m 42 sec
Reputation Power: 346
What layout manager have you looked at?
If the components are to go in a rectangular grid, look at the GridLayout class.

Reply With Quote
  #3  
Old November 23rd, 2012, 10:48 AM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation 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.

Reply With Quote
  #4  
Old November 23rd, 2012, 10:55 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,041 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 50 m 42 sec
Reputation Power: 346
Quote:
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.

Reply With Quote
  #5  
Old November 23rd, 2012, 10:57 AM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation Power: 0
Ok thanks again

Reply With Quote
  #6  
Old November 23rd, 2012, 12:29 PM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation 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");
	
	}
}

Reply With Quote
  #7  
Old November 23rd, 2012, 12:35 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,041 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 50 m 42 sec
Reputation Power: 346
Please edit your post and wrap the code in code wrappers to preserve its formatting.

Quote:
where I could add action listeners

Define each button with a variable name that can be used to call its addListener() method.

Reply With Quote
  #8  
Old November 23rd, 2012, 01:00 PM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation 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);
	
	
	
	

Reply With Quote
  #9  
Old November 23rd, 2012, 01:03 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,041 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 50 m 42 sec
Reputation Power: 346
Does it compile, execute and display what you are looking for?

Reply With Quote
  #10  
Old November 23rd, 2012, 01:05 PM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation Power: 0
Compiles and displays. Still lost when it comes to how the numbers will be displayed in the JLabel after they are pushed.

Reply With Quote
  #11  
Old November 23rd, 2012, 01:07 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,041 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 50 m 42 sec
Reputation Power: 346
Quote:
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.

Reply With Quote
  #12  
Old November 26th, 2012, 02:12 PM
kd324 kd324 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 kd324 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 22 sec
Reputation 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?

Reply With Quote
  #13  
Old November 26th, 2012, 02:19 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,041 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 50 m 42 sec
Reputation Power: 346
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Creating a number pad for a calculator

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap