swing - java Calculator Text Field -
i have create simple calculate exercise. there should 4 buttons , text field display. here code 4 buttons:
import java.awt.*; import java.awt.event.*; import javax.swing.*; class jframeexample { jbutton btn1 = new jbutton("1"); jbutton btn2 = new jbutton("2"); jbutton btn3 = new jbutton("+"); jbutton btn4 = new jbutton("="); public void calculator(){ jframe frame = new jframe("simple java calculator"); frame.setsize(320,320); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); frame.setvisible(true); jpanel headpanel = new jpanel(); jpanel numberpanel = new jpanel(); jpanel labelpanel = new jpanel(); numberpanel.add(btn1); numberpanel.add(btn2); numberpanel.add(btn3); numberpanel.add(btn4); numberpanel.setlayout(new gridlayout(2,2)); frame.add(numberpanel,borderlayout.center); } public static void main(string[] args) { jframeexample jf = new jframeexample(); jf.calculator(); } }
my problem have no idea on how insert text field. tried methods internet no result. can guys give me solution? how should that? @ least link tutorial how it.
you need jtextfield. can set , text content calling settext()
, gettext()
.
i go for:
jpanel (borderlayout) | +-- jpanel (gridlayout), borderlayout.south | | | + buttons | +-- jtextfield, borderlayout.north
Comments
Post a Comment