Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very Very New

 
jide agboola
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
MaryT Tsele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jide agboola
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic