实验12图形用户界面.docx
《实验12图形用户界面.docx》由会员分享,可在线阅读,更多相关《实验12图形用户界面.docx(15页珍藏版)》请在冰点文库上搜索。
实验12图形用户界面
实验12图形用户界面
一、实验目的
1.掌握GUI开发的基本过程
2.能够根据要求,选择合适的布局管理器和相应组件完成页面的布局
3.理解事件响应机制,掌握事件编程
二、实验题目
1、完成课本171-172页的选择题和填空题
2、完成173页编程题的第4题
3、下面是一个计算器实现的代码,有兴趣的同学可以进行阅读和分析
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.text.DecimalFormat;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclasscalimplementsActionListener{//导入动作监听接口
//设计面板中的单位
JFrameframe;
JTextFieldtextAnswer;
JPanelpanel1,panel2,panel3;
JLabellabelMemSpace;
JButtonbuttonBk,buttonCe,buttonC;
JButtonbutton[];
JButtonbuttonDot,buttonAddAndSub,buttonAdd,buttonSub,buttonMul,
buttonDiv,buttonMod;
JButtonbuttonSqrt,buttonDao,buttonEqual;
DecimalFormatdf;//设置数据输出精度
booleanclickable;//控制当前能否按键
doublevard,answerd;//用来保存double型数据的中间值(vard)和最后结果(answerd)
shortkey=-1,prekey=-1;//key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
Stringcopy;//做复制用
JTextAreahelp;//帮助
//构造函数
publiccal(){
clickable=true;
answerd=0;
frame=newJFrame("计算器");
df=newDecimalFormat("0.##############");//设置数据输出精度(对于double型值)
textAnswer=newJTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(newColor(255,255,255));
panel1=newJPanel();
panel2=newJPanel();
frame.getContentPane().add(panel1);
panel1.setLayout(newBorderLayout());
//设计整个面板
panel1.add(textAnswer,BorderLayout.NORTH);
labelMemSpace=newJLabel("");
buttonBk=newJButton("Backspace");
buttonBk.setForeground(newColor(255,0,0));
buttonCe=newJButton("CE");
buttonCe.setForeground(newColor(255,0,0));
buttonC=newJButton("C");
buttonC.setForeground(newColor(255,0,0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2,BorderLayout.CENTER);
panel2.setLayout(newFlowLayout(FlowLayout.RIGHT));
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonCe);
panel2.add(buttonC);
panel3=newJPanel();
panel1.add(panel3,BorderLayout.SOUTH);
button=newJButton[10];
for(inti=0;ibutton[i]=newJButton(Integer.toString(i));
button[i].setForeground(newColor(0,0,255));
}
buttonDot=newJButton(".");
buttonDot.setForeground(newColor(0,0,255));
buttonAddAndSub=newJButton("+/-");
buttonAddAndSub.setForeground(newColor(0,0,255));
buttonAdd=newJButton("+");
buttonAdd.setForeground(newColor(255,0,0));
buttonSub=newJButton("-");
buttonSub.setForeground(newColor(255,0,0));
buttonMul=newJButton("*");
buttonMul.setForeground(newColor(255,0,0));
buttonDiv=newJButton("/");
buttonDiv.setForeground(newColor(255,0,0));
buttonMod=newJButton("%");
buttonMod.setForeground(newColor(0,0,255));
buttonSqrt=newJButton("sqrt");
buttonSqrt.setForeground(newColor(0,0,255));
buttonDao=newJButton("1/x");
buttonDao.setForeground(newColor(0,0,255));
buttonEqual=newJButton("=");
buttonEqual.setForeground(newColor(255,0,0));
//将所有行为与监听绑定
panel3.setLayout(newGridLayout(4,5));
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel3.add(buttonSqrt);
buttonSqrt.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel3.add(buttonMod);
buttonMod.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(buttonDao);
buttonDao.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonAddAndSub);
buttonAddAndSub.addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//设置各个按钮行为
publicvoidactionPerformed(ActionEventevent){
Objecttemp=event.getSource();
//如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后
for(inti=0;i<=9;i++)
if(temp==button[i]&&clickable==true)
textAnswer.setText(textAnswer.getText()+Integer.toString(i));
//按下"."按钮时,判断当前文本框内字符串中含不含".",如果已含,则不允许再插入"."
if(temp==buttonDot&&clickable==true){
booleanisDot=false;
if(textAnswer.getText().length()==0)
isDot=true;
for(inti=0;iif('.'==textAnswer.getText().charAt(i)){
isDot=true;
break;
}
if(isDot==false)
textAnswer.setText(textAnswer.getText()+".");
}
if((temp==buttonAdd||temp==buttonSub||temp==buttonMul||temp==buttonDiv)
&&clickable==true){
//+
if(temp==buttonAdd){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
case1:
answerd-=Double.parseDouble(textAnswer.getText());
case2:
answerd*=Double.parseDouble(textAnswer.getText());
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不能为零");
clickable=false;
}else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=0;
}
//-
if(temp==buttonSub){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
case1:
answerd-=Double.parseDouble(textAnswer.getText());
case2:
answerd*=Double.parseDouble(textAnswer.getText());
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不能为零");
clickable=false;
}else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=1;
}
//*
if(temp==buttonMul){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
case1:
answerd-=Double.parseDouble(textAnswer.getText());
case2:
answerd*=Double.parseDouble(textAnswer.getText());
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不能为零");
clickable=false;
}else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=2;
}
///
if(temp==buttonDiv){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
case1:
answerd-=Double.parseDouble(textAnswer.getText());
case2:
answerd*=Double.parseDouble(textAnswer.getText());
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不能为零");
clickable=false;
}else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=3;
}
}
//=
if(temp==buttonEqual&&clickable==true){
if(prekey==5){
if(key==0){
answerd+=vard;
textAnswer.setText(df.format(answerd));
}
if(key==1){
answerd-=vard;
textAnswer.setText(df.format(answerd));
}
if(key==2){
answerd*=vard;
textAnswer.setText(df.format(answerd));
}
if(key==3){
answerd/=vard;
textAnswer.setText(df.format(answerd));
}
}else{
vard=Double.parseDouble(textAnswer.getText());
if(key==0){
prekey=-1;
answerd+=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if(key==1){
prekey=-1;
answerd-=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if(key==2){
prekey=-1;
answerd*=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if(key==3){
prekey=-1;
answerd/=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
}
prekey=5;
}
//取余
if(temp==buttonMod&&clickable==true){
if(answerd==0){
Strings=textAnswer.getText();
textAnswer.setText(s);
}else{
}
}
//开根号
if(temp==buttonSqrt&&clickable==true){
Strings=textAnswer.getText();
if(s.charAt(0)=='-'){
textAnswer.setText("不能开根");
clickable=false;
}else{
textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double
.parseDouble(textAnswer.getText()))));
}
}
//求倒数
if(temp==buttonDao&&clickable==true){
Strings=textAnswer