java - How do I get the buttons on my calculator to actually do equations? -
i wanted make small calculator project fun test out new gui knowledge java swing api. created gui, there's crucial part missing: math! question is, how add functionality each of these buttons? in simpler terms: how make buttons add text box(2+2) when user clicks buttons, , have system add numbers , display them user?
here's code:
package calculatorpack; import java.awt.borderlayout; import java.awt.color; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.uimanager; public class basiccalculatordesign { public static void main(string[] args) { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception e) { } basiccalculatordesign calc = new basiccalculatordesign(); calc.start(); } jtextarea input = new jtextarea(); string[] operators; int[] numbers; public void start() { jframe frame = new jframe(); jpanel panel = new jpanel(); jbutton 1 = new jbutton("1"); jbutton 2 = new jbutton("2"); jbutton 3 = new jbutton("3"); jbutton 4 = new jbutton("4"); jbutton 5 = new jbutton("5"); jbutton 6 = new jbutton("6"); jbutton 7 = new jbutton("7"); jbutton 8 = new jbutton("8"); jbutton 9 = new jbutton("9"); jbutton 0 = new jbutton("0"); jbutton plus = new jbutton("+"); jbutton minus = new jbutton("-"); jbutton divide = new jbutton("/"); jbutton multiply = new jbutton("*"); jbutton sqrt = new jbutton("sqrt"); jbutton percentage = new jbutton("%"); jbutton equals = new jbutton("="); // work on jlabel label = new jlabel("basic calculator"); frame.getcontentpane().add(borderlayout.north, label); frame.getcontentpane().add(borderlayout.south, input); panel.setbackground(color.blue); panel.setlayout(new gridbaglayout()); jpanel panel2 = new jpanel(); panel2.setlayout(new boxlayout(panel2, boxlayout.y_axis)); panel2.setbackground(color.red); frame.getcontentpane().add(borderlayout.east, panel2); frame.getcontentpane().add(borderlayout.center, panel); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); frame.setsize(210, 260); gridbagconstraints right = new gridbagconstraints(); right.anchor = gridbagconstraints.west; gridbagconstraints left = new gridbagconstraints(); left.anchor = gridbagconstraints.east; gridbagconstraints middle = new gridbagconstraints(); middle.anchor = gridbagconstraints.center; right.gridwidth = gridbagconstraints.remainder; right.fill = gridbagconstraints.horizontal; // add buttons panel.add(one, left); panel.add(two, middle); panel.add(three, right); panel.add(four, left); panel.add(five, middle); panel.add(six, right); panel.add(seven, left); panel.add(eight, middle); panel.add(nine, right); panel.add(zero, right); panel2.add(equals); panel2.add(plus); panel2.add(minus); panel2.add(divide); panel2.add(multiply); panel2.add(sqrt); panel2.add(percentage); //integrate buttons one.addactionlistener(new buttonlistener()); two.addactionlistener(new buttonlistener()); three.addactionlistener(new buttonlistener()); four.addactionlistener(new buttonlistener()); five.addactionlistener(new buttonlistener()); six.addactionlistener(new buttonlistener()); seven.addactionlistener(new buttonlistener()); eight.addactionlistener(new buttonlistener()); nine.addactionlistener(new buttonlistener()); zero.addactionlistener(new buttonlistener()); equals.addactionlistener(new operatorlistener()); } class buttonlistener implements actionlistener { public void actionperformed(actionevent e) { jbutton source = (jbutton)e.getsource(); input.replaceselection(source.getactioncommand()); } } class operatorlistener implements actionlistener { public void actionperformed(actionevent e) { jbutton source2 = (jbutton)e.getsource(); input.replaceselection(source2.getactioncommand()); } } // need math. class equalslistener implements actionlistener { public void actionperformed(actionevent e) { } } }
it looks want evaluate arithmetic expression. options:
write recursive descent parser, illustrated here.
evaluate expression user types in text area using
documentlistener
, discussed here.
Comments
Post a Comment