我目前正在学习mvc模式,所以我决定用java和我的计算机创建自己的计算器 View.java
,我有以下代码。
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.awt.Color;
import javax.swing.*;
@SuppressWarnings("serial")
public class View extends JFrame {
private Controller theController;
public JLabel resultLabel = new JLabel("0");
public View () {
}
//public void open (Controller theController) {
// this.theController = theController;
// init();
// setVisible(true);
//}
private void init () {
this.setTitle("Calculator");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
JPanel calculatorPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
calculatorPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
resultLabel.setHorizontalAlignment(SwingConstants.RIGHT);
resultLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
resultLabel.setPreferredSize(new Dimension(0, 25));
gbc.gridwidth = 5;
gbc.gridheight = 1;
gbc.gridx = 0;
gbc.gridy = 0;
// For the fix, changed this constraint to fill BOTH X & Y axis.
// gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.BOTH;
calculatorPanel.add(resultLabel, gbc);
// and then removed this following code:
// gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
String commandsList[] = {"←", "±", "√", "%", "1/x", "CE", "C", "="};
for(String commandName : commandsList) {
JButton command = new JButton(commandName);
// command.addActionListener(theController.actionListener());
switch(commandName) {
case "←":
case "±":
case "√":
case "%":
case "1/x":
gbc.gridy = 1;
break;
case "CE":
gbc.gridy = 2;
gbc.gridx = 4;
break;
case "C":
gbc.gridy = 3;
gbc.gridx = 4;
break;
case "=":
gbc.gridy = 4;
gbc.gridx = 4;
gbc.gridheight = 2;
break;
}
calculatorPanel.add(command, gbc);
gbc.gridx++;
}
// and changed the overlooked `gridheight` back to 1
// so I added this 1 line of code:
gbc.gridheight = 1;
gbc.gridx = 0;
JButton[] Btns = new JButton[10];
for(int i=0; i<10; i++) {
Btns[i] = new JButton(String.valueOf(i));
// Btns[i].addActionListener(theController.actionListener());
if(i==0) {
gbc.gridy = 5;
gbc.gridwidth = 3;
}
if(i>=1 && i<=3) {
gbc.gridy = 4;
if(i==1) gbc.gridx = 0;
if(i==2) gbc.gridx = 1;
if(i==3) gbc.gridx = 2;
}
if(i>=4 && i<=6) {
gbc.gridy = 3;
if(i==4) gbc.gridx = 0;
if(i==5) gbc.gridx = 1;
if(i==6) gbc.gridx = 2;
}
if(i>=7 && i<=9) {
gbc.gridy = 2;
if(i==7) gbc.gridx = 0;
if(i==8) gbc.gridx = 1;
if(i==9) gbc.gridx = 2;
}
calculatorPanel.add(Btns[i], gbc);
gbc.gridwidth = 1;
}
gbc.gridx = 3;
gbc.gridy = 2;
String operatorsList[] = {"+", "-", "*", "/"};
for(String operatorName : operatorsList) {
JButton operator = new JButton(operatorName);
// operator.addActionListener(theController.actionListener());
calculatorPanel.add(operator, gbc);
gbc.gridy++;
}
this.add(calculatorPanel);
this.pack();
}
}
我的问题是,在构建ui时,我无法使用“gridbaglayout”和“gridbagconstraints”使组件正确对齐。
问题是,在我的代码中,组件最终彼此重叠,似乎分散在其容器中。
**我喜欢我知道的循环:)
编辑:我终于能够修复它,重新阅读文档,发现了我遗漏的问题。修正反映在代码和注解上
暂无答案!
目前还没有任何答案,快来回答吧!