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

下載本文檔

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

文檔簡介

1、信 息 工 程 學(xué) 院實驗報告的內(nèi)容與格式按任課教師的要求書寫。 Java程序設(shè)計 實習(xí)報告JAVA圖形用戶界面實驗六Java圖形用戶界面1實驗?zāi)康模?)掌握圖形用戶界面基本組件。(2)了解如何使用布局管理器對組件進行管理。(3)掌握Java事件處理機制。2實驗內(nèi)容實驗題1 編寫一個模擬計算器的程序,使用面板和網(wǎng)格布局,添加一個文本框,10個數(shù)字按鈕(0-9),4個加減乘除按鈕,一個等號按鈕,一個清除按鈕,要求將計算公式和結(jié)果顯示在文本框中。運行結(jié)果:1 / 30加法:主要代碼:private void initComponents() jButton1 = new javax.swing.J

2、Button();jButton2 = new javax.swing.JButton();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jButton5 = new javax.swing.JButton();jButton6 = new javax.swing.JButton();jButton7 = new javax.swing.JButton();jButton8 = new javax.swing.JButton();jButton9 = new javax.swing.JButt

3、on();jButton10 = new javax.swing.JButton();jButton11 = new javax.swing.JButton();jButton12 = new javax.swing.JButton();jButton13 = new javax.swing.JButton();jButton14 = new javax.swing.JButton();jButton15 = new javax.swing.JButton();jTextField1 = new javax.swing.JTextField();setStub(null);jButton1.s

4、etText("3");jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton1ActionPerformed(evt););jButton2.setText("1");jButton2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(j

5、ava.awt.event.ActionEvent evt) jButton2ActionPerformed(evt););jButton3.setText("5");jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton3ActionPerformed(evt););jButton4.setText("2");jButton4.addActionLi

6、stener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton4ActionPerformed(evt););jButton5.setText("6");jButton5.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton5Act

7、ionPerformed(evt););jButton6.setText("8");jButton6.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton6ActionPerformed(evt););jButton7.setText("4");jButton7.addActionListener(new java.awt.event.ActionListener()

8、 public void actionPerformed(java.awt.event.ActionEvent evt) jButton7ActionPerformed(evt););jButton8.setText("7");jButton8.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton8ActionPerformed(evt););jButton9.setText("

9、;0");jButton9.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton9ActionPerformed(evt););jButton10.setText("9");jButton10.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.ev

10、ent.ActionEvent evt) jButton10ActionPerformed(evt););jButton11.setText("u00f7");jButton11.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton11ActionPerformed(evt););jButton12.setText("u00d7");jButton12.addActi

11、onListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton12ActionPerformed(evt););jButton13.setText("-");jButton13.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jBut

12、ton13ActionPerformed(evt););jButton14.setText("+");jButton14.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton14ActionPerformed(evt););jButton15.setText("=");jButton15.addActionListener(new java.awt.event.Act

13、ionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton15ActionPerformed(evt););實驗題2 編寫一個程序,有一個窗口,該窗口為BorderLayout布局。窗口的中心添加一個Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49個標(biāo)簽,用來顯示日歷。窗口北面添加一個Panel容器pNorth,其布局是FlowLayout布局,pNorth放置兩個按鈕:nextMonth和previousMonth按鈕,單

14、擊nextMonth,可以顯示當(dāng)前月的下一個月的日歷;單擊previousMonth按鈕,可以顯示當(dāng)前月的上一個月的日歷。窗口的南面添加一個Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一個標(biāo)簽用來顯示一些信息。運行結(jié)果如圖所示。圖3.8 運行結(jié)果圖基本要求 編寫完整程序。運行結(jié)果:主要代碼:private JLabel buttonDay = new JLabel42;private JButton buttonWeek = new JButton7;private JLabel labelMonth = new JLabel();private JButt

15、on buttonLastMonth = new JButton();private JButton buttonNextMonth = new JButton();private JPanel pCenter=new JPanel();private JPanel pNorth=new JPanel();private JPanel pSouth=new JPanel();private JLabel time=new JLabel();public Calender() super("Calender"); setBounds(250, 200, 600, 500);

