java - Quick Advice with GUI simple alignment? -
i'm new java , got assignment. have create math frame operations , has gui formatted way. can except 3 textfields stacked on top. this:
t1 t2 t3
these 3 go down on left vertically, while other 4 boxes across bottom.
where t1, t2 , t3 textfields , 1, 2, 3 , 4 functions of operation.
i can't x, y , z on left. here's code, please if can!
convertframe.java:
import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingconstants; public class convertframe extends jframe { private final jlabel prompt; // label prompt user enter fahrenheit private final jlabel display; // label display temperature in celsius private final jtextfield temperaturef; // textfield enter temperature private jbutton b1; private jbutton b2; private jbutton b3; private jbutton b4; private jtextfield t1; private jtextfield t2; private jtextfield t3; private jlabel operation; private jlabel equl; // constructor sets gui public convertframe() { super("math frame"); prompt = new jlabel("enter 2 numbers:"); operation = new jlabel(" opr "); equl = new jlabel(" = "); temperaturef = new jtextfield(10); // textfield jpanel panel = new jpanel(); panel.setlayout(new gridlayout(1, 3)); jpanel panel2 = new jpanel(); panel2.setlayout(new gridlayout(1, 5)); b1 = new jbutton("add"); b2 = new jbutton("sub"); b3 = new jbutton("mul"); b4 = new jbutton("clear"); t1 = new jtextfield(7); t2 = new jtextfield(7); t3 = new jtextfield(7); panel2.add(t1); panel2.add(operation); panel2.add(t2); panel2.add(equl); panel2.add(t3); panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); // register anonymous action listener handler1 h1 = new handler1(); handler2 h2 = new handler2(); handler3 h3 = new handler3(); handler4 h4 = new handler4(); b1.addactionlistener(h1); b2.addactionlistener(h2); b3.addactionlistener(h3); b4.addactionlistener(h4); display = new jlabel(""); add(prompt, borderlayout.north); // north region add(panel2, borderlayout.center); // center region add(panel, borderlayout.south); // south region } // end convertframe constructor private class handler1 implements actionlistener // anonymous inner class { public void actionperformed(actionevent e) { string st1 = t1.gettext(); string st2 = t2.gettext(); int x = integer.parseint(st1); int y = integer.parseint(st2); int z = x + y; string st3 = ""; st3 = st3 + z; t3.settext(st3); } } // private class handler2 implements actionlistener // anonymous inner class { public void actionperformed(actionevent e) { string st1 = t1.gettext(); string st2 = t2.gettext(); int x = integer.parseint(st1); int y = integer.parseint(st2); int z = x - y; string st3 = ""; st3 = st3 + z; t3.settext(st3); } } // end anonymous inner class private class handler3 implements actionlistener // anonymous inner class { public void actionperformed(actionevent e) { string st1 = t1.gettext(); string st2 = t2.gettext(); int x = integer.parseint(st1); int y = integer.parseint(st2); int z = x * y; string st3 = ""; st3 = integer.tostring(z); t3.settext(st3); } } // end anonymous inner class private class handler4 implements actionlistener // anonymous inner class { public void actionperformed(actionevent e) { t1.settext(""); t2.settext(""); t3.settext(""); } } // end anonymous inner class // end call addactionlistener } // end class convertframe
convert.java:
import javax.swing.jframe; public class convert { public static void main(string[] args) { convertframe convertframe = new convertframe(); convertframe.setdefaultcloseoperation(jframe.exit_on_close); convertframe.setsize(500, 400); // set frame size convertframe.setvisible(true); // display frame } } // end class
if want stack textfields on top
you should change
panel2.setlayout(new gridlayout(1, 5));
to this
panel2.setlayout(new gridlayout(5, 1));
because in new gridlayout(row, column)
first argument row count , next 1 column count .currently have 1 row , 5 columns.so text field positioned coulmns .if want position vertically should use 1 column , 5 rows
Comments
Post a Comment