java實驗報告實驗六Java圖形用戶界面_第1頁
java實驗報告實驗六Java圖形用戶界面_第2頁
java實驗報告實驗六Java圖形用戶界面_第3頁
java實驗報告實驗六Java圖形用戶界面_第4頁
java實驗報告實驗六Java圖形用戶界面_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

./信息工程學院實驗報告的內(nèi)容與格式按任課教師的要求書寫。實驗報告的內(nèi)容與格式按任課教師的要求書寫。Java程序設(shè)計實習報告JAVA圖形用戶界面實驗六Java圖形用戶界面1.實驗目的〔1掌握圖形用戶界面基本組件?!?了解如何使用布局管理器對組件進行管理?!?掌握Java事件處理機制。2.實驗內(nèi)容實驗題1編寫一個模擬計算器的程序,使用面板和網(wǎng)格布局,添加一個文本框,10個數(shù)字按鈕<0-9>,4個加減乘除按鈕,一個等號按鈕,一個清除按鈕,要求將計算公式和結(jié)果顯示在文本框中。運行結(jié)果:加法:主要代碼:privatevoidinitComponents<>{jButton1=newjavax.swing.JButton<>;jButton2=newjavax.swing.JButton<>;jButton3=newjavax.swing.JButton<>;jButton4=newjavax.swing.JButton<>;jButton5=newjavax.swing.JButton<>;jButton6=newjavax.swing.JButton<>;jButton7=newjavax.swing.JButton<>;jButton8=newjavax.swing.JButton<>;jButton9=newjavax.swing.JButton<>;jButton10=newjavax.swing.JButton<>;jButton11=newjavax.swing.JButton<>;jButton12=newjavax.swing.JButton<>;jButton13=newjavax.swing.JButton<>;jButton14=newjavax.swing.JButton<>;jButton15=newjavax.swing.JButton<>;jTextField1=newjavax.swing.JTextField<>; setStub<null>;jButton1.setText<"3">;jButton1.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton1ActionPerformed<evt>; } }>;jButton2.setText<"1">;jButton2.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton2ActionPerformed<evt>; } }>;jButton3.setText<"5">;jButton3.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton3ActionPerformed<evt>; } }>;jButton4.setText<"2">;jButton4.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton4ActionPerformed<evt>; } }>;jButton5.setText<"6">;jButton5.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton5ActionPerformed<evt>; } }>;jButton6.setText<"8">;jButton6.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton6ActionPerformed<evt>; } }>;jButton7.setText<"4">;jButton7.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton7ActionPerformed<evt>; } }>;jButton8.setText<"7">;jButton8.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton8ActionPerformed<evt>; } }>;jButton9.setText<"0">;jButton9.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton9ActionPerformed<evt>; } }>;jButton10.setText<"9">;jButton10.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton10ActionPerformed<evt>; } }>;jButton11.setText<"\u00f7">;jButton11.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton11ActionPerformed<evt>; } }>;jButton12.setText<"\u00d7">;jButton12.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton12ActionPerformed<evt>; } }>;jButton13.setText<"-">;jButton13.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton13ActionPerformed<evt>; } }>;jButton14.setText<"+">;jButton14.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton14ActionPerformed<evt>; } }>;jButton15.setText<"=">;jButton15.addActionListener<newjava.awt.event.ActionListener<>{publicvoidactionPerformed<java.awt.event.ActionEventevt>{ jButton15ActionPerformed<evt>; } }>;實驗題2編寫一個程序,有一個窗口,該窗口為BorderLayout布局。窗口的中心添加一個Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49個標簽,用來顯示日歷。窗口北面添加一個Panel容器pNorth,其布局是FlowLayout布局,pNorth放置兩個按鈕:nextMonth和previousMonth按鈕,單擊nextMonth,可以顯示當前月的下一個月的日歷;單擊previousMonth按鈕,可以顯示當前月的上一個月的日歷。窗口的南面添加一個Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一個標簽用來顯示一些信息。運行結(jié)果如圖所示。圖3.8運行結(jié)果圖[基本要求]編寫完整程序。運行結(jié)果:主要代碼:privateJLabel[]buttonDay=newJLabel[42];privateJButton[]buttonWeek=newJButton[7];privateJLabellabelMonth=newJLabel<>;privateJButtonbuttonLastMonth=newJButton<>;privateJButtonbuttonNextMonth=newJButton<>;privateJPanelpCenter=newJPanel<>;privateJPanelpNorth=newJPanel<>;privateJPanelpSouth=newJPanel<>;privateJLabeltime=newJLabel<>;publicCalender<>{super<"Calender">;setBounds<250,200,600,500>;setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;buttonLastMonth.setText<"上月">;buttonLastMonth.addActionListener<this>;pNorth.add<buttonLastMonth>;buttonNextMonth.setText<"下月">;buttonNextMonth.addActionListener<this>;pNorth.add<buttonNextMonth>;getContentPane<>.add<pNorth,BorderLayout.NORTH>;getContentPane<>.add<pCenter,BorderLayout.CENTER>;pCenter.setLayout<newGridLayout<7,7>>;for<inti=0;i<7;i++>{buttonWeek[i]=newJButton<>;buttonWeek[i].setText<stringWeekCn[i]>;pCenter.add<buttonWeek[i]>;}for<inti=0;i<42;i++>{buttonDay[i]=newJLabel<>;buttonDay[i].setText<"">;pCenter.add<buttonDay[i]>;}實驗題3實現(xiàn)如圖3.9所示的布局方式功能:前兩個文本框輸入整型數(shù)據(jù)。第三個文本框存放前兩個文本框數(shù)據(jù)之和。要求如下:第一個文本框的數(shù)據(jù)是[100,200],如果超出該范圍彈出對話框提示用戶。彈出提示對話框的時刻是光標離開第一個文本框時。圖3.9求和運行結(jié)果:檢驗輸入數(shù)據(jù)范圍:主要代碼:classMouseHanderextendsMouseAdapter{publicMouseHander<JTextFieldc>{current=c;}publicvoidmousePressed<MouseEventevent>{if<current==result>{doublefirstNumber=Double.parseDouble<first.getText<>>;doublesecondNumber=Double.parseDouble<second.getText<>>;doubleResult=firstNumber+secondNumber;result.setText<""+Result>;}elsecurrent.setText<"">;}privateJTextFieldcurrent;}classMouseMotionHanderextendsMouseMotionAdapter{publicvoidmouseMoved<MouseEventevent>{doublenumber=Double.parseDouble<first.getText<>>;if<number<100||number>200>{inttype=JOptionPane.ERROR_MESSAGE;JOptionPane.showMessageDialog<null,newString<"輸入數(shù)字必須在100~200之間">,"提示",2>;}}}實驗題4編寫一個顯示圖像文件的Application應(yīng)用程序,在該程序JFrame窗體中添加JPanel面板和一個JToolBar工具欄,在工具欄上添加一個JButton"打開"按扭,單擊"打開"按紐,彈出JFileChooser文件打開選擇對話框,選擇圖像文件后將其顯示在JPnel面板中。運行結(jié)果:主要代碼:publicclassNewJFrameextendsjavax.swing.JFrame{privateJPaneljPanel1;privateJPaneljPanel2;privateJButtonjButton2;privateJButtonjButton1;privateStringfilePath="";/***Auto-generatedmainmethodtodisplaythisJFrame*/publicstaticvoidmain<String[]args>{ SwingUtilities.invokeLater<newRunnable<>{publicvoidrun<>{ NewJFrameinst=newNewJFrame<>; inst.setLocationRelativeTo<null>; inst.setVisible<true>; } }>; }publicNewJFrame<>{super<>; initGUI<>; }privatevoidinitGUI<>{try{ setDefaultCloseOperation<WindowConstants.DISPOSE_ON_CLOSE>; {jPanel1=newJPanel<true>; FlowLayoutjPanel1Layout=newFlowLayout<>; getContentPane<>.add<jPanel1,BorderLayout.NORTH>;jPanel1.setPreferredSize<newjava.awt.Dimension<384,43>>;jPanel1.setLayout<jPanel1Layout>; {jButton2=newJButton<>;jPanel1.add<jButton2>;jButton2.setText<"Open">;jButton2.setBounds<104,12,62,22>;jButton2.setPreferredSize<newjava.awt.Dimension<76,22>>;jButton2.addActionListener<newActionListener<>{publicvoidactionPerformed<ActionEventevt>{ jButton2ActionPerformed<evt>; } }>; } {jButton1=newJButton<>;jPanel1.add<jButton1>;jButton1.setText<"Close">;jButton1.setBounds<204,12,62,22>;jButton1.setPreferredSize<newjava.awt.Dimension<72,22>>;jButton1.addMouseListener<newMouseAdapter<>{publicvoidmousePressed<MouseEventevt>{ System.exit<0>;//TODOaddyourcodeforjButton1.mousePressed } }>; } } {jPanel2=newJPanel<>; getContentPane<>.add<jPanel2,BorderLayout.CENTER>; } pack<>; setSize<400,300>; }catch<Exceptione>{//addyourerrorhandlingcodehere e.printStackTrace<>; } }privatevoidjButton2ActionPerformed<ActionEventevt>{ Stringaction=evt.getActionCommand<>; JFileChooserfileChooser=newJFileChooser<>;intindex=fileChooser.showOpenDialog<getContentPane<>>;if<index==JFileChooser.APPROVE_OPTION> { FileselectedFile=fileChooser.getSelectedFile<>;//取得選中的文件filePath=selectedFile.getPath<>;filePath=filePath.replace<'\\','/'>; ImageIconicon=newImageIcon<filePath>; JLabellabel=newJLabel<icon,JLabel.CENTER>; JLabelp=newJLabel<"圖片路徑:"+filePath>;jPanel2.add<p,BorderLayout.SOUTH>;jPanel2.add<label>;jPanel2.update<null>; } }}實驗題5使用SWT技術(shù)完成圖3.10所示圖形界面的制作。要求"查詢結(jié)果"用group組件。圖3.10數(shù)據(jù)查詢界面運行結(jié)果;主要代碼:publicclassSearchextendsorg.eclipse.swt.widgets.Composite{privateCompositecomposite1;privateCompositecomposite2;privateGroupgroup1;privateTexttext4;privateLabellabel4;privateTexttext3;privateLabellabel3;privateTexttext2;privateLabellabel2;privateButtonbutton2;privateButtonbutton1;privateTexttext1;privateLabellabel1; {//Registerasaresourceuser-SWTResourceManagerwill//handletheobtaininganddisposingofresourcesSWTResourceManager.registerResourceUser<this>; }publicSearch<Compositeparent,intstyle>{super<parent,style>; initGUI<>; }/** *InitializestheGUI. */privatevoidinitGUI<>{try{this.setSize<neworg.eclipse.swt.graphics.Point<400,300>>;this.setBackground<SWTResourceManager.getColor<192,192,192>>;FormLayoutthisLayout=newFormLayout<>;this.setLayout<thisLayout>; {composite1=newComposite<this,SWT.NONE>;FormLayoutcomposite1Layout=newFormLayout<>;FormDatacomposite1LData=newFormData<>; composite1LData.left=newFormAttachment<0,1000,0>; composite1LData.top=newFormAttachment<0,1000,0>; composite1LData.width=400; composite1LData.height=300;composite1.setLayoutData<composite1LData>;composite1.setLayout<composite1Layout>; {group1=newGroup<composite1,SWT.NONE>;GridLayoutgroup1Layout=newGridLayout<>; group1Layout.marginBottom=30; group1Layout.numColumns=2; group1Layout.verticalSpacing=15; group1Layout.marginTop=10;group1.setLayout<group1Layout>;FormDatagroup1LData=newFormData<>; group1LData.left=newFormAttachment<0,1000,38>; group1LData.top=newFormAttachment<0,1000,106>; group1LData.width=318; group1LData.height=145;group1.setLayoutData<group1LData>;group1.setText<"\u67e5\u8be2\u7ed3\u679c">;group1.setFont<SWTResourceManager.getFont<"微軟雅黑",11,0,false,false>>; {label2=newLabel<group1,SWT.NONE>;GridDatalabel2LData=newGridData<>; label2LData.widthHint=70; label2LData.heightHint=25;label2.setLayoutData<label2LData>;label2.setText<"\u5b66\u53f7\uff1a">;label2.setFont<SWTResourceManager.getFont<"微軟雅黑",11,0,false,false>>; } {GridDatatext2LData=newGridData<>; text2LData.widthHint=181; text2LData.heightHint=27;text2=newText<group1,SWT.NONE>;text2.setLayoutData<text2LData>; } {label3=newLabel<group1,SWT.NONE>;GridDatalabel3LData=newGridData<>; label3LData.widthHint=63; label3LData.heightHint=23;label3.setLayoutData<label3LData>;label3.setText<"\u59d3\u540d\uff1a">;label3.setFont<SWTResourceManager.getFont<"微軟雅黑",11,0,false,false>>; } {GridDatatext3LData=newGridData<>; text3LData.widthHint=181; text3LData.heightHint=27;text3=newText<group1,SWT.NONE>;text3.setLayoutData<text3LData>; } {label4=newLabel<group1,SWT.NONE>;GridDatalabel4LData=newGridData<>; label4LData.widthHint=57; label4LData.heightHint=28;label4.setLayoutData<label4LData>;label4.setText<"\u5e74\u9f84\uff1a">;label4.setFont<SWTResourceManager.getFont<"微軟雅黑",11,0,false,false>>; } { GridDatatext4LData=newGridData<>; text4LData.widthHint=182; text4LData.heightHint=31;text4=newText<group1,SWT.NONE>;text4.setLayoutData<text4LData>; } } {composite2=newComposite<composite1,SWT.NONE>; GridLayoutcomposite2Layout=newGridLayout<>; composite2Layout.numColumns=2; composite2Layout.marginLeft=40; composite2Layout.horizontalSpacing=10; composite2Layout.verticalSpacing=15; FormDatacomposite2LData=newFormData<>; composite2LData.left=newFormAttachment<0,1000,12>; composite2LData.top=newFormAttachment<0,1000,12>; composite2LData.width=376; composite2LData.height=88;composite2.setLayoutData<composite2LData>;composite2.setLayout<composite2Layout>; {label1=newLabel<composite2,SWT.NONE>; GridDatalabel1LData=newGridData<>; label1LData.widthHint=96; label1LData.heightHint=22;label1.setLayoutData<label1LData>;label1.setText<"\u8bf7\u8f93\u5165\u5b66\u53f7\uff1a">;label1.setFont<SWTResourceManager.getFont<"微軟雅黑",12,0,false,false>>; } { GridDatatext1LData=newGridData<>; text1LData.widthHint=182; text1LData.heightHint=28;text1=newText<composite2,SWT.NONE>;text1.setLayoutData<text1LData>; } {button1=newButton<composite2,SWT.PUSH|SWT.CENTER>; GridDatabutton1LData=newGridData<>; button1LData.widthHint=70; button1LData.heightHint=27;button1.setLayoutData<button1LData>;button1.setText<"\u786e\u5b9a">; } {button2=newButton<composite2,SWT.PUSH|SWT.CENTER>; GridDatabutton2LData=newGridData<>; button2LData.widthHint=78; button2LData.heightHint=27;button2.setLayoutData<button2LData>;button2.setText<"\u91cd\u7f6e">; } } }this.layout<>; }catch<Exceptione>{ e.printStackTrace<>; } }/** *Auto-generatedmainmethodtodisplaythis *org.eclipse.swt.widgets.CompositeinsideanewShell. */publicstaticvoidmain<String[]args>{Displaydisplay=Display.getDefault<>;Shellshell=newShell<display>; Searchinst=newSearch<shell,SWT.NULL>;Pointsize=inst.getSize<>; shell.setLayout<newFillLayout<>>; shell.layout<>;if<size.x==0&&size.y==0>{ inst.pack<>; shell.pack<>; }else{RectangleshellBounds=puteTrim<0,0,size.x,size.y>; shell.setSize<shellBounds.width,shellBounds.height>; } shell.open<>;while<!shell.isDisposed<>>{if<!display.readAndDispatch<>> display.sleep<>; } }}實驗題6使用SWT技術(shù)完成圖3.11所示學籍管理主界面的設(shè)計與制作。圖3.11學籍管理主界面運行結(jié)果:主要代碼:publicclassExample{publicstaticvoidmain<String[]args>{ Windowwin=newWindow<"學籍管理系統(tǒng)",300,200,500,500>; }}packagepackage6;importjava.awt.*;importjavax.swing.*;import;import;importstatic.*;import.*;importjavax.swing.*;publicclassWindowextendsJFrame{ JMenuBarmenubar; JMenumenu,menu2,menu3,menuhelp; JMenuItemitem1,item2; JLabeltext;publicWindow<>{ }publicWindow<Strings,intx,inty,intw,inth>{ init<s>; setLocation<x,y>; setSize<w,h>; setVisible<true>; setDefaultCloseOperation<DISPOSE_ON_CLOSE>; }voidinit<Strings>{ setTitle<s>; setLayout<null>;text=newJLabel<"歡迎使用學籍管理系統(tǒng)!">;text.setForeground<Color.red>;text.setFont<newjava.awt.Font<"Dialog",1,22>>; add<text>;text.setBounds<140,150,250,150>;menubar=newJMenuBar<>;menu=newJMenu<"在線登陸">;menu2=newJMenu<"用戶管理">;menu3=newJMenu<"數(shù)據(jù)維護">;menuhelp=newJMenu<"幫助">;item1=newJMenuItem<"用戶登錄",newImageIcon<"Login,gif">>;item2=newJMenuItem<"退出",newImageIco

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論