GridBagLayout:网格袋布局管理器,它不要求组件的大小相同便可以将组件垂直、水平或沿它们的基线对齐。每个GridBagLayout对象维持一个动态的矩形单元格(动态计算出单元格的大小),每个组件占用一个或多个单元格,该单元格被称为显示区域。每个组件显示区域按从左到右、从上到下依次排列。
GridBagConstraints:封装了若干对组件的约束属性,每个由GridBagLayout管理的组件都关联一个该约束实例,以指定组件所在显示区域的具体放置位置,以及组件在其显示区域中的对齐方式。给组件添加约束:GridBagLayout.setConstraints(Component comp, GridBagConstraints c)。
GridBagConstraints属性:
- gridx,gridy 组件显示区域 开始显示的位置(单元格坐标),容器左上角第一个单元格位置为 (0, 0),默认值为RELATIVE,表示放置在 “上一个组件”(所在行列最后添加的一个组件)的 “后面”。
- gridwidth,gridheight 组件显示区域 水平/竖直方向 所占单元格的个数,默认值为 1,如要占多行/列,需整体结果有足够的行/列。有如下两个常量特殊值可选:
REMAINDER: 占完所在行/列余下所有单元格(该值可实现 换行 作用);
RELATIVE: 占到所在行/列余下的倒数第二个单元格(使用该值,所在行/列的最后一个单元格需要“合理”安排组件,并手动换行)。设置该值不能导致 “前面” 或 “后面” 单元格有留空白,否则可能无效。 - weightx,weighty 如何分布额外空间(单元格区域外,容器边缘内的间隔),当制定行/列中的其中任意一个组件的权重大于0,则该行/列将(和其他行/列按权重比例)分配额外的水平/竖直空间。当权重为0时,则整个单元格区域居中于容器中心。
- fill 当显示区域大小大于组件所需要的大小时,组件在其显示区域内的填充方式。可能的值如下:NONE:不调整组件大小;HORIZONTAL:加宽组件,使它在水平方向上填满其显示区域,但是不改变高度;VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度;BOTH:使组件完全填满其显示区域。
- anchor
- ipadx,ipady 组件的内部填充(可看做组件的内边距),即对组件最大大小的添加量。
- insets 组件的外部填充(可看做是组件的外边距,也可以看做是显示区域的内边距)。
预期界面:
实现代码:
private JPanel panelResult; private JPanel panelM; private JPanel panelParam; private JTextField textFieldResult; private JButton buttonMC; private JButton buttonMR; private JButton buttonMPlus; private JButton buttonMDec; private JButton buttonMs; private JButton buttonM; private JButton buttonCE; private JButton buttonC; private JButton buttonClear; private JButton buttonDIV; private JButton button7; private JButton button8; private JButton button9; private JButton buttonMUL; private JButton button4; private JButton button5; private JButton button6; private JButton buttonDec; private JButton button1; private JButton button2; private JButton button3; private JButton buttonAdd; private JButton buttonMinus; private JButton button0; private JButton buttonPoint; private JButton buttonEqual; private void initCompents() { this.textFieldResult = new JTextField(); this.textFieldResult.setBackground(Color.GRAY); this.textFieldResult.setForeground(Color.white); this.textFieldResult.setFont(new Font(Font.SERIF, Font.BOLD, 32)); this.textFieldResult.setHorizontalAlignment(SwingConstants.RIGHT); this.textFieldResult.setEnabled(false); this.textFieldResult.setText("0"); this.buttonMC = new JButton("MC"); this.buttonMR = new JButton("MR"); this.buttonMPlus = new JButton("M+"); this.buttonMDec = new JButton("M-"); this.buttonMs = new JButton("MS"); this.buttonM = new JButton("M`"); this.buttonCE = new JButton("CE"); this.buttonC = new JButton("C"); this.buttonClear = new JButton("Clear"); this.buttonDIV = new JButton("/"); this.button7 = new JButton("7"); this.button8 = new JButton("8"); this.button9 = new JButton("9"); this.buttonMUL = new JButton("*"); this.button4 = new JButton("4"); this.button5 = new JButton("5"); this.button6 = new JButton("6"); this.buttonDec = new JButton("-"); this.button1 = new JButton("1"); this.button2 = new JButton("2"); this.button3 = new JButton("3"); this.buttonMinus = new JButton("+_"); this.button0 = new JButton("0"); this.buttonPoint = new JButton("."); this.buttonEqual = new JButton("="); this.buttonAdd = new JButton("+"); this.panelM = new JPanel(); this.panelParam = new JPanel(); this.panelResult = new JPanel(); } private void initLayout() { this.setSize(420, 500); this.setLocationRelativeTo(null); this.getContentPane().setLayout(new GridBagLayout()); this.setTitle("计算器"); this.getContentPane().add(panelResult, new GridBagConstraintsHelper(0, 0, 1, 1).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.getContentPane().add(panelM, new GridBagConstraintsHelper(0, 1, 1, 1).setWeight(1, 0).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.getContentPane().add(panelParam, new GridBagConstraintsHelper(0, 3, 1, 1).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelResult.setLayout(new GridBagLayout()); this.panelResult.add(this.textFieldResult, new GridBagConstraintsHelper(0, 0, 1, 1).setInsets(5, 1, 0, 1).setIpad(0, 30).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelM.setLayout(new GridBagLayout()); this.panelM.add(this.buttonMC, new GridBagConstraintsHelper(0, 0, 1, 1).setInsets(1).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelM.add(this.buttonMR, new GridBagConstraintsHelper(1, 0, 1, 1).setInsets(1).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelM.add(this.buttonMPlus, new GridBagConstraintsHelper(2, 0, 1, 1).setInsets(1).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelM.add(this.buttonMDec, new GridBagConstraintsHelper(3, 0, 1, 1).setInsets(1).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelM.add(this.buttonMs, new GridBagConstraintsHelper(4, 0, 1, 1).setInsets(1).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelM.add(this.buttonM, new GridBagConstraintsHelper(5, 0, 1, 1).setInsets(1).setWeight(1, 1).setAnchor(GridBagConstraints.CENTER).setFill(GridBagConstraints.BOTH)); this.panelParam.setLayout(new GridBagLayout()); this.panelParam.add(this.buttonCE, new GridBagConstraintsHelper(0, 0, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonC, new GridBagConstraintsHelper(1, 0, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonClear, new GridBagConstraintsHelper(2, 0, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonDIV, new GridBagConstraintsHelper(3, 0, 1, 1).setInsets(1).setIpad(5, 20).setWeight(2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button7, new GridBagConstraintsHelper(0, 1, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button8, new GridBagConstraintsHelper(1, 1, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button9, new GridBagConstraintsHelper(2, 1, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonMUL, new GridBagConstraintsHelper(3, 1, 1, 1).setInsets(1).setIpad(5, 20).setWeight(2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button4, new GridBagConstraintsHelper(0, 2, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button5, new GridBagConstraintsHelper(1, 2, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button6, new GridBagConstraintsHelper(2, 2, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonDec, new GridBagConstraintsHelper(3, 2, 1, 1).setInsets(1).setIpad(5, 20).setWeight(2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button1, new GridBagConstraintsHelper(0, 3, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button2, new GridBagConstraintsHelper(1, 3, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button3, new GridBagConstraintsHelper(2, 3, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonAdd, new GridBagConstraintsHelper(3, 3, 1, 1).setInsets(1).setIpad(5, 20).setWeight(2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonMinus, new GridBagConstraintsHelper(0, 4, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.button0, new GridBagConstraintsHelper(1, 4, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonPoint, new GridBagConstraintsHelper(2, 4, 1, 1).setInsets(1).setIpad(5, 20).setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); this.panelParam.add(this.buttonEqual, new GridBagConstraintsHelper(3, 4, 1, 1).setInsets(1).setIpad(5, 20).setWeight(2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH)); }