java实验32.docx

上传人:b****3 文档编号:27094890 上传时间:2023-06-27 格式:DOCX 页数:24 大小:218.44KB
下载 相关 举报
java实验32.docx_第1页
第1页 / 共24页
java实验32.docx_第2页
第2页 / 共24页
java实验32.docx_第3页
第3页 / 共24页
java实验32.docx_第4页
第4页 / 共24页
java实验32.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

java实验32.docx

《java实验32.docx》由会员分享,可在线阅读,更多相关《java实验32.docx(24页珍藏版)》请在冰豆网上搜索。

java实验32.docx

java实验32

实验项目名称:

图形用户界面实验学时:

6

同组学生姓名:

————实验地点:

1514/A203

实验日期:

2016、5、4/5、6/5、11实验成绩:

批改教师:

王倩倩批改时间:

 

一、实验目的与要求

(1)掌握Swing组件的使用方法;

(2)熟练掌握Swing中常用布局管理器的使用方法;

(3)掌握用户界面动作与事件的处理程序的编写方法;

(4)熟练掌握构造用户界面的方法与常见界面元素的使用;

(5)熟练掌握Java绘图的主要方法。

(6)调试程序要记录调试过程中出现的问题及解决办法;

(7)编写程序要规范、正确,上机调试过程与结果要有记录,不断积累编程及调试经验;

(8)做完实验后给出本实验的实验报告。

二、实验仪器与设备

奔腾以上计算机,Windows操作系统,装有JDK1、7与Eclipse软件。

三、实验过程

1、计算器设计

2、模拟裁判评分。

设计如图所示图形界面,显示n个裁判的评分,根据制定规则计算出最后得分。

要求:

图形界面采用表格显示裁判评分,随裁判人数变化而变化;指定分数范围,若超出,则异常处理;得分规则有指定接口约定,由多个接口对象给出多种得分规则,如求平均数值,或去掉一个最高分与一个最低分后,再求平均值。

 

3、编译运行下例,然后修改程序,当使用鼠标单击后在另一位置重新绘制月亮。

【例】在Applet中画月亮。

importjava、awt、*;

importjava、applet、Applet;

publicclassMoonAppletextendsApplet

{

publicvoidpaint(Graphicsg)//在Applet上绘图

{

g、setColor(Color、red);

g、drawString("TheMoon",100,20);

intx=0,y=0;//圆外切矩形左上角坐标

x=this、getWidth()/4;

y=this、getHeight()/4;

intdiameter=Math、min(this、getWidth()/2,this、getHeight()/2);//圆的直径

g、setColor(Color、yellow);

g、fillOval(x,y,diameter,diameter);//画圆

g、setColor(this、getBackground());//设置为背景色

g、fillOval(x-20,y-20,diameter,diameter);//画圆

}

}

4、根据阿基米德螺线的极坐标方程:

r=aθ画出相应图形。

要求:

(1)注意选用适当的布局管理器设计图形用户界面,比较它们的布局情况;

(2)养成良好的编程习惯,严格按照命名规则为包、类及类成员命名,将每个程序打包,包的命名方式如three、num1表示实验三的第一题;

(3)学会使用Eclipse的各种调试方法;

(4)学会查阅JavaAPI文档,如查找事件类的处理里方法。

程序清单:

