GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Very Very New Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very Very New" Watch "Very Very New" New topic
Author

Very Very New

jide agboola
Greenhorn

Joined: Nov 11, 2006
Posts: 9
i am trying to develop a calculator, i have able to develop the GUI from the frame class but still have problem in writing codes for the keys i.e event hanlers...

could anyone be of help to make me develop my first real application.
Thanks
[ November 13, 2006: Message edited by: jide agboola ]

When you are going through Hell, you keep going<br /> <br /> Winston Churchill.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by jide agboola:
I m a very new man in java, i love to have people i can quickly mail for question...

Welcome to JavaRanch!

I think your best approach would be to post such questions in the JavaRanch forums.

(PS: You might want to remove your email address, because it's a good target for SPAM. You can click on the paper/pencil icon to edit your own posts.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

Hi Jide, welcome to JavaRanch.

Can you please explain in more detail what you are having problems with? Show us the source code that you've written so far (please use code tags) and explain where exactly you get stuck.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
MaryT Tsele
Greenhorn

Joined: Oct 30, 2006
Posts: 8
Hi Jide

Welcome to the ranch.

You might want to use a switch statement to alternate between your keys.If your calculator GUI has a textfield, which will be the only textfield.Create the methods for add,minus,multiply and divide,do your calculations in this methods by getting the value from the textfield,then in your case:key(specify the key to be pressed) call the methods you have created. I hope this will help.Feel free to reply if you are still confused.
Good luck.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a neat trick. Build the calculator first without the GUI and just drive it with a test program.

Then when you build your GUI it's obvious what each button listener has to do - just send the text from the button to the calculator.

This test program asks for the answer. Making the answer pop into the results box is a different scheme, probably involving a call back or listener.

Show us what you've made so we can see where you're stuck and try to get you going again.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
jide agboola
Greenhorn

Joined: Nov 11, 2006
Posts: 9
this is the Calculator code that i developed, the GUI was not perfectly arranged as i wanted but still contains all neccessary components that i needed. My main problem now is to get those buttons working and i will later learn more about my Layout manager.



import java.awt.*;
import javax.swing.*;

class jideCalculator extends JFrame {
//setting row1
JPanel row1 = new JPanel();
JTextField screen = new JTextField(15);
//setting row2
JPanel row2 = new JPanel();
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonDel = new JButton("DEL");
JButton buttonAc = new JButton("AC");
//setting row3
JPanel row3 = new JPanel();
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton buttonX = new JButton("X");
JButton buttonDivision = new JButton("/");
//seting row4
JPanel row4 = new JPanel();
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton buttonPlus = new JButton("+");
JButton buttonMinus = new JButton("-");
//setting row5
JPanel row5 = new JPanel();
JButton buttonZero = new JButton("0");
JButton buttonPeriod = new JButton(".");
JButton buttonComma = new JButton(",");
JButton buttonResult = new JButton("=");
//setting the Frame
public jideCalculator() {
super("Jide's Calculator");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(5, 1, 10, 10);
Container pane = getContentPane();
pane.setLayout(layout);

//setting row1 layout
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setLayout(layout1);
row1.add(screen);
screen.setEditable(false);
pane.add(row1);
//setting row2 layout
FlowLayout layout2 = new FlowLayout();
row2.setLayout(layout2);
row2.add(button7);
row2.add(button8);
row2.add(button9);
row2.add(buttonDel);
row2.add(buttonAc);
pane.add(row2);
//setting row3 layout
FlowLayout layout3 = new FlowLayout();
row3.setLayout(layout3);
row3.add(button4);
row3.add(button5);
row3.add(button6);
row3.add(buttonX);
row3.add(buttonDivision);
pane.add(row3);
//setting row4 layout
FlowLayout layout4 = new FlowLayout();
row4.setLayout(layout4);
row4.add(button1);
row4.add(button2);
row4.add(button3);
row4.add(buttonPlus);
row4.add(buttonMinus);
pane.add(row4);
//setting row5 layout
FlowLayout layout5 = new FlowLayout();
row5.setLayout(layout5);
row5.add(buttonZero);
row5.add(buttonPeriod);
row5.add(buttonComma);
row5.add(buttonResult);
pane.add(row5);
setVisible(true);
}
//execution statement
public static void main(String[] arguments) {
jideCalculator jide = new jideCalculator();
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
Look into the concept of "action listeners", which are callback methods that get invoked when a button is clicked, so that the application can react to the button click. The Sun Java tutorial has a section on this.


Ping & DNS - my free Android networking tools app
 
GeeCON Prague 2014
 
subject: Very Very New