java - What is the best way to make my buttons on my calculator interactive, so that they can actually add, subtract, etc? -
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?
import java.awt.borderlayout; import java.awt.color; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; 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(); } 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("="); jtextfield input = new jtextfield(); 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); } }
is possible implement single inner class, , use inner class multiple times different buttons.
you can use same action (actionlistener) number (0-9) buttons. like:
public void actionperformed(actionevent e) { jbutton source = (jbutton)e.getsource(); display.replaceselection( source.getactioncommand() ); }
where "display" textfield displays digits , result. code append digit of button clicked display.
for other buttons should create separate actions because different functions.
Comments
Post a Comment