(建议程序中适当添加注释信息,增强可读性;较长程序可分栏书写,保证报告排版整洁美观。

第1题实验代码:

packageshiyan3_1;

importjava、awt、*;

importjava、awt、event、*;

importjavax、swing、*;

importjavax、swing、event、*;

publicclasscalculatorframeextendsJFrameimplementsActionListener

{

privateJTextFieldtext;

privateJButtonbuttons[];

publiccalculatorframe()

{

super("Calculator");

this、setSize(500,300);

this、setLocation(300,240);

this、setDefaultCloseOperation(3);//EXIT_ON_CLOSE

text=newJTextField("",20);

text、setHorizontalAlignment(JTextField、RIGHT);

text、setEditable(false);

this、getContentPane()、add(text,"North");

JPanelpanel=newJPanel(newGridLayout(5,4));

this、getContentPane()、add(panel,"Center");

Stringstr[]={"sqrt","+/-","Backspace","C","7","8","9","/","4","5","6","*","1","2","3","-","0","、","=","+"};

this、buttons=newJButton[str、length];

for(inti=0;i

{

buttons[i]=newJButton(str[i]);

panel、add(buttons[i]);

buttons[i]、addActionListener(this);

}

this、setVisible(true);

}

publicvoidactionPerformed(ActionEventev)

{

Stringcurrent=ev、getActionCommand();

if(current、equals("C"))

{

text、setText("0");

}

else

if(current、equals("Backspace"))

{

Stringstr=text、getText()、substring(0,text、getText()、length()-1);

text、setText(str);

}

else

{

if(text、getText()、equals("0"))//字符串比较要用equals

{

text、setText("");

}

if(!

current、equals("="))

{

if(current、equals("sqrt"))

{

Stringstrtemp=text、getText();

doublevalue=Double、parseDouble(strtemp);

text、setText(""+Math、sqrt(value));

}

else

{

text、setText(text、getText()+current);

}

}

else

{

Stringstrtemp=text、getText();

Stringstr1,str2;

inti=0;

charop;

doublevalue=0;

/*while(strtemp、charAt(i)!

='+'&&strtemp、charAt(i)!

='-'&&strtemp、charAt(i)!

='*'&&strtemp、charAt(i)!

='/')

{

System、out、println(strtemp、charAt(i));

}

*/

for(i=0;i

{

if(strtemp、charAt(i)=='+'||strtemp、charAt(i)=='-'||strtemp、charAt(i)=='*'||strtemp、charAt(i)=='/')

{

break;

}

}

str1=strtemp、substring(0,i);

op=strtemp、charAt(i);

str2=strtemp、substring(i+1);

if(op=='+')

{

value=Double、parseDouble(str1)+Double、parseDouble(str2);

}

if(op=='-')

{

value=Double、parseDouble(str1)-Double、parseDouble(str2);

}

if(op=='*')

{

value=Double、parseDouble(str1)*Double、parseDouble(str2);

}

if(op=='/')

{

value=Double、parseDouble(str1)/Double、parseDouble(str2);

}

text、setText(""+value);

}

}

}

publicstaticvoidmain(Stringargs[])

{

newcalculatorframe();

}

}

第2题实验代码:

packageshiyan3_2;

importjava、io、*;

importjava、awt、*;

importjava、awt、event、*;

importjavax、swing、*;

importjavax、swing、event、*;

importjavax、swing、table、DefaultTableModel;

importshiyan2_6、MyException;

publicclassanalogscoreextendsJFrameimplementsActionListener,scoreprinciple

{

privateJTextFieldtext;

privateJButtonbutton;

privateDefaultTableModeltablemodel;

privatedoublea[];

publicanalogscore(doublea[])

{

super("模拟裁判评分");

this、setBounds(300,240,780,400);

this、setDefaultCloseOperation(EXIT_ON_CLOSE);

this、a=a;

introw;

intrest;

if(a、length%5!

=0)

{

if(a、length<5)

{

this、tablemodel=newDefaultTableModel(1,a、length);

intt=0,i;

for(i=0;i

{

this、tablemodel、setValueAt(a[t],0,i);

t++;

}

}

else

{

System、out、println("就是整行");

row=a、length/5+1;

rest=a、length%5;

System、out、println(""+row);

System、out、println(""+rest);

this、tablemodel=newDefaultTableModel(row,5);

intt=0,i,j;

for(i=0;i

{for(j=0;j<5;j++)

{

this、tablemodel、setValueAt(a[t],i,j);

t++;

}

}

for(j=0;j

{

this、tablemodel、setValueAt(a[t],row-1,j);

t++;

}

}

}

else

{

System、out、println("不就是整行");

row=a、length/5;

this、tablemodel=newDefaultTableModel(row,5);

intt=0,i,j;

for(i=0;i

{for(j=0;j<5;j++)

{

this、tablemodel、setValueAt(a[t],i,j);

t++;

}

}

}

JTablejtable=newJTable(this、tablemodel);

this、getContentPane()、add(jtable,"North");

JPanelpanel=newJPanel(newFlowLayout(FlowLayout、RIGHT));

this、getContentPane()、add(panel);

button=newJButton("平均分");

panel、add(button);

button、addActionListener(this);

text=newJTextField("",8);

text、setEditable(false);

panel、add(text);

this、setVisible(true);

}

publicdoublesp(doublea[])

{

doublesum=0;

if(a!

=null&&a、length>2)

{

doublemax=a[0],min=a[0];

for(inti=0;i

{

sum+=a[i];

if(a[i]>max)

{

max=a[i];

}

if(a[i]

{

min=a[i];

}

}

return(sum-max-min)/(a、length-2);

}

return0、0;

}

publicvoidactionPerformed(ActionEventev)

{

doublet=this、sp(a);

this、text、setText(""+t);

}

publicstaticvoidmain(Stringarg[])

{

try

{

BufferedReaderstrin=newBufferedReader(newInputStreamReader(System、in));//建立输入流缓冲区

System、out、println("请输入裁判人数:

");

Stringcl=strin、readLine();

intn=Integer、parseInt(cl);

System、out、println("请以此输入裁判的分数:

");

String[]strArray=newString[n];

for(inti=0;i

{

strArray[i]=strin、readLine();

if(Double、parseDouble(strArray[i])<0||Double、parseDouble(strArray[i])>10)thrownewMyException();

}

doublea[]=newdouble[n];

for(inti=0;i

{

a[i]=Double、parseDouble(strArray[i]);

System、out、println(""+a[i]);

//if(a[i]<0||a[i]>10)thrownewMyException();

}

newanalogscore(a);

}

catch(IOExceptionex)

{

System、out、println("I/O有关的异常");

}

catch(NumberFormatExceptionex)

{

System、out、println("字符串不能转换成整数");

}

catch(MyExceptionex)

{

System、out、println("输入评分超出范围");

}

}

}

第3题实验代码:

packageshiyan3_3_1;

importjava、awt、*;

importjava、awt、event、*;

importjavax、swing、*;

importjavax、swing、event、*;

publicclassmooncanvasextendsCanvasimplementsMouseListener

{

privateColorcolor;

intx=this、getWidth()/2;

inty=this、getHeight()/2;

publicmooncanvas()

{

this、addMouseListener(this);

}

publicmooncanvas(Colorcolor)

{

this、setColor(color);

}

publicvoidsetColor(Colorcolor)

{

this、color=color;

}

publicvoidpaint(Graphicsg)

{

g、setColor(Color、red);

g、drawString("TheMoon",300,20);//圆外切矩形左上角坐标

intdiameter=Math、min(this、getWidth()/2,this、getHeight()/2);//圆的直径

g、setColor(Color、yellow);

g、fillOval(x,y,diameter,diameter);//画圆

g、setColor(this、getBackground());//设置为背景色

g、fillOval(x-20,y-20,diameter,diameter);//画圆

}

publicvoidmousePressed(MouseEvente)

{

x=e、getX();

y=e、getY();

repaint();

}

publicvoidmouseReleased(MouseEvente){}

publicvoidmouseEntered(MouseEvente){}

publicvoidmouseExited(MouseEvente){}

publicvoidmouseClicked(MouseEvente)

{

x=e、getX();

y=e、getY();

repaint();

}

/*publicvoidupdate(Graphicsg)

{

paint(g);

super、update(g);

}

*/

}

packageshiyan3_3_1;

importjava、awt、*;

importjava、awt、event、*;

importjavax、swing、*;

importjavax、swing、event、*;

publicclassMoonJframeextendsJFrame

{

privatemooncanvascanvas;

publicMoonJframe()

{

super("月亮");

Dimensiondim=this、getToolkit()、getScreenSize();this、setBounds(dim、width/4,dim、height/4,dim、width/2,dim、height/2);

this、setDefaultCloseOperation(EXIT_ON_CLOSE);

this、canvas=newmooncanvas();this、getContentPane()、add(this、canvas,"Center");

this、setVisible(true)

}

publicstaticvoidmain(Stringargs[])

{

newMoonJframe();

}

}

第4题实验代码:

packageshiyan3_4;

importjava、awt、*;

importjava、awt、event、*;

importjavax、swing、*;

publicclassarchimedeslinejframeextendsJFrameimplementsActionListener

{

privateArchimedesCanvasarchimedes;

privateJButtonbutton_color;

publicarchimedeslinejframe()

{

super("阿基米德螺线");

Dimensiondim=getToolkit()、getScreenSize();

this、setBounds(dim、width/4,dim、height/4,dim、width/2,dim、height/2);

this、setDefaultCloseOperation(EXIT_ON_CLOSE);

button_color=newJButton("选择颜色");

this、getContentPane()、a

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1