java - Attempting to write code for a simple calculator. Trouble displaying text in textbox when user presses a button -
towards end of code write if commands using getactioncommand see button user presses. want make whatever user presses added textbox can see entering. reason whatever number press first seems show rest of numbers thereafter. how can fix this?
//michael moradi //period c1 //june 2, 2015 //final project semester 2 import javax.swing.*; //imports needed code import java.awt.*; import java.awt.event.*; public class calculator extends jframe implements actionlistener { public jbutton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16; public jtextarea text; public calculator() { setsize(350,300); //sets size 300 300 //look how lock size setresizable(false); //does not let user change size of window setdefaultcloseoperation(exit_on_close);//makes app close when press x on top left container contentpane = getcontentpane();//gets contentpane contentpane.setbackground(color.cyan);//sets background color white contentpane.setlayout(new flowlayout());//makes contentpane read left right text = new jtextarea(1, 25); contentpane.add(text); text.seteditable(false); button1 = new jbutton ("7"); contentpane.add(button1); button1.addactionlistener(this); button2 = new jbutton ("8"); contentpane.add(button2); button2.addactionlistener(this); button3 = new jbutton ("9"); contentpane.add(button3); button3.addactionlistener(this); button4 = new jbutton ("÷"); contentpane.add(button4); button4.addactionlistener(this); button5 = new jbutton ("4"); contentpane.add(button5); button5.addactionlistener(this); button6 = new jbutton ("5"); contentpane.add(button6); button6.addactionlistener(this); button7 = new jbutton ("6"); contentpane.add(button7); button7.addactionlistener(this); button8 = new jbutton ("x"); contentpane.add(button8); button8.addactionlistener(this); button9 = new jbutton ("1"); contentpane.add(button9); button9.addactionlistener(this); button10 = new jbutton ("2"); contentpane.add(button10); button10.addactionlistener(this); button11 = new jbutton ("3"); contentpane.add(button11); button11.addactionlistener(this); button12 = new jbutton ("-"); contentpane.add(button12); button12.addactionlistener(this); button13 = new jbutton ("0"); contentpane.add(button13); button13.addactionlistener(this); button14 = new jbutton ("."); contentpane.add(button14); button14.addactionlistener(this); button15 = new jbutton ("="); contentpane.add(button15); button15.addactionlistener(this); button16 = new jbutton ("+"); contentpane.add(button16); button16.addactionlistener(this); } public static void main(string[] args) { calculator guiwindow = new calculator(); //uses gui guiwindow.setvisible(true); //makes visible } public void actionperformed(actionevent e) { container contentpane = getcontentpane(); string enterednumbers = text.gettext(); if (e.getactioncommand().equals("7")); text.settext(enterednumbers + ("7")); if (e.getactioncommand().equals("8")); text.settext(enterednumbers + ("8")); if (e.getactioncommand().equals("9")); text.settext(enterednumbers + ("9")); if (e.getactioncommand().equals("÷")); text.settext(enterednumbers + ("÷")); if (e.getactioncommand().equals("4")); text.settext(enterednumbers + ("4")); if (e.getactioncommand().equals("5")); text.settext(enterednumbers + ("5")); if (e.getactioncommand().equals("6")); text.settext(enterednumbers + ("6")); if (e.getactioncommand().equals("x")); text.settext(enterednumbers + ("x")); if (e.getactioncommand().equals("1")); text.settext(enterednumbers + ("1")); if (e.getactioncommand().equals("2")); text.settext(enterednumbers + ("2")); if (e.getactioncommand().equals("3")); text.settext(enterednumbers + ("3")); if (e.getactioncommand().equals("-")); text.settext(enterednumbers + ("-")); if (e.getactioncommand().equals("0")); text.settext(enterednumbers + ("0")); if (e.getactioncommand().equals(".")); text.settext(enterednumbers + (".")); if (e.getactioncommand().equals("=")); text.settext(enterednumbers + ("=")); if (e.getactioncommand().equals("+")); text.settext(enterednumbers + ("+")); } }
you have semicolons directly after each if-statement. semicolon empty statement, cause if statement needn't semicolon @ end. empty statement (just semicolon) nothing , completes normaly without error.
now empty statement @ end of if statement means, if expression of if statement evaluates true, nothing done , next statement executed whether or not if statement returns true.
so instead of
if (e.getactioncommand().equals("7")); text.settext(enterednumbers + ("7")); use this
if (e.getactioncommand().equals("7")) text.settext(enterednumbers + ("7"));
Comments
Post a Comment