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)); }