The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Creating a number pad for a calculator
Discuss Creating a number pad for a calculator in the Java Help forum on Dev Shed. Creating a number pad for a calculator Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 19th, 2012, 06:06 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
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.
|

November 19th, 2012, 07:03 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
What layout manager have you looked at?
If the components are to go in a rectangular grid, look at the GridLayout class.
|

November 23rd, 2012, 10:48 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
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.
|

November 23rd, 2012, 10:55 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

November 23rd, 2012, 10:57 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
Time spent in forums: 1 h 5 m 22 sec
Reputation Power: 0
|
|
|
Ok thanks again
|

November 23rd, 2012, 12:29 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
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");
}
}
|

November 23rd, 2012, 12:35 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

November 23rd, 2012, 01:00 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
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);
|

November 23rd, 2012, 01:03 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Does it compile, execute and display what you are looking for?
|

November 23rd, 2012, 01:05 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
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.
|

November 23rd, 2012, 01:07 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

November 26th, 2012, 02:12 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 8
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?
|

November 26th, 2012, 02:19 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|