




已閱讀5頁,還剩60頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
河北工業(yè)大學(xué) Java程序設(shè)計(jì)試驗(yàn)指導(dǎo)書 Java程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)書計(jì)算機(jī)科學(xué)與軟件學(xué)院 軟 件151 張路 153200 實(shí)驗(yàn)四 圖形用戶界面程序設(shè)計(jì)實(shí)驗(yàn)?zāi)康模赫莆战M件的使用方法,理解委托事件處理模型。熟悉圖形用戶界面基本組件的使用方法,熟悉如何使用布局管理器對(duì)組件進(jìn)行管理及如何使用Java的事件處理機(jī)制。實(shí)驗(yàn)內(nèi)容:1、 輸入一個(gè)整數(shù),分別顯示其百位、十位和個(gè)位數(shù)字,圖形用戶界面如圖4.1所示。要求:整數(shù)文本行可編輯且能實(shí)現(xiàn)事件處理,當(dāng)輸入數(shù)據(jù)錯(cuò)誤時(shí),處理異常,彈出對(duì)話框,提示重新輸入信息;其他文本行僅用于顯示不可編輯。1 import java.awt.*;2 import java.awt.event.*;3 import javax.swing.*;456 public class ex_1 extends JFrame implements ActionListener7 8 /*9 * 10 */11 private static final long serialVersionUID = 1L;12 private MessageDialog dialog; /對(duì)話框 內(nèi)部類對(duì)象13 TextField text_num,text_hun,text_dec,text_uni;14 15 public ex_1()16 17 super(顯示整數(shù)數(shù)字); 18 this.setSize(300,240);19 this.setLocation(300,240);20 this.setBackground(Color.lightGray); /背景顏色:亮灰21 /窗口關(guān)閉按鈕,結(jié)束程序運(yùn)行22 this.setDefaultCloseOperation(3);23 this.setLayout(new GridLayout(4,2); /流體布局:4行2列24 25 this.add(new Label(整數(shù) ,0);26 text_num=new TextField();27 this.add(text_num);28 text_num.addActionListener(this); /注冊(cè)文本編輯事件監(jiān)視器29 30 this.add(new Label(百位 ,0);31 text_hun=new TextField();32 text_hun.setEditable(false); /只顯示,不允許編輯33 this.add(text_hun);34 35 this.add(new Label(十位 ,0);36 text_dec=new TextField();37 text_dec.setEditable(false);38 this.add(text_dec);39 40 this.add(new Label(個(gè)位 ,0);41 text_uni=new TextField();42 text_uni.setEditable(false);43 this.add(text_uni);44 45 this.setVisible(true);46 dialog=new MessageDialog(this);47 48 4950 private class MessageDialog extends JDialog51 52 /*53 * 54 */55 private static final long serialVersionUID = 1L;56 Frame frame; /對(duì)話框所依賴的框架窗口57 Label label; /對(duì)話框中顯示信息58 MessageDialog(Frame frame)59 60 super(frame,消息,true);61 this.frame=frame;62 this.setSize(300, 80);63 label=new Label(,Label.CENTER);64 this.add(label);65 this.setDefaultCloseOperation(HIDE_ON_CLOSE);66 67 public void show(String string) 68 69 label.setText(string);70 this.setLocation(frame.getX()+100, frame.getY()+100);71 this.setVisible(true);72 7374 7576 public void actionPerformed(ActionEvent e)77 78 try79 80 int x=(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(NumberFormatException nfe)86 87 dialog.show(+text_num.getText()+不能轉(zhuǎn)換為整數(shù),請(qǐng)重新輸入);88 89 finally90 91 92 public static String uni(int x)93 94 String result=+x%10;95 return result;96 97 public static String hun(int x)98 99 int y=(x%1000-x%100)/100;100 String result=+y;101 return result;102 103 public static String dec(int x)104 105 int y=(x%100-x%10)/10;106 String result=+y;107 return result;108 109 110 public static void main(String arg)111 112 new ex_1();113 114 2、 模擬實(shí)現(xiàn)一個(gè)可視化的簡單計(jì)算器,至少提供進(jìn)行加法、減法、乘法、除法等基本運(yùn)算的功能,希望能支持加正負(fù)號(hào)、求平方根、清零等其他功能。1 import java.awt.*;2 import java.awt.event.*;3 import javax.swing.*;45 public class calculator extends JFrame implements ActionListener 67 private static final long serialVersionUID = -169068472193786457L;89 private class WindowCloser extends WindowAdapter10 public void windowClosing(WindowEvent we)11 System.exit(0);12 13 14 15 16 private final String str = sqrt,+/-,CE,C,7, 8, 9, /, 4, 5, 6, *, 1,2, 3, -, ., 0, =, + ;17 JButton buttons = new JButton str.length;18 JTextField display = new JTextField(0);19 double number = 0.0;20 String operator = =;21 boolean isFirstDigit = true;22 23 public calculator() 24 super(Calculator);25 this.setSize(800,600);26 this.setLocation(300,240);27 28 JPanel panel1 = new JPanel(new GridLayout(5, 4); /面板1 網(wǎng)格布局29 for (int i = 0; i str.length; i+) /加入數(shù)字 符號(hào)鍵30 buttonsi = new JButton(stri);31 panel1.add(buttonsi);32 33 34 JPanel panel2 = new JPanel(new BorderLayout(); /面板2 邊界布局35 panel2.add(Center, display); /加入顯示框 36 37 getContentPane().setLayout(new BorderLayout();38 getContentPane().add(North, panel2);39 getContentPane().add(Center, panel1);40 for (int i = 0; i = 0) 52 handleNumber(label); /數(shù)字的輸入53 else if(label =sqrt|label=+/-)54 handleMonocular(label); /單目運(yùn)算55 else56 handleOperator(label); /雙目運(yùn)算+-*/57 58 59 private void handleMonocular(String key) 60 if (key.equals(+/-)61 number = -Double.valueOf(display.getText();62 else if (key.equals(sqrt)63 number = Math.sqrt(Double.valueOf(display.getText();64 display.setText(String.valueOf(number);65 6667 public void handleNumber(String key) 68 if (isFirstDigit)69 display.setText(key);70 else if (key.equals(.) & (display.getText().indexOf(.) 0) 71 display.setText(display.getText() + .);72 else if (!key.equals(.)73 display.setText(display.getText() + key); 74 isFirstDigit = false;75 7677 public void handleReset() 78 display.setText(0);79 isFirstDigit = true;80 operator = =;81 8283 public void handleOperator(String key) 84 if (operator.equals(-)85 number -= Double.valueOf(display.getText();86 else if (operator.equals(*)87 number *= Double.valueOf(display.getText();88 else if (operator.equals(/)89 number /= Double.valueOf(display.getText();90 else if (operator.equals(+)91 number += Double.valueOf(display.getText();92 else if (operator.equals(=)93 number = Double.valueOf(display.getText();94 display.setText(String.valueOf(number);95 operator = key;96 isFirstDigit = true;97 98 99 public static void main(String args)100 101 new calculator();102 103 3、 設(shè)計(jì)圖形頁面實(shí)現(xiàn)學(xué)生的信息錄入,至少包括姓名、年齡、出生年月日、java課程實(shí)驗(yàn)成績,成績使用浮點(diǎn)數(shù),年齡使用整型,使用數(shù)據(jù)存儲(chǔ)輸入對(duì)象,程序輸出按年齡排序的學(xué)生信息。1 public class student 2 String name;3 int age;4 double score;5 String birth;6 public student(String name,int age,double score,String birth)7 this.set(name, age, score, birth);8 9 public void set(String name,int age,double score,String birth)10 =name;11 this.age=age;12 this.score=score;13 this.birth=birth;14 15 public String getname()16 return name;17 18 public int getage()19 return age;20 21 public double getscore()22 return score;23 24 public String getbirth()25 return birth;26 27 public String toString()28 return this.getname()+t+this.getage()+t+this.getbirth()+t+this.getscore()+n;29 3031 /*public static void main(String arg)32 student a=new student100;33 a0=new student(1,1,12,123);34 a1=new student(1,1,12,123);35 a2=new student(1,1,12,123);36 System.out.print(a0.toString();37 */38 1 import java.awt.*;2 import java.awt.event.*;345 public class studentmessage extends Frame implements ActionListener6 /*7 * 8 */9 private static final long serialVersionUID = 1L;10 private final String st = Name,Age,Birth,Score;11 static int ii=0;12 student a=new student100;13 TextField t_name,t_age,t_birth,t_score;14 MDialog dia;1516 public studentmessage() 17 super(學(xué)生信息); 18 this.setSize(400,400);19 this.setLocation(300,240); 20 21 Panel panel1 = new Panel(new GridLayout(4, 1);22 for (int i = 0; i st.length; i+) 23 panel1.add(new Label(sti,0);24 25 Panel panel2 = new Panel(new GridLayout(4, 1);26 t_name =new TextField(,20);27 t_age =new TextField();28 t_birth=new TextField();29 t_score=new TextField();30 panel2.add(t_name);31 panel2.add(t_age);32 panel2.add(t_birth);33 panel2.add(t_score);34 35 Panel panel3 = new Panel(new FlowLayout();36 Button b_save = new Button(Save); 37 Button b_check= new Button(Check);38 panel3.add(b_save);39 panel3.add(b_check);40 41 this.setLayout(new BorderLayout();42 this.add(West, panel1);43 this.add(East, panel2);44 this.add(South, panel3);45 46 addWindowListener(new WindowCloser();47 b_save.addActionListener(this);48 b_check.addActionListener(this);49 this.setVisible(true);50 dia=new MDialog(this);51 pack();52 53 54 private class WindowCloser extends WindowAdapter55 public void windowClosing(WindowEvent we)56 System.exit(0);57 58 59 60 private class MDialog extends Dialog61 62 private static final long serialVersionUID = 1L;63 Frame frame; /對(duì)話框所依賴的框架窗口64 TextArea t_show;65 MDialog(Frame frame)66 super(frame,記錄,true);67 this.frame=frame;68 this.setSize(300, 80);69 t_show=new TextArea(20,20);70 this.add(t_show);71 this.addWindowListener(new WindowCloser();72 73 public void show(String s) 74 t_show.setText(s);75 this.setLocation(frame.getX()+100, frame.getY()+100);76 this.setVisible(true);77 78 79 80 public void actionPerformed(ActionEvent e) 81 String label = e.getActionCommand();82 if(label=Save)83 String name=t_name.getText();84 int age =Integer.parseInt(t_age.getText();85 double score=Double.parseDouble(t_score.getText();86 String birth=t_birth.getText();87 aii=new student(name,age,score,birth);88 ii+;89 t_name .setText();90 t_age .setText();91 t_birth.setText();92 t_score.setText();93 selectsort(a);94 95 if(label=Check)96 dia.show(this.toshow();97 98 99 100 101 public static student selectsort(student ex)102 for(int i=0;iii-1;i+)103 int min=i;104 for(int j=i+1;jexj.getage()106 min=j;107 108 if(min!=i)109 student temp=exi;110 exi=exmin;111 exmin=temp;112 113 114 return ex;115 116 117118 public String toshow()119 String s=姓名 年齡 生日 成績n;120 for(int i=0;iii;i+)121 s+=ai.toString();122 return s;123 124125 public static void main(String arg)126 new studentmessage();127 128 實(shí)驗(yàn)五 多線程程序設(shè)計(jì)實(shí)驗(yàn)?zāi)康模豪斫舛嗑€程的概念,掌握創(chuàng)建、管理和控制Java線程對(duì)象的方法,包括創(chuàng)建Java線程對(duì)象、改變線程狀態(tài)、設(shè)置線程優(yōu)先級(jí)及控制線程調(diào)度等方法,掌握實(shí)現(xiàn)線程互斥和線程同步的方法。實(shí)驗(yàn)內(nèi)容:1、 編寫一個(gè)有兩個(gè)線程的程序,第一個(gè)線程用來計(jì)算21000之間的偶數(shù)及個(gè)數(shù),第二個(gè)線程用來計(jì)算10002000之間的偶數(shù)及個(gè)數(shù)。1 public class ex_1 extends Thread2 3 private int first , end, count;4 5 public ex_1(String name,int first,int end)6 super(name);7 this.first=first;8 this.end =end;9 10 public ex_1(String name)11 this(name,0,0);12 13 14 public int getcount()15 return count;16 17 18 public void run()19 System.out.print(n+this.getName()+:);20 if(first%2=1)21 first+;22 for(int i=first;i=end;i+=2)23 System.out.print(i+ );24 count+;25 26 System.out.print(n+this.getName()+結(jié)束,偶數(shù)個(gè)數(shù) +this.getcount()+ );27 28 29 public static void main(String arg)30 ex_1 thread1 = new ex_1(線程1,2,1000);31 ex_1 thread2 = new ex_1(線程2,1000,2000);32 thread1.start();33 thread2.start();34 35 線程1:線程2:1000 1002 1004 2 1006 1008 4 1010 1012 6 8 10 12 14 16 18 20 22 1014 1016 1018 24 1020 1022 26 1024 1026 28 1028 1030 30 1032 32 1034 34 1036 36 1038 38 1040 1042 40 1044 42 1046 44 1048 46 1050 1052 1054 1056 1058 1060 1062 1064 1066 1068 1070 1072 1074 48 1076 50 1078 52 1080 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 1082 1084 1086 1088 1090 1092 1094 1096 1098 1100 1102 1104 1106 1108 1110 1112 1114 1116 1118 1120 1122 1124 1126 1128 1130 1132 1134 1136 1138 1140 1142 1144 1146 1148 1150 1152 1154 1156 1158 1160 1162 1164 1166 1168 1170 1172 1174 1176 1178 1180 1182 1184 1186 1188 1190 1192 1194 1196 1198 1200 1202 1204 1206 1208 1210 1212 1214 1216 1218 1220 1222 1224 1226 1228 1230 1232 1234 1236 106 1238 108 1240 1242 110 1244 1246 112 114 1248 1250 116 1252 1254 118 1256 1258 120 1260 1262 122 1264 1266 124 1268 1270 126 1272 1274 128 1276 1278 130 1280 1282 132 1284 1286 134 1288 1290 1292 1294 136 1296 1298 138 1300 1302 1304 140 1306 1308 142 1310 1312 144 1314 1316 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 282 284 286 288 290 292 294 296 298 300 302 304 306 308 310 312 314 316 318 320 322 324 326 328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358 360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390 392 394 396 398 400 402 404 406 408 410 412 414 416 418 420 422 424 426 428 430 432 434 436 438 440 442 444 446 448 450 452 454 456 458 460 462 464 466 468 470 472 474 476 478 480 482 484 486 488 490 492 494 496 498 500 502 504 506 508 510 512 514 516 518 520 522 524 526 528 530 532 534 536 538 540 542 544 546 548 550 552 554 556 558 560 1318 1320 562 564 566 568 570 572 574 576 578 580 582 584 586 588 590 592 594 596 598 600 602 604 606 608 610 612 614 616 618 620 622 624 626 628 630 632 634 636 638 640 642 644 646 648 650 652 654 656 658 660 662 664 666 668 670 672 674 676 678 680 682 684 686 688 690 692 694 696 698 700 702 704 706 708 710 712 714 716 718 720 722 724 726 728 730 732 734 736 738 740 742 744 746 748 750 752 754 756 758 760 762 764 766 768 770 772 774 776 778 780 782 784 786 788 790 792 794 796 798 800 802 804 806 808 810 812 814 816 818 820 822 824 826 828 830 832 834 836 838 840 842 844 846 848 850 852 854 856 858 860 862 864 866 868 870 872 874 876 878 880 882 884 886 888 890 892 894 896 898 900 902 904 906 908 910 912 914 916 918 920 922 924 926 928 930 932 934 936 938 940 942 944 946 948 950 952 954 956 958 960 962 964 966 968 970 972 974 976 978 980 982 984 986 988 990 992 994 996 998 1000 1322 1324 1326 1328 1330 1332 1334 1336 1338 1340 1342 1344 1346 1348 1350 1352 1354 1356 1358 1360 1362 1364 1366 1368 1370 1372 1374 1376 1378 1380 1382 線程1結(jié)束,偶數(shù)個(gè)數(shù) 500 1384 1386 1388 1390 1392 1394 1396 1398 1400 1402 1404 1406 1408 1410 1412 1414 1416 1418 1420 1422 1424 1426 1428 1430 1432 1434 1436 1438 1440 1442 1444 1446 1448 1450 1452 1454 1456 1458 1460 1462 1464 1466 1468 1470 1472 1474 1476 1478 1480 1482 1484 1486 1488 1490 1492 1494 1496 1498 1500 1502 1504 1506 1508 1510 1512 1514 1516 1518 1520 1522 1524 1526 1528 1530 1532 1534 1536 1538 1540 1542 1544 1546 1548 1550 1552 1554 1556 1558 1560 1562 1564 1566 1568 1570 1572 1574 1576 1578 1580 1582 1584 1586 1588 1590 1592 1594 1596 1598 1600 1602 1604 1606 1608 1610 1612 1614 1616 1618 1620 1622 1624 1626 1628 1630 1632 1634 1636 1638 1640 1642 1644 1646 1648 1650 1652 1654 1656 1658 1660 1662 1664 1666 1668 1670 1672 1674 1676 1678 1680 1682 1684 1686 1688 1690 1692 1694 1696 1698 1700 1702 1704 1706 1708 1710 1712 1714 1716 1718 1720 1722 1724 1726 1728 1730 1732 1734 1736 1738 1740 1742 1744 1746 1748 1750 1752 1754 1756 1758 1760 1762 1764 1766
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年信息技術(shù)對(duì)社會(huì)的影響考試試卷及答案
- 2025年師范生教育理論考試試題及答案
- 2025年氣候變化與應(yīng)對(duì)考試試題及答案
- 2025年會(huì)計(jì)電算化考試試卷及答案解析
- 2025年化學(xué)專業(yè)認(rèn)證考試試卷及答案
- 2025年電子商務(wù)運(yùn)營師培訓(xùn)考試試題及答案
- 2025年供應(yīng)鏈金融與風(fēng)險(xiǎn)控制考試試題及答案
- 2025年廣告學(xué)專業(yè)理論知識(shí)測(cè)試試題及答案
- 農(nóng)業(yè)項(xiàng)目農(nóng)產(chǎn)品采購協(xié)議
- 岳陽樓記文章主旨和修辭教學(xué)教案
- 優(yōu)化能源消耗的綠色I(xiàn)T部署戰(zhàn)略規(guī)劃
- 一年級(jí)學(xué)生元角分練習(xí)500題
- 小學(xué)校長在國旗下講話:守紀(jì)律、善學(xué)習(xí)、鑄品德
- 2025-2030年可調(diào)節(jié)高度臺(tái)球桿行業(yè)跨境出海戰(zhàn)略研究報(bào)告
- 歡樂購物街第2課時(shí) 買賣我做主(說課稿)-2024-2025學(xué)年 一年級(jí)數(shù)學(xué)下冊(cè)人教版
- 合作成果與未來展望模板
- 初中生物2021年初專題周練-血液循環(huán)訓(xùn)練題(一)【含詳解】
- BMS電池管理系統(tǒng)
- 4.2.2光柵傳感器測(cè)量位移
- 四川省成都市(2024年-2025年小學(xué)六年級(jí)語文)部編版小升初模擬(上學(xué)期)試卷及答案
- 2025年華遠(yuǎn)陸港集團(tuán)所屬華遠(yuǎn)陸港網(wǎng)絡(luò)貨運(yùn)(山西)限公司招聘(72人)管理單位筆試遴選500模擬題附帶答案詳解
評(píng)論
0/150
提交評(píng)論