java 程序設(shè)計 實驗五_第1頁
java 程序設(shè)計 實驗五_第2頁
java 程序設(shè)計 實驗五_第3頁
java 程序設(shè)計 實驗五_第4頁
java 程序設(shè)計 實驗五_第5頁
已閱讀5頁,還剩21頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、 實 驗 報 告課程名稱: JAVA程序設(shè)計 學 生 姓 名: 孫文浩 班 級 計算機141 學 號 14416122 學 院(系): 信息數(shù)理學院 指 導(dǎo) 教 師: 呂猛 實驗時間: 2016 年 12 月 6 日 實驗名稱: 實驗五 基于Swing的圖形用戶界面開發(fā)實驗 實驗五 基于Swing的圖形用戶界面開發(fā)實驗一 實驗內(nèi)容(1)完成開發(fā)界面如圖所示,實現(xiàn)如下功能選中“查看已有名片”按鈕,程序處于查看狀態(tài),可以在“名片列表”中選擇要查看的名片,程序在“名片詳實信息”欄顯示該名片的詳實信息,此狀態(tài)中名片信息處于不可編輯狀態(tài)選中“添加新名片”按鈕,程序處于添加狀態(tài),所有關(guān)于明天信息的項目都處

2、于可編輯狀態(tài),設(shè)置完成相關(guān)信息后,單擊“添加”按鈕,在“名片列表”中出現(xiàn)新添加名片的名稱。單擊“清空”按鈕可以清空還沒提交的內(nèi)容在查看狀態(tài)和添加狀態(tài)下,都可以通過點擊“愛好”、“學歷”單選按鈕以顯示名片的不同附加信息(備注:名片附加信息部分,當點擊“愛好”按鈕時顯示的如圖1所示,當點擊“學歷”按鈕時顯示如圖2所示。)圖1圖2(2)記事本設(shè)計與實現(xiàn)如圖所示(1)編程實現(xiàn)如圖所示的界面(2)實現(xiàn)文件、編輯、格式、查看,幫助菜單(3)實現(xiàn)記事本的基本功能二 實驗?zāi)康模?) 理解組件的分類(2) 掌握容器、布局管理器、普通組件的使用(3) 理解事件處理機制的工作原理(4) 掌握事件的添加和功能代碼的編

3、寫方法三 實驗過程(1) 問題分析答:名片的設(shè)計除了使用最基本的組件,如JButton,Jlist,JCheckBox, JTextField, JRadioButton, CardLayout以外,最主要的是邏輯事件的編寫,在成功布局完見面后,就需要編寫事件處理。首先,由于是名片信息,保存著一個人的基本信息,所以可以用一個類來表示,如PersonInf類,其次,由于是多個人的信息,可以通過ArrayList來存放類;其次就是當點擊按鈕的時候,正確添加和顯示信息,此處用到cardLayout布局,可以減少界面的復(fù)雜度,在使用JList的時候,除了加滾動條意外,還需要listModel獲取數(shù)據(jù)等

4、。(2) 代碼編寫答:(1)public class PersonInf public String yourname;public String address1;public String address2;public String email;public String sing = null;public String dance = null;public String web = null;public String football = null;public String basketball = null;public String volleyball = null;pu

5、blic String xueshi = null;public String shuoshi = null;public String boshi = null;public String qita = null;public PersonInf(String yourname, String address1, String address2, String email, String sing, String dance,String web, String football, String basketball, String volleyball,String xueshi,Stri

6、ng shuoshi,String boshi, String qita) super();this.yourname = yourname;this.address1 = address1;this.address2 = address2;this.email = email;this.sing = sing;this.dance = dance;this.web = web;this.football = football;this.basketball = basketball;this.volleyball = volleyball;this.xueshi = xueshi;this.

7、shuoshi = shuoshi;this.boshi = boshi;this.qita = qita;import java.awt.BorderLayout;import javax.swing.*;import java.awt.*;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JInternalFrame;import javax.swing.JRadioBu

8、tton;import javax.swing.JLayeredPane;import javax.swing.JSplitPane;import javax.swing.BorderFactory;import javax.swing.Box;import javax.swing.DefaultListModel;import java.awt.Component;import javax.swing.border.SoftBevelBorder;import javax.swing.event.ListSelectionEvent;import javax.swing.event.List

9、SelectionListener;import javax.swing.border.BevelBorder;import javax.swing.JLabel;import javax.swing.JTextField;import javax.swing.JButton;import javax.swing.border.EtchedBorder;import javax.swing.JFormattedTextField;import javax.swing.JSpinner;import javax.swing.JTable;import javax.swing.JList;impo

10、rt javax.swing.JScrollPane;import java.awt.Color;import javax.swing.JTextArea;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.util.ArrayList;import java.awt.event.ActionEvent;import java.awt.CardLayout;public class card extends JFra

11、me private JPanel contentPane;public JTextField nametext;public JTextField address1test;public JTextField address2test;public JTextField emailtest;public JButton addmessagebutton;public JButton clearbutton;private JList list;/學歷,愛好,添加新名片,查看名片的按鈕private JRadioButton rdbtnNewRadioButton_2;private JRad

12、ioButton rdbtnNewRadioButton_3;public JRadioButton rdbtnNewRadioButton_1;public JRadioButton rdbtnNewRadioButton;private JPanel panel_4;/學歷和愛好內(nèi)的多選項public JCheckBox singbox;public JCheckBox dancebox;public JCheckBox webbox;public JCheckBox footballbox;public JCheckBox basketballbox;public JCheckBox v

13、olleyballbox;public JCheckBox xueshibox;public JCheckBox boshibox;public JCheckBox shuoshibox;public JCheckBox qitabox;ListSelectionModel listSelectionModel;CardLayout mycard; /用于切換愛好和學歷卡片/定義默認的list模型DefaultListModel defaultListModel = new DefaultListModel(); /* * Launch the application. */public st

14、atic void main(String args) EventQueue.invokeLater(new Runnable() public void run() try card frame = new card();frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the frame. */窗體的構(gòu)造函數(shù),用于設(shè)定相關(guān)組件并且添加事件,合理安排組建的位置public card() setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setB

15、ounds(100, 100, 464, 338);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);setContentPane(contentPane);contentPane.setLayout(null);JPanel panel = new JPanel();panel.setBounds(0, 0, 459, 316);contentPane.add(panel);panel.setLayout(null);Box horizontalBox = Box.createHorizo

16、ntalBox();horizontalBox.setBounds(131, 203, 121, -41);panel.add(horizontalBox);Box horizontalBox_1 = Box.createHorizontalBox();horizontalBox_1.setBounds(0, 0, 1, 1);panel.add(horizontalBox_1);JPanel panel_1 = new JPanel();panel_1.setBounds(16, 27, 166, 66);panel_1.setBorder(BorderFactory.createLower

17、edSoftBevelBorder();panel.add(panel_1);rdbtnNewRadioButton_1 = new JRadioButton(查看已有名片);rdbtnNewRadioButton_1.setSelected(true);panel_1.add(rdbtnNewRadioButton_1);rdbtnNewRadioButton_1.addActionListener(new cardseechangelistener();rdbtnNewRadioButton = new JRadioButton(添加新名片);panel_1.add(rdbtnNewRad

18、ioButton);rdbtnNewRadioButton.addActionListener(new cardseechangelistener();ButtonGroup group2 = new ButtonGroup();group2.add(rdbtnNewRadioButton_1);group2.add(rdbtnNewRadioButton);JPanel panel_2 = new JPanel();panel_2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null);panel_

19、2.setBounds(194, 27, 248, 182);panel.add(panel_2);panel_2.setLayout(null);JLabel lblNewLabel = new JLabel(姓名);lblNewLabel.setBounds(34, 13, 61, 16);panel_2.add(lblNewLabel);nametext = new JTextField();nametext.setBounds(100, 8, 130, 26);panel_2.add(nametext);nametext.setColumns(10);address1test = ne

20、w JTextField();address1test.setColumns(10);address1test.setBounds(100, 36, 130, 26);panel_2.add(address1test);JLabel label = new JLabel(地址);label.setBounds(34, 41, 61, 16);panel_2.add(label);address2test = new JTextField();address2test.setColumns(10);address2test.setBounds(100, 64, 130, 26);panel_2.

21、add(address2test);JLabel label_1 = new JLabel(聯(lián)系地址);label_1.setBounds(28, 69, 61, 16);panel_2.add(label_1);JLabel label_2 = new JLabel(電子郵件);label_2.setBounds(28, 97, 61, 16);panel_2.add(label_2);emailtest = new JTextField();emailtest.setColumns(10);emailtest.setBounds(100, 92, 130, 26);panel_2.add(

22、emailtest);rdbtnNewRadioButton_2 = new JRadioButton(愛好);rdbtnNewRadioButton_2.setBounds(18, 125, 61, 16);panel_2.add(rdbtnNewRadioButton_2);rdbtnNewRadioButton_2.addActionListener(new cardchangelistener();rdbtnNewRadioButton_3 = new JRadioButton(學歷);rdbtnNewRadioButton_3.setBounds(18, 153, 61, 16);p

23、anel_2.add(rdbtnNewRadioButton_3);rdbtnNewRadioButton_3.addActionListener(new cardchangelistener();ButtonGroup group1 = new ButtonGroup();group1.add(rdbtnNewRadioButton_2);group1.add(rdbtnNewRadioButton_3);addmessagebutton = new JButton(添加);addmessagebutton.addActionListener(new addmessagebuttonlist

24、ener();addmessagebutton.setBounds(123, 121, 85, 27);panel_2.add(addmessagebutton);/清空按鈕,點擊時觸發(fā)清空的事件clearbutton = new JButton(清空);clearbutton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(e.getSource() = clearbutton)singbox.setSelected(false);dancebox.setSelected

25、(false);webbox.setSelected(false);footballbox.setSelected(false);basketballbox.setSelected(false);volleyballbox.setSelected(false);xueshibox.setSelected(false);shuoshibox.setSelected(false);boshibox.setSelected(false);qitabox.setSelected(false);nametext.setText();address1test.setText();address2test.

26、setText();emailtest.setText(););clearbutton.setBounds(123, 149, 85, 27);panel_2.add(clearbutton);clearbutton.setEnabled(false);addmessagebutton.setEnabled(false);JPanel panel_3 = new JPanel();panel_3.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null);panel_3.setBounds(16, 123

27、, 166, 187);panel.add(panel_3);panel_3.setLayout(null);list = new JList();list.setModel(defaultListModel);list.setBounds(19, 82, 85, 87);listSelectionModel = list.getSelectionModel();listSelectionModel.addListSelectionListener( new SharedListSelectionHandler();JScrollPane scrollPane = new JScrollPan

28、e(list);scrollPane.setBounds(6, 6, 154, 175);panel_3.add(scrollPane);panel_4 = new JPanel();panel_4.setBounds(194, 232, 248, 78);panel.add(panel_4);mycard = new CardLayout();panel_4.setLayout(mycard);JPanel panel_5 = new JPanel();panel_5.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,

29、 null, null);panel_4.add(panel_5);panel_5.setLayout(null);singbox = new JCheckBox(唱歌);singbox.setBounds(6, 16, 66, 23);panel_5.add(singbox);dancebox = new JCheckBox(跳舞);dancebox.setBounds(82, 16, 66, 23);panel_5.add(dancebox);webbox = new JCheckBox(上網(wǎng));webbox.setBounds(160, 16, 66, 23);panel_5.add(w

30、ebbox);footballbox = new JCheckBox(足球);footballbox.setBounds(6, 41, 66, 23);panel_5.add(footballbox);basketballbox = new JCheckBox(籃球);basketballbox.setBounds(82, 41, 66, 23);panel_5.add(basketballbox);volleyballbox = new JCheckBox(排球);volleyballbox.setBounds(160, 41, 66, 23);panel_5.add(volleyballb

31、ox);JPanel panel_6 = new JPanel();panel_6.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null);panel_4.add(panel_6);panel_6.setLayout(null);xueshibox = new JCheckBox(學士);xueshibox.setBounds(16, 16, 75, 23);panel_6.add(xueshibox); boshibox = new JCheckBox(博士);boshibox.setBounds(

32、16, 49, 83, 23);panel_6.add(boshibox);shuoshibox = new JCheckBox(碩士);shuoshibox.setBounds(111, 16, 75, 23);panel_6.add(shuoshibox);qitabox = new JCheckBox(其他);qitabox.setBounds(111, 49, 111, 23);panel_6.add(qitabox);nametext.setEnabled(false);address1test.setEnabled(false);address2test.setEnabled(fa

33、lse);emailtest.setEnabled(false);JLabel lblNewLabel_1 = new JLabel(選擇動作);lblNewLabel_1.setBounds(16, 6, 61, 16);panel.add(lblNewLabel_1);JLabel lblNewLabel_2 = new JLabel(名片詳細信息);lblNewLabel_2.setBounds(194, 6, 84, 16);panel.add(lblNewLabel_2);JLabel lblNewLabel_3 = new JLabel(名片附加信息);lblNewLabel_3.

34、setBounds(191, 214, 87, 16);panel.add(lblNewLabel_3);JLabel lblNewLabel_4 = new JLabel(名片列表);lblNewLabel_4.setBounds(16, 105, 61, 16);panel.add(lblNewLabel_4);public ArrayList items = new ArrayList();/當點擊添加按鈕時,將文本編輯框及多選框中的信息保存到新建的personinf類中/并且將每個新建的personinf放到Arraylist中,只將名字顯示到list中class addmessage

35、buttonlistener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = addmessagebutton)System.out.println(you add addmessagebutton); String dance = null; String sing = null; String web = null; String football = null; String bas

36、ketball = null; String volleyball = null; String xueshi = null; String shuoshi = null; String boshi = null; String qita = null;if(singbox.isSelected() sing = singbox.getText();if(dancebox.isSelected() dance = dancebox.getText();if(webbox.isSelected() web = webbox.getText();if(footballbox.isSelected(

37、) football = footballbox.getText();if(basketballbox.isSelected()basketball = basketballbox.getText();if(volleyballbox.isSelected() volleyball = volleyballbox.getText();if(xueshibox.isSelected()xueshi = xueshibox.getText();if(shuoshibox.isSelected()shuoshi = shuoshibox.getText();if(boshibox.isSelecte

38、d()boshi = boshibox.getText();if(qitabox.isSelected()qita = qitabox.getText();PersonInf p1 = new PersonInf(nametext.getText(),address1test.getText(),address2test.getText(),emailtest.getText(),sing,dance,web,football,basketball,volleyball,xueshi,shuoshi,boshi,qita);items.add(p1);defaultListModel.addE

39、lement(名字為:+p1.yourname);/此處用于判斷是選擇愛好卡片還是學歷卡片class cardchangelistener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stub/如果選擇的是愛好,則顯示愛好的界面if(rdbtnNewRadioButton_2.isSelected()mycard.next(panel_4);/如果是選擇學歷,則顯示學歷的界面if(rdbtnNewRadioButton_3.isS

40、elected()mycard.previous(panel_4);/此處是用與判斷名片的兩種狀態(tài),如果是查看已有名片,則將可編輯區(qū)的屬性設(shè)置為不可編輯/如果是添加現(xiàn)有名片,則可以編輯class cardseechangelistener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = rdbtnNewRadioButton)nametext.setEnabled(true);addres

41、s1test.setEnabled(true);address2test.setEnabled(true);emailtest.setEnabled(true);addmessagebutton.setEnabled(true);clearbutton.setEnabled(true);rdbtnNewRadioButton_2.setSelected(true);singbox.setEnabled(true);dancebox.setEnabled(true);webbox.setEnabled(true);footballbox.setEnabled(true);basketballbo

42、x.setEnabled(true);volleyballbox.setEnabled(true);xueshibox.setEnabled(true);boshibox.setEnabled(true);shuoshibox.setEnabled(true);qitabox.setEnabled(true);if(e.getSource() = rdbtnNewRadioButton_1)nametext.setEnabled(false);address1test.setEnabled(false);address2test.setEnabled(false);emailtest.setE

43、nabled(false);addmessagebutton.setEnabled(false);clearbutton.setEnabled(false);singbox.setSelected(false);dancebox.setSelected(false);webbox.setSelected(false);footballbox.setSelected(false);basketballbox.setSelected(false);volleyballbox.setSelected(false);xueshibox.setSelected(false);shuoshibox.set

44、Selected(false);boshibox.setSelected(false);qitabox.setSelected(false);singbox.setEnabled(false);dancebox.setEnabled(false);webbox.setEnabled(false);footballbox.setEnabled(false);basketballbox.setEnabled(false);volleyballbox.setEnabled(false);xueshibox.setEnabled(false);boshibox.setEnabled(false);sh

45、uoshibox.setEnabled(false);qitabox.setEnabled(false);/list中的事件處理器,當點擊list中的選項時,將類信息顯示到文本框中/與radiobutton上,此時處理不可編輯狀態(tài)class SharedListSelectionHandler implements ListSelectionListenerOverridepublic void valueChanged(ListSelectionEvent e) / TODO Auto-generated method stub/將學歷及愛好的編輯狀態(tài)設(shè)為不可編輯singbox.setSel

46、ected(false);dancebox.setSelected(false);webbox.setSelected(false);footballbox.setSelected(false);basketballbox.setSelected(false);volleyballbox.setSelected(false);xueshibox.setSelected(false);shuoshibox.setSelected(false);boshibox.setSelected(false);qitabox.setSelected(false);/獲取list的index值int k =

47、0;k = list.getSelectedIndex();/創(chuàng)建PersonInf空類,用于存儲從arraylist中獲取的類對象PersonInf px = null;px = items.get(k);/將此類的信息顯示到相應(yīng)的組件上nametext.setText(px.yourname);address1test.setText(px.address1);address2test.setText(px.address2);emailtest.setText(px.email);if(px.sing != null)singbox.setSelected(true);if(px.dan

48、ce != null)dancebox.setSelected(true);if(px.web != null)webbox.setSelected(true);if(px.football != null)footballbox.setSelected(true);if(px.basketball != null)basketballbox.setSelected(true);if(px.volleyball != null)volleyballbox.setSelected(true);if(px.xueshi != null)xueshibox.setSelected(true);if(

49、px.shuoshi != null)shuoshibox.setSelected(true);if(px.boshi != null)boshibox.setSelected(true);if(px.qita != null)qitabox.setSelected(true);(2)import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Date;import java.text.SimpleDateFormat;public class Notepad extends JFrameJMenuBar menub=new JMenuBar();JTextArea text=new JTextArea();JMenu files=new JMenu(文件(F);JMenu edit=new JMenu(編輯(E);JMenu formats=ne

溫馨提示

  • 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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論