16、setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonLastMonth.setText("上月"); buttonLastMonth.addActionListener(this); pNorth.add(buttonLastMonth); buttonNextMonth.setText("下月"); buttonNextMonth.addActionListener(this); pNorth.add(buttonNextMonth); getContentPane().add(pNorth,Bo

17、rderLayout.NORTH); getContentPane().add(pCenter,BorderLayout.CENTER); pCenter.setLayout(new GridLayout(7,7); for (int i = 0; i < 7; i+) buttonWeeki = new JButton(); buttonWeeki.setText(stringWeekCni); pCenter.add(buttonWeeki); for (int i = 0; i < 42; i+) buttonDayi = new JLabel(); buttonDayi.s

18、etText(" "); pCenter.add(buttonDayi); 實驗題3 實現(xiàn)如圖3.9所示的布局方式功能:前兩個文本框輸入整型數(shù)據(jù)。第三個文本框存放前兩個文本框數(shù)據(jù)之和。要求如下:第一個文本框的數(shù)據(jù)是100,200,如果超出該范圍彈出對話框提示用戶。彈出提示對話框的時刻是光標(biāo)離開第一個文本框時。圖3.9 求和運行結(jié)果:檢驗輸入數(shù)據(jù)范圍:主要代碼:class MouseHander extends MouseAdapter public MouseHander(JTextField c) current=c; public void mousePressed(M

19、ouseEvent event) if(current=result) double firstNumber=Double.parseDouble(first.getText(); double secondNumber=Double.parseDouble(second.getText(); double Result=firstNumber+secondNumber; result.setText(""+Result); else current.setText(""); private JTextField current; class Mouse

20、MotionHander extends MouseMotionAdapter public void mouseMoved(MouseEvent event) double number=Double.parseDouble(first.getText(); if(number<100|number>200) int type=JOptionPane.ERROR_MESSAGE; JOptionPane.showMessageDialog(null,new String("輸入數(shù)字必須在100200之間"),"提示", 2); 實驗題4 編

21、寫一個顯示圖像文件的Application應(yīng)用程序,在該程序JFrame窗體中添加JPanel面板和一個JToolBar工具欄,在工具欄上添加一個JButton“打開”按扭,單擊“打開”按紐,彈出JFileChooser文件打開選擇對話框,選擇圖像文件后將其顯示在JPnel面板中。運行結(jié)果:主要代碼:public class NewJFrame extends javax.swing.JFrame private JPanel jPanel1;private JPanel jPanel2;private JButton jButton2;private JButton jButton1;pri

22、vate String filePath = ""/* Auto-generated main method to display this JFrame*/public static void main(String args) SwingUtilities.invokeLater(new Runnable() public void run() NewJFrame inst = new NewJFrame();inst.setLocationRelativeTo(null);inst.setVisible(true););public NewJFrame() super

23、();initGUI();private void initGUI() try setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);jPanel1 = new JPanel(true);FlowLayout jPanel1Layout = new FlowLayout();getContentPane().add(jPanel1, BorderLayout.NORTH);jPanel1.setPreferredSize(new java.awt.Dimension(384, 43);jPanel1.setLayout(jPane

24、l1Layout);jButton2 = new JButton();jPanel1.add(jButton2);jButton2.setText("Open");jButton2.setBounds(104, 12, 62, 22);jButton2.setPreferredSize(new java.awt.Dimension(76, 22);jButton2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent evt) jButton2ActionPerforme

25、d(evt););jButton1 = new JButton();jPanel1.add(jButton1);jButton1.setText("Close");jButton1.setBounds(204, 12, 62, 22);jButton1.setPreferredSize(new java.awt.Dimension(72, 22);jButton1.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent evt) System.exit(0);/TODO add your

26、 code for jButton1.mousePressed);jPanel2 = new JPanel();getContentPane().add(jPanel2, BorderLayout.CENTER);pack();setSize(400, 300); catch (Exception e) /add your error handling code heree.printStackTrace();private void jButton2ActionPerformed(ActionEvent evt) String action = evt.getActionCommand();

27、JFileChooser fileChooser = new JFileChooser();int index = fileChooser.showOpenDialog(getContentPane();if(index = JFileChooser.APPROVE_OPTION)File selectedFile = fileChooser.getSelectedFile(); /取得選中的文件filePath = selectedFile.getPath();filePath = filePath.replace('','/');ImageIcon icon

28、 = new ImageIcon(filePath);JLabel label = new JLabel(icon,JLabel.CENTER);JLabel p=new JLabel("圖片路徑:" + filePath);jPanel2.add(p,BorderLayout.SOUTH);jPanel2.add(label);jPanel2.update(null);實驗題5使用SWT技術(shù)完成圖3.10 所示圖形界面的制作。要求“查詢結(jié)果”用group組件。圖3.10 數(shù)據(jù)查詢界面運行結(jié)果;主要代碼:public class Search extends org.ecl

29、ipse.swt.widgets.Composite private Composite composite1;private Composite composite2;private Group group1;private Text text4;private Label label4;private Text text3;private Label label3;private Text text2;private Label label2;private Button button2;private Button button1;private Text text1;private L

30、abel label1;/Register as a resource user - SWTResourceManager will/handle the obtaining and disposing of resourcesSWTResourceManager.registerResourceUser(this);public Search(Composite parent, int style) super(parent, style);initGUI();/* Initializes the GUI.*/private void initGUI() try this.setSize(n

31、ew org.eclipse.swt.graphics.Point(400,300);this.setBackground(SWTResourceManager.getColor(192, 192, 192);FormLayout thisLayout = new FormLayout();this.setLayout(thisLayout);composite1 = new Composite(this, SWT.NONE);FormLayout composite1Layout = new FormLayout();FormData composite1LData = new FormDa

32、ta();composite1LData.left = new FormAttachment(0, 1000, 0);composite1LData.top = new FormAttachment(0, 1000, 0);composite1LData.width = 400;composite1LData.height = 300;composite1.setLayoutData(composite1LData);composite1.setLayout(composite1Layout);group1 = new Group(composite1, SWT.NONE);GridLayou

33、t group1Layout = new GridLayout();group1Layout.marginBottom = 30;group1Layout.numColumns = 2;group1Layout.verticalSpacing = 15;group1Layout.marginTop = 10;group1.setLayout(group1Layout);FormData group1LData = new FormData();group1LData.left = new FormAttachment(0, 1000, 38);group1LData.top = new For

34、mAttachment(0, 1000, 106);group1LData.width = 318;group1LData.height = 145;group1.setLayoutData(group1LData);group1.setText("u67e5u8be2u7ed3u679c");group1.setFont(SWTResourceManager.getFont("微軟雅黑", 11, 0, false, false);label2 = new Label(group1, SWT.NONE);GridData label2LData = n

35、ew GridData();label2LData.widthHint = 70;label2LData.heightHint = 25;label2.setLayoutData(label2LData);label2.setText("u5b66u53f7uff1a");label2.setFont(SWTResourceManager.getFont("微軟雅黑", 11, 0, false, false);GridData text2LData = new GridData();text2LData.widthHint = 181;text2LDa

36、ta.heightHint = 27;text2 = new Text(group1, SWT.NONE);text2.setLayoutData(text2LData);label3 = new Label(group1, SWT.NONE);GridData label3LData = new GridData();label3LData.widthHint = 63;label3LData.heightHint = 23;label3.setLayoutData(label3LData);label3.setText("u59d3u540duff1a");label3

37、.setFont(SWTResourceManager.getFont("微軟雅黑", 11, 0, false, false);GridData text3LData = new GridData();text3LData.widthHint = 181;text3LData.heightHint = 27;text3 = new Text(group1, SWT.NONE);text3.setLayoutData(text3LData);label4 = new Label(group1, SWT.NONE);GridData label4LData = new Gri

38、dData();label4LData.widthHint = 57;label4LData.heightHint = 28;label4.setLayoutData(label4LData);label4.setText("u5e74u9f84uff1a");label4.setFont(SWTResourceManager.getFont("微軟雅黑", 11, 0, false, false);GridData text4LData = new GridData();text4LData.widthHint = 182;text4LData.hei

39、ghtHint = 31;text4 = new Text(group1, SWT.NONE);text4.setLayoutData(text4LData);composite2 = new Composite(composite1, SWT.NONE);GridLayout composite2Layout = new GridLayout();composite2Layout.numColumns = 2;composite2Layout.marginLeft = 40;composite2Layout.horizontalSpacing = 10;composite2Layout.ve

40、rticalSpacing = 15;FormData composite2LData = new FormData();composite2LData.left = new FormAttachment(0, 1000, 12);composite2LData.top = new FormAttachment(0, 1000, 12);composite2LData.width = 376;composite2LData.height = 88;composite2.setLayoutData(composite2LData);composite2.setLayout(composite2L

41、ayout);label1 = new Label(composite2, SWT.NONE);GridData label1LData = new GridData();label1LData.widthHint = 96;label1LData.heightHint = 22;label1.setLayoutData(label1LData);label1.setText("u8bf7u8f93u5165u5b66u53f7uff1a");label1.setFont(SWTResourceManager.getFont("微軟雅黑", 12, 0,

42、 false, false);GridData text1LData = new GridData();text1LData.widthHint = 182;text1LData.heightHint = 28;text1 = new Text(composite2, SWT.NONE);text1.setLayoutData(text1LData);button1 = new Button(composite2, SWT.PUSH | SWT.CENTER);GridData button1LData = new GridData();button1LData.widthHint = 70;

43、button1LData.heightHint = 27;button1.setLayoutData(button1LData);button1.setText("u786eu5b9a");button2 = new Button(composite2, SWT.PUSH | SWT.CENTER);GridData button2LData = new GridData();button2LData.widthHint = 78;button2LData.heightHint = 27;button2.setLayoutData(button2LData);button2

44、.setText("u91cdu7f6e");this.layout(); catch (Exception e) e.printStackTrace();/* Auto-generated main method to display this * org.eclipse.swt.widgets.Composite inside a new Shell.*/public static void main(String args) Display display = Display.getDefault();Shell shell = new Shell(display);

45、Search inst = new Search(shell, SWT.NULL);Point size = inst.getSize();shell.setLayout(new FillLayout();shell.layout();if(size.x = 0 && size.y = 0) inst.pack();shell.pack(); else Rectangle shellBounds = puteTrim(0, 0, size.x, size.y);shell.setSize(shellBounds.width, shellBounds.height);shell.

46、open();while (!shell.isDisposed() if (!display.readAndDispatch()display.sleep();實驗題6 使用SWT技術(shù)完成圖3.11所示學(xué)籍管理主界面的設(shè)計與制作。圖3.11學(xué)籍管理主界面運行結(jié)果:主要代碼:public class Example public static void main(String args) Window win=new Window("學(xué)籍管理系統(tǒng)",300,200,500,500);package package6;import java.awt.*;import javax

47、.swing.*;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import static javax.swing.JFrame.*;import java.awt.event.*;import javax.swing.*;public class Window extends JFrame JMenuBar menubar;JMenu menu, menu2, menu3, menuhelp;JMenuItem item1, item2;JLabel text;public Window() public Wi

48、ndow(String s, int x, int y, int w, int h) init(s);setLocation(x, y);setSize(w, h);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);void init(String s) setTitle(s);setLayout(null);text = new JLabel("歡迎使用學(xué)籍管理系統(tǒng) !");text.setForeground(Color.red);text.setFont(new java.awt.Font(&quo

49、t;Dialog", 1, 22);add(text);text.setBounds(140, 150, 250, 150);menubar = new JMenuBar();menu = new JMenu("在線登陸");menu2 = new JMenu("用戶管理");menu3 = new JMenu("數(shù)據(jù)維護");menuhelp = new JMenu("幫助");item1 = new JMenuItem("用戶登錄", new ImageIcon("Login,gif");item2 = new JMenuItem("退 出", new ImageIcon("Exit.gif");menu.add(item1);menu.add(item2);menubar.add(menu);menubar.add(menu2);menubar.add(menu3);menubar.add(menuhel

溫馨提示

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

評論

0/150

提交評論