java实验报告Word下载.docx
- 文档编号:18657085
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:78
- 大小:212.67KB
java实验报告Word下载.docx
《java实验报告Word下载.docx》由会员分享,可在线阅读,更多相关《java实验报告Word下载.docx(78页珍藏版)》请在冰豆网上搜索。
23.this.setLayout(newGridLayout(4,2));
//流体布局:
4行2列
24.
25.this.add(newLabel("
整数"
0));
26.text_num=newTextField();
27.this.add(text_num);
28.text_num.addActionListener(this);
//注册文本编辑事件监视器
29.
30.this.add(newLabel("
百位"
31.text_hun=newTextField("
"
32.text_hun.setEditable(false);
//只显示,不允许编辑
33.this.add(text_hun);
34.
35.this.add(newLabel("
十位"
36.text_dec=newTextField("
37.text_dec.setEditable(false);
38.this.add(text_dec);
39.
40.this.add(newLabel("
个位"
41.text_uni=newTextField("
42.text_uni.setEditable(false);
43.this.add(text_uni);
44.
45.this.setVisible(true);
46.dialog=newMessageDialog(this);
47.}
48.
49.
50.privateclassMessageDialogextendsJDialog
51.{
52./**
53.*
54.*/
55.privatestaticfinallongserialVersionUID=1L;
56.Frameframe;
//对话框所依赖的框架窗口
57.Labellabel;
//对话框中显示信息
58.MessageDialog(Frameframe)
59.{
60.super(frame,"
消息"
true);
61.this.frame=frame;
62.this.setSize(300,80);
63.label=newLabel("
Label.CENTER);
64.this.add(label);
65.this.setDefaultCloseOperation(HIDE_ON_CLOSE);
66.}
67.publicvoidshow(Stringstring)
68.{
69.label.setText(string);
70.this.setLocation(frame.getX()+100,frame.getY()+100);
71.this.setVisible(true);
72.}
73.
74.}
75.
76.publicvoidactionPerformed(ActionEvente)
77.{
78.try
79.{
80.intx=(int)Double.parseDouble(text_num.getText());
81.text_uni.setText(uni(x));
82.text_dec.setText(dec(x));
83.text_hun.setText(hun(x));
84.}
85.catch(NumberFormatExceptionnfe)
86.{
87.dialog.show("
\"
+text_num.getText()+"
不能转换为整数,请重新输入"
88.}
89.finally{}
90.}
91.
92.publicstaticStringuni(intx)
93.{
94.Stringresult="
+x%10;
95.returnresult;
96.}
97.publicstaticStringhun(intx)
98.{
99.inty=(x%1000-x%100)/100;
100.Stringresult="
+y;
101.returnresult;
102.}
103.publicstaticStringdec(intx)
104.{
105.inty=(x%100-x%10)/10;
106.Stringresult="
107.returnresult;
108.}
109.
110.publicstaticvoidmain(Stringarg[])
111.{
112.newex_1();
113.}
114.}
2、模拟实现一个可视化的简单计算器,至少提供进行加法、减法、乘法、除法等基本运算的功能,希望能支持加正负号、求平方根、清零等其他功能。
5.publicclasscalculatorextendsJFrameimplementsActionListener{
6.
7.privatestaticfinallongserialVersionUID=-169068472193786457L;
8.
9.privateclassWindowCloserextendsWindowAdapter{
10.publicvoidwindowClosing(WindowEventwe){
11.System.exit(0);
12.}
13.}
15.
16.privatefinalString[]str={"
sqrt"
"
+/-"
CE"
C"
7"
"
8"
9"
/"
4"
5"
6"
*"
1"
2"
3"
-"
."
0"
="
+"
};
17.JButton[]buttons=newJButton[str.length];
18.JTextFielddisplay=newJTextField("
19.doublenumber=0.0;
20.Stringoperator="
;
21.booleanisFirstDigit=true;
22.
23.publiccalculator(){
24.super("
Calculator"
25.this.setSize(800,600);
26.this.setLocation(300,240);
27.
28.JPanelpanel1=newJPanel(newGridLayout(5,4));
//面板1网格布局
29.for(inti=0;
i<
str.length;
i++){//加入数字符号键
30.buttons[i]=newJButton(str[i]);
31.panel1.add(buttons[i]);
32.}
33.
34.JPanelpanel2=newJPanel(newBorderLayout());
//面板2边界布局
35.panel2.add("
Center"
display);
//加入显示框
36.
37.getContentPane().setLayout(newBorderLayout());
38.getContentPane().add("
North"
panel2);
39.getContentPane().add("
panel1);
40.for(inti=0;
i++)
41.buttons[i].addActionListener(this);
42.display.addActionListener(this);
43.addWindowListener(newWindowCloser());
44.setVisible(true);
45.pack();
46.}
47.publicvoidactionPerformed(ActionEvente){
48.Stringlabel=e.getActionCommand();
//调取按钮上字符串
49.if(label=="
||label=="
)
50.handleReset();
//清零
51.elseif("
0123456789."
.indexOf(label)>
=0)
52.handleNumber(label);
//数字的输入
53.elseif(label=="
||label=="
54.handleMonocular(label);
//单目运算
55.else
56.handleOperator(label);
//双目运算+-*/
57.}
58.
59.privatevoidhandleMonocular(Stringkey){
60.if(key.equals("
))
61.number=-Double.valueOf(display.getText());
62.elseif(key.equals("
63.number=Math.sqrt(Double.valueOf(display.getText()));
64.display.setText(String.valueOf(number));
65.}
66.
67.publicvoidhandleNumber(Stringkey){
68.if(isFirstDigit)
69.display.setText(key);
70.elseif((key.equals("
))&
&
(display.getText().indexOf("
)<
0))
71.display.setText(display.getText()+"
72.elseif(!
key.equals("
73.display.setText(display.getText()+key);
74.isFirstDigit=false;
75.}
76.
77.publicvoidhandleReset(){
78.display.setText("
79.isFirstDigit=true;
80.operator="
81.}
82.
83.publicvoidhandleOperator(Stringkey){
84.if(operator.equals("
85.number-=Double.valueOf(display.getText());
86.elseif(operator.equals("
87.number*=Double.valueOf(display.getText());
88.elseif(operator.equals("
89.number/=Double.valueOf(display.getText());
90.elseif(operator.equals("
91.number+=Double.valueOf(display.getText());
92.elseif(operator.equals("
93.number=Double.valueOf(display.getText());
94.display.setText(String.valueOf(number));
95.operator=key;
96.isFirstDigit=true;
97.}
98.
99.publicstaticvoidmain(String[]args)
100.{
101.newcalculator();
103.}
3、设计图形页面实现学生的信息录入,至少包括姓名、年龄、出生年月日、java课程实验成绩,成绩使用浮点数,年龄使用整型,使用数据存储输入对象,程序输出按年龄排序的学生信息。
1.publicclassstudent{
2.Stringname;
3.intage;
4.doublescore;
5.Stringbirth;
6.publicstudent(Stringname,intage,doublescore,Stringbirth){
7.this.set(name,age,score,birth);
8.}
9.publicvoidset(Stringname,intage,doublescore,Stringbirth){
10.this.name=name;
11.this.age=age;
12.this.score=score;
13.this.birth=birth;
14.}
15.publicStringgetname(){
16.returnname;
17.}
18.publicintgetage(){
19.returnage;
20.}
21.publicdoublegetscore(){
22.returnscore;
23.}
24.publicStringgetbirth(){
25.returnbirth;
26.}
27.publicStringtoString(){
28.returnthis.getname()+"
\t"
+this.getage()+"
+this.getbirth()+"
+this.getscore()+"
\n"
29.}
30.
31./*publicstaticvoidmain(Stringarg[]){
32.studenta[]=newstudent[100];
33.a[0]=newstudent("
1,12,"
123"
34.a[1]=newstudent("
35.a[2]=newstudent("
36.System.out.print(a[0].toString());
37.}*/
38.}
3.
5.publicclassstudentmessageextendsFrameimplementsActionListener{
6./**
7.*
8.*/
9.privatestaticfinallongserialVersionUID=1L;
10.privatefinalString[]st={"
Name"
Age"
Birth"
Score"
};
11.staticintii=0;
12.studenta[]=newstudent[100];
13.TextFieldt_name,t_age,t_birth,t_score;
14.MDialogdia;
15.
16.publicstudentmessage(){
学生信息"
18.this.setSize(400,400);
20.
21.Panelpanel1=newPanel(newGridLayout(4,1));
22.for(inti=0;
st.length;
i++)
23.panel1.add(newLabel(st[i],0));
25.Panelpanel2=newPanel(newGridLayout(4,1));
26.t_name=newTextField("
20);
27.t_age=newTextField("
28.t_birth=newTextField("
29.t_score=newTextField("
30.panel2.add(t_name);
31.panel2.add(t_age);
32.panel2.add(t_birth);
33.panel2.add(t_score);
35.Panelpanel3=newPanel(newFlowLayout());
36.Buttonb_save=newButton("
Save"
37.Buttonb_check=newButton("
Check"
38.panel3.add(b_save);
39.panel3.add(b_check);
40.
41.this.setLayout(newBorderLayout());
42.this.add("
West"
43.this.add("
East"
44.this.add("
South"
panel3);
45.
46.addWindowListener(newWindowCloser());
47.b_save.addActionListener(this);
48.b_check.addActionListener(this);
49.this.setVisible(true);
50.dia=newMDialog(this);
51.pack();
52.}
53.
54.privateclassWindowCloserextendsWindowAdapter{
55.publicvoidwindowClosing(WindowEventwe){
56.System.exit(0);
57.}
58.}
59.
60.privateclassMDialogextendsDialog{
61.
62.privatestaticfinallongserialVersionUID=1L;
63.Frameframe;
64.TextAreat_show;
65.MDialog(Frameframe){
66.super(frame,"
记录"
67.this.frame=frame;
68.this.setSize(300,80);
69.t_show=newTextArea(20,20);
70.this.add(t_show);
71.this.addWindowListener(newWindowCloser());
72.}
73.publicvoidshow(Strings){
74.t_show.setText(s);
75.this.setLocation(frame.getX()+100,frame.getY()+100);
76.this.setVisible(true);
77.}
78.}
79.
80.publicvoidactionPerformed(ActionEvente){
81.Stringlabel=e.getActionCommand();
82.if(label=="
){
83.Stringname=t_name.getText();
84.intage=In
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 报告