GridBagLayout is more flexible and more powerful than GridLayout, and the layout of the calculator it implements is as follows:
Import Java.awt.*;import javax.swing.*;/** * Calculator class, implement Calculator interface * @author Ares */public class calculator{final string[] K EYS = {"CE", "C", "←", "÷", "7", "8", "9", "X", "4", "5", "6", "-", "1", "2", "3", "+", "0", ".", "="}; Jbutton[] keys = new Jbutton[keys.length]; JTextField Resulttext = new JTextField ("0");p ublic void Addcomponentstopane (Container pane) {gridbaglayout layout = new Gr Idbaglayout ();p ane.setlayout (layout), Resulttext.setfont (New Font ("Century schoolbook", Font.plain, 14)); Resulttext.seteditable (false); Resulttext.sethorizontalalignment (swingconstants.right);p Ane.add (resultText,new GBC (0, 0, 4, 1). Setipad (+). Setweight (0.5, 0.5). Setfill (Gridbagconstraints.both)); for (int i = 0; i < keys.length; i++) {Keys[i] = new JButton (Keys[i]), if (i = = keys.length-3) {pane.add (keys[i],new GBC (i% 4, I/4 + 1,2,1). Setipad (0, 12) . setinsets (1). Setfill (Gridbagconstraints.both). Setweight (0.5, 0.5));} else if (i = = Keys.length-2 | | i = = keys.length-1) {pane.add (keys[i],new GBC (i% 4+1, I/4 + 1). Setipad (0, a). Setinsets (1). Setfill (Gridbagconstraints.both). Setweight (0.5, 0.5));} else {Pane.add (keys[i], new GBC (i% 4, I/4 + 1). Setipad (0). Setinsets (1). Setfill (Gridbagconstraints.both). Setweight ( 0.5, 0.5));}}} public void Createandshowgui () {JFrame frame = new JFrame ("Calculator"); Frame.setdefaultcloseoperation (jframe.exit_on _close); Addcomponentstopane (Frame.getcontentpane ()); Frame.pack (); frame.setvisible (true);} public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {new Calculator (). Create Andshowgui ();}});}} The classes used to set Gridbagconstraints are as follows: Import Java.awt.gridbagconstraints;import Java.awt.insets;public class GBC extends gridbagconstraints {/** * constructor, used to set the coordinates of the cell that the component occupies * * @param gridx Horizontal coordinates * @param gridy longitudinal coordinates */public GBC (int gridx, int gridy {this.gridx = Gridx;this.gridy = GridY;} /** * constructor that sets the coordinates of the cell that the component occupies, specifying both width and height * @param gridx Horizontal coordinates * @param gridy longitudinal coordinates * @param gridwidth component Width * @param gridheight Group Piece height */public GBC (int gridx, int gridy, int gridwidth, int gridheight) {this.gridx = Gridx;this.gridy = Gridy;this.gridwidth = Gridwidth;this.gridhei ght = Gridheight;} /** * When the component does not have large space, set the component's location * @param anchor Build location * @return Current object */public GBC setanchor (int anchor) {This.anchor = Anchor;retur n this;} /** * Set Fill * @param fill Set Fill method * @return Current object */public GBC setfill (int fill) {This.fill = Fill;return this;} /** * Sets the amplitude of the component to extend with the window * @param weightx horizontal Extension size * @param weighty longitudinal extension size * @return Current object */public GBC setweight (double weightx, D Ouble weighty) {this.weightx = Weightx;this.weighty = Weighty;return this;} /** * Set the spacing between components * @param distance the same as the left and right spacing * @return Current object */public GBC setinsets (int distance) {this.insets = new insets (d Istance, distance, distance, distance); return this;} /** * Set spacing between components * @param top spacing * @param left margin * @param bottom @param right pitch * @return Current object */public GBC SetI Nsets (int top, int left, int bottom, int.) {this.insets = new insets (top, left, bottom, right); /** * Set the component internal padding emptyHow much space is added to the minimum height or width of the component * @param ipadx Horizontal fill * @param ipady vertical Fill * @return Current object */public GBC setipad (int ipadx, int ipady) {T HIS.IPADX = Ipadx;this.ipady = Ipady;return this;}}
Calculator UI implemented with GridBagLayout