今天学习了Java的GUI图形界面,并实现了如下图所示的复杂计算器界面的设计,在设计算器界面的整个过程中遇到了不少小问题,值得注意和总结。
由于计算器界面较复杂,按键较多,因此需要用到面板的嵌套,但是面板为二级容器,不能单独出现,必须依赖于窗口。
其中代码如下:
public class CalTest extends JFrame {
public CalTest(){
this.setSize(820,680);
this.setTitle("计算器");
this.setLayout(null);
initPanel1();
initPanel2();
initPanel3();
initPanel5();
initPrintText();
initHelp();
this.setVisible(true);
}
//面板1,25个按键
private void initPanel1() {
JPanel Panel1 = new JPanel();
Panel1.setSize(350, 350);
Panel1.setLocation(50, 230);
Panel1.setLayout(new GridLayout(5,5,3,3));
Panel1.setBackground(Color.WHITE);
JButton[] btsl = new JButton[25];
String[] strs = {" ", "Inv", "In", "(", ")", "Int", "sinh", "sin", "x^2", "n!", "dms", "cosh", "cos", "x^y", "y|x", "3.14", "tanh", "tan", "x^3", "3|x", "F-E","Exp","Mod","log","10^x"};
for (int i = 0; i < strs.length; i++) {
btsl[i] = new JButton(strs[i]);
Panel1.add(btsl[i]);
}
btsl[0].setEnabled(false);
this.add(Panel1);
}
//面板2,20个按键
private void initPanel2(){
JPanel panel2 = new JPanel();
panel2.setSize(350,280);
panel2.setLocation(402,160);
panel2.setLayout(new GridLayout(4,5,3,3));
panel2.setBackgroun/d(Color.WHITE);
JButton[] btsl2 = new JButton[20];
String[] strs2 = {"MC","MR","M5","M+","M-","<——","CE","C","+-","根号","7","8","9","/","%","4","5","6","*","1/x"};
for (int i = 0; i < strs2.length ; i++) {
btsl2[i] = new JButton(strs2[i]);
panel2.add(btsl2[i]);
}
this.add(panel2);
}
//对特殊位置的按键进行处理
private void initPanel3(){
initPanel4();
JButton button1 = new JButton("1");
button1.setSize(67,67);
button1.setLocation(402,440);
this.add(button1);
JButton button2 = new JButton("2");
button2.setSize(67,67);
button2.setLocation(472,440);
this.add(button2);
JButton button3 = new JButton("0");
button3.setSize(137,67);
button3.setLocation(402,510);
this.add(button3);
JButton button4 = new JButton("=");
button4.setSize(67,137);
button4.setLocation(684,441);
this.add(button4);
}
//面板4,4个按键
private void initPanel4(){
JPanel Panel4 = new JPanel();
Panel4.setSize(140,140);
Panel4.setLocation(542,440);
Panel4.setLayout(new GridLayout(2,2,3,3));
Panel4.setBackground(Color.WHITE);
JButton[] btsl4 = new JButton[4];
String[] strs4 = {"3","——",".","+"};
for (int i = 0; i < strs4.length ; i++) {
btsl4[i] = new JButton(strs4[i]);
Panel4.add(btsl4[i]);
}
this.add(Panel4);
}
//面板5,3个按键
private void initPanel5(){
JPanel panel5 = new JPanel();
panel5.setSize(347,67);
panel5.setLocation(50,160);
panel5.setLayout(new GridLayout(1,3,10,10));
JButton[] btls5 = new JButton[3];
String[] strs = {"度","弧度","梯度"};
for (int i = 0; i < strs.length ; i++) {
btls5[i] = new JButton(strs[i]);
panel5.add(btls5[i]);
}
this.add(panel5);
}
//输出框
private void initPrintText(){
JTextField printText = new JTextField();
printText.setSize(702,100);
printText.setLocation(50,50);
this.add(printText);
}
//顶部的帮助按键
private void initHelp(){
JPanel panel6 = new JPanel();
panel6.setSize(400,40);
panel6.setLocation(50,0);
panel6.setLayout(new GridLayout(1,3,10,10));
JButton[] btls5 = new JButton[3];
String[] strs = {"查看(v)","编辑(E)","帮助(H)"};
for (int i = 0; i < strs.length ; i++) {
btls5[i] = new JButton(strs[i]);
panel6.add(btls5[i]);
}
this.add(panel6);
}
}
在主方法中调用就可以运行成功
在编写代码中遇到了一些小问题需要注意
1.布局时坐标是按照此控件左上角到窗口的距离来确定x,y坐标的。
2.大窗口的尺寸要足够大,控件的尺寸大小和坐标中和不能超过大窗口的尺寸,否则运行时窗口不能显示完全。
3.在使用对象设置相应参数时要注意不能用this来代替当前对象,因为此时this指向调用当前方法的对象,不能搞混淆。
例如: panel6.setSize(400,40);就不能写成 this.setSize(400,40);
4.按钮一定要add到相应面板上,面板要add到窗口上
5.设置可见:this.setVisible(true);否则窗口不会显示相应控件