java - Why is the middle column not showing up? -
i making simple calculator gui in java start learn how use java. following code not working. first column appearing. second , third column going?
package start; import java.awt.*; import javax.swing.*; public class calculatorgui { public static void addcomponentstopane(container pane) { pane.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; jlabel label; jbutton button; label = new jlabel("i'm calculator"); c.gridwidth = gridbagconstraints.remainder; c.weightx = 0.0; c.gridx = 0; c.gridy = 0; pane.add(label, c); button = new jbutton("1"); c.gridx = 0; c.gridy = 1; pane.add(button, c); button = new jbutton("2"); c.gridx = 1; c.gridy = 1; pane.add(button, c); button = new jbutton("3"); c.gridx = 2; c.gridy = 1; pane.add(button, c); button = new jbutton("4"); c.gridx = 0; c.gridy = 2; pane.add(button, c); button = new jbutton("5"); c.gridx = 1; c.gridy = 2; pane.add(button, c); button = new jbutton("6"); c.gridx = 2; c.gridy = 2; pane.add(button, c); button = new jbutton("7"); c.gridx = 0; c.gridy = 3; pane.add(button, c); button = new jbutton("8"); c.gridx = 1; c.gridy = 3; pane.add(button, c); button = new jbutton("9"); c.gridx = 2; c.gridy = 3; pane.add(button, c); } public static void createandshowgui() { jframe frame = new jframe("calculatorgui"); frame.setdefaultcloseoperation(jframe.exit_on_close); addcomponentstopane(frame.getcontentpane()); frame.pack(); frame.setvisible(true); } public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } i tried making label c.gridwidth = 3; same thing. when make width equal 1 buttons appear, label in in 1 cell. how make label span 3 columns? without making other buttons disappear.
how make label span 3 columns without gridwidth?
c.gridwidth = 3; you need reset 1 before adding other components.
so centered on '2' button
you need set text alignment of label:
label.sethorizontalalignment(jlabel.center);
Comments
Post a Comment