java程序设计实验五(共26页).doc

上传人:飞****2 文档编号:14456709 上传时间:2022-05-04 格式:DOC 页数:26 大小:674KB
返回 下载 相关 举报
java程序设计实验五(共26页).doc_第1页
第1页 / 共26页
java程序设计实验五(共26页).doc_第2页
第2页 / 共26页
点击查看更多>>
资源描述

《java程序设计实验五(共26页).doc》由会员分享,可在线阅读,更多相关《java程序设计实验五(共26页).doc(26页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上 实 验 报 告课程名称: JAVA程序设计 学 生 姓 名: 孙文浩 班 级 计算机141 学 号 学 院(系): 信息数理学院 指 导 教 师: 吕猛 实验时间: 2016 年 12 月 6 日 实验名称: 实验五 基于Swing的图形用户界面开发实验 专心-专注-专业实验五 基于Swing的图形用户界面开发实验一 实验内容(1)完成开发界面如图所示,实现如下功能选中“查看已有名片”按钮,程序处于查看状态,可以在“名片列表”中选择要查看的名片,程序在“名片详实信息”栏显示该名片的详实信息,此状态中名片信息处于不可编辑状态选中“添加新名片”按钮,程序处于添加状态,所

2、有关于明天信息的项目都处于可编辑状态,设置完成相关信息后,单击“添加”按钮,在“名片列表”中出现新添加名片的名称。单击“清空”按钮可以清空还没提交的内容在查看状态和添加状态下,都可以通过点击“爱好”、“学历”单选按钮以显示名片的不同附加信息(备注:名片附加信息部分,当点击“爱好”按钮时显示的如图1所示,当点击“学历”按钮时显示如图2所示。)图1图2(2)记事本设计与实现如图所示(1)编程实现如图所示的界面(2)实现文件、编辑、格式、查看,帮助菜单(3)实现记事本的基本功能二 实验目的(1) 理解组件的分类(2) 掌握容器、布局管理器、普通组件的使用(3) 理解事件处理机制的工作原理(4) 掌握

3、事件的添加和功能代码的编写方法三 实验过程(1) 问题分析答:名片的设计除了使用最基本的组件,如JButton,Jlist,JCheckBox, JTextField, JRadioButton, CardLayout以外,最主要的是逻辑事件的编写,在成功布局完见面后,就需要编写事件处理。首先,由于是名片信息,保存着一个人的基本信息,所以可以用一个类来表示,如PersonInf类,其次,由于是多个人的信息,可以通过ArrayList来存放类;其次就是当点击按钮的时候,正确添加和显示信息,此处用到cardLayout布局,可以减少界面的复杂度,在使用JList的时候,除了加滚动条意外,还需要li

4、stModel获取数据等。(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 volleyba

5、ll = null;public 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

6、 xueshi,String 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 =

7、xueshi;this.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.sw

8、ing.JRadioButton;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.swin

9、g.event.ListSelectionListener;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.swin

10、g.JList;import 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

11、extends JFrame 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;

12、private JRadioButton rdbtnNewRadioButton_3;public JRadioButton rdbtnNewRadioButton_1;public JRadioButton rdbtnNewRadioButton;private JPanel panel_4;/学历和爱好内的多选项public JCheckBox singbox;public JCheckBox dancebox;public JCheckBox webbox;public JCheckBox footballbox;public JCheckBox basketballbox;public

13、 JCheckBox volleyballbox;public JCheckBox xueshibox;public JCheckBox boshibox;public JCheckBox shuoshibox;public JCheckBox qitabox;ListSelectionModel listSelectionModel;CardLayout mycard; /用于切换爱好和学历卡片/定义默认的list模型DefaultListModel defaultListModel = new DefaultListModel(); /* * Launch the application.

14、 */public static 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. */窗体的构造函数,用于设定相关组件并且添加事件,合理安排组建的位置public card() setDefaultCloseOperation(JFrame.EXIT_ON

15、_CLOSE);setBounds(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.

16、createHorizontalBox();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

17、.createLoweredSoftBevelBorder();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

18、(rdbtnNewRadioButton);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,

19、null);panel_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);addre

20、ss1test = new 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,

21、26);panel_2.add(address2test);JLabel label_1 = new JLabel(联系地址);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);

22、panel_2.add(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, 15

23、3, 61, 16);panel_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 addmessa

24、gebuttonlistener();addmessagebutton.setBounds(123, 121, 85, 27);panel_2.add(addmessagebutton);/清空按钮,点击时触发清空的事件clearbutton = new JButton(清空);clearbutton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(e.getSource() = clearbutton)singbox.setSelected(false);dancebox

25、.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);nametext.setText();address1test.setText();a

26、ddress2test.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.setBo

27、unds(16, 123, 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 = ne

28、w JScrollPane(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,

29、 null, null, 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(上网);webbox.setBounds(160, 16, 66, 23);p

30、anel_5.add(webbox);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

31、(volleyballbox);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(博士);boshibo

32、x.setBounds(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.s

33、etEnabled(false);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(名片附加信息);lb

34、lNewLabel_3.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中clas

35、s addmessagebuttonlistener 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

36、; String basketball = 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

37、.isSelected() 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(boshib

38、ox.isSelected()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);defaultLi

39、stModel.addElement(名字为:+p1.yourname);/此处用于判断是选择爱好卡片还是学历卡片class cardchangelistener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stub/如果选择的是爱好,则显示爱好的界面if(rdbtnNewRadioButton_2.isSelected()mycard.next(panel_4);/如果是选择学历,则显示学历的界面if(rdbtnNewRadio

40、Button_3.isSelected()mycard.previous(panel_4);/此处是用与判断名片的两种状态,如果是查看已有名片,则将可编辑区的属性设置为不可编辑/如果是添加现有名片,则可以编辑class cardseechangelistener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = rdbtnNewRadioButton)nametext.setEnabled(

41、true);address1test.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);basketballbox.setEnabled(true);volleyballbox.setEnabled(true);xueshibox.setEnabled(true);boshibox.setEnabled(true);shuoshibox.setEnabled(true);qitabox.setEnabled(true);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