java - GridBagLayout is not displaying as expected -
this first foray gridbaglayout, have reviewed java documentation online (as lot of q&as) , i've creating panel i'm expecting display such:
+--------------------------+ | choose timer | +--------+--------+--------+ | 5min | 25min | 30min | +--------+--------+--------+ | 00:00:00 | +--------+--------+--------+ | start | pause | quit | +--------+--------+--------+
and here's gridbaglayout i'm using.
public class buttonsel extends jframe implements actionlistener { jlabel buttonsellabel = new jlabel("choose timer run"); jbutton pomobutton = new jbutton("00:25:00"); jbutton shrtbutton = new jbutton("00:05:00"); jbutton longbutton = new jbutton("00:30:00"); jbutton startbutton = new jbutton("go"); jbutton pausebutton = new jbutton("pause"); jbutton quitbutton = new jbutton("quit"); jlabel textdisplay = new jlabel("00:00:00"); // jpanel timerpanel = new jpanel(); // public buttonsel() { super("buttontest"); setdefaultcloseoperation(jframe.exit_on_close); setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); add(buttonsellabel, c); // line 1 c.fill = gridbagconstraints.horizontal; c.gridx = 1; c.gridy = 1; c.gridwidth = 3; add(pomobutton, c); // line 2 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; add(shrtbutton, c); c.gridx = 2; c.gridy = 2; c.gridwidth = 1; add(longbutton, c); c.gridx = 3; c.gridy = 2; c.gridwidth = 1; add(textdisplay, c); // line 3 c.fill = gridbagconstraints.horizontal; c.gridx=1; c.gridy=3; c.gridwidth=3; add(startbutton, c); // line 4 c.gridx = 1; c.gridy = 4; c.gridwidth = 1; add(pausebutton, c); c.gridx = 2; c.gridy = 4; c.gridwidth = 1; add(quitbutton, c); c.gridx = 2; c.gridy = 4; c.gridwidth = 1; pomobutton.addactionlistener(this); shrtbutton.addactionlistener(this); longbutton.addactionlistener(this); startbutton.addactionlistener(this); pausebutton.addactionlistener(this); quitbutton.addactionlistener(this); } public void actionperformed(actionevent radioselect) { object source = radioselect.getsource(); if (source == pomobutton) textdisplay.settext("00:25:00"); else if (source == shrtbutton) textdisplay.settext("00:05:00"); else if (source == longbutton) textdisplay.settext("00:30:00"); else if (source == startbutton) textdisplay.settext("started"); else if (source == pausebutton) textdisplay.settext("paused"); else if (source == quitbutton) textdisplay.settext("quit"); else textdisplay.settext("00:00:00"); } }
i getting output,
and taking cue horizontal alignment this question added horizontal
constraints the fields. i'm getting:
i've used (0,0)
, (1,1)
starting (x,y)
coordinates, interestingly enough, same results.
can see i'm missing?
i had add few lines make code runnable.
you setting gridbagconstraints after add them component. must set them before add.
the trick set sethorizontalalignment method of jlabels wanted center center alignment.
i formatted code , added insets make gui more visually appealing.
here's picture of gui.
and here's code.
import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; 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.swingconstants; import javax.swing.swingutilities; public class buttonsel extends jframe implements actionlistener { jlabel buttonsellabel = new jlabel("choose timer run"); jbutton pomobutton = new jbutton("00:25:00"); jbutton shrtbutton = new jbutton("00:05:00"); jbutton longbutton = new jbutton("00:30:00"); jbutton startbutton = new jbutton("go"); jbutton pausebutton = new jbutton("pause"); jbutton quitbutton = new jbutton("quit"); jlabel textdisplay = new jlabel("00:00:00"); jpanel timerpanel = new jpanel(); public buttonsel() { super("buttontest"); setdefaultcloseoperation(jframe.exit_on_close); timerpanel = new jpanel(); timerpanel.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; c.anchor = gridbagconstraints.center; c.weightx = 1.0d; c.weighty = 1.0d; c.gridx = 1; c.gridy = 1; c.gridwidth = 3; c.gridheight = 1; c.insets = new insets(10, 10, 0, 0); c.ipadx = 0; c.ipady = 0; buttonsellabel.sethorizontalalignment(swingconstants.center); timerpanel.add(buttonsellabel, c); // line 1 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; timerpanel.add(pomobutton, c); // line 2 c.gridx = 2; c.gridy = 2; c.gridwidth = 1; timerpanel.add(shrtbutton, c); c.gridx = 3; c.gridy = 2; c.gridwidth = 1; c.insets = new insets(10, 10, 0, 10); timerpanel.add(longbutton, c); c.fill = gridbagconstraints.horizontal; c.gridx = 1; c.gridy = 3; c.gridwidth = 3; c.insets = new insets(10, 10, 0, 0); textdisplay.sethorizontalalignment(swingconstants.center); timerpanel.add(textdisplay, c); // line 3 c.gridx = 1; c.gridy = 4; c.gridwidth = 1; c.insets = new insets(10, 10, 10, 0); timerpanel.add(startbutton, c); // line 4 c.gridx = 2; c.gridy = 4; c.gridwidth = 1; timerpanel.add(pausebutton, c); c.gridx = 3; c.gridy = 4; c.gridwidth = 1; c.insets = new insets(10, 10, 10, 10); timerpanel.add(quitbutton, c); pomobutton.addactionlistener(this); shrtbutton.addactionlistener(this); longbutton.addactionlistener(this); startbutton.addactionlistener(this); pausebutton.addactionlistener(this); quitbutton.addactionlistener(this); this.add(timerpanel); this.pack(); this.setvisible(true); } public void actionperformed(actionevent radioselect) { object source = radioselect.getsource(); if (source == pomobutton) textdisplay.settext("00:25:00"); else if (source == shrtbutton) textdisplay.settext("00:05:00"); else if (source == longbutton) textdisplay.settext("00:30:00"); else if (source == startbutton) textdisplay.settext("started"); else if (source == pausebutton) textdisplay.settext("paused"); else if (source == quitbutton) textdisplay.settext("quit"); else textdisplay.settext("00:00:00"); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new buttonsel(); } }); } }
Comments
Post a Comment