欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

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

    • 资源ID:14456709       资源大小:674KB        全文页数:26页
    • 资源格式: DOC        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    精选优质文档-倾情为你奉上 实 验 报 告课程名称: JAVA程序设计 学 生 姓 名: 孙文浩 班 级 计算机141 学 号 学 院(系): 信息数理学院 指 导 教 师: 吕猛 实验时间: 2016 年 12 月 6 日 实验名称: 实验五 基于Swing的图形用户界面开发实验 专心-专注-专业实验五 基于Swing的图形用户界面开发实验一 实验内容(1)完成开发界面如图所示,实现如下功能选中“查看已有名片”按钮,程序处于查看状态,可以在“名片列表”中选择要查看的名片,程序在“名片详实信息”栏显示该名片的详实信息,此状态中名片信息处于不可编辑状态选中“添加新名片”按钮,程序处于添加状态,所有关于明天信息的项目都处于可编辑状态,设置完成相关信息后,单击“添加”按钮,在“名片列表”中出现新添加名片的名称。单击“清空”按钮可以清空还没提交的内容在查看状态和添加状态下,都可以通过点击“爱好”、“学历”单选按钮以显示名片的不同附加信息(备注:名片附加信息部分,当点击“爱好”按钮时显示的如图1所示,当点击“学历”按钮时显示如图2所示。)图1图2(2)记事本设计与实现如图所示(1)编程实现如图所示的界面(2)实现文件、编辑、格式、查看,帮助菜单(3)实现记事本的基本功能二 实验目的(1) 理解组件的分类(2) 掌握容器、布局管理器、普通组件的使用(3) 理解事件处理机制的工作原理(4) 掌握事件的添加和功能代码的编写方法三 实验过程(1) 问题分析答:名片的设计除了使用最基本的组件,如JButton,Jlist,JCheckBox, JTextField, JRadioButton, CardLayout以外,最主要的是逻辑事件的编写,在成功布局完见面后,就需要编写事件处理。首先,由于是名片信息,保存着一个人的基本信息,所以可以用一个类来表示,如PersonInf类,其次,由于是多个人的信息,可以通过ArrayList来存放类;其次就是当点击按钮的时候,正确添加和显示信息,此处用到cardLayout布局,可以减少界面的复杂度,在使用JList的时候,除了加滚动条意外,还需要listModel获取数据等。(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;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 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 = 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.swing.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.swing.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.swing.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 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;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 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. */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_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.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.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(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, 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);address1test = 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, 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);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, 153, 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 addmessagebuttonlistener();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.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("");address2test.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, 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 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, 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);panel_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(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("博士");boshibox.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.setEnabled(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("名片附加信息");lblNewLabel_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 <PersonInf> items = new ArrayList<>();/当点击添加按钮时,将文本编辑框及多选框中的信息保存到新建的personinf类中/并且将每个新建的personinf放到Arraylist中,只将名字显示到list中class 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; 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.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(boshibox.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);defaultListModel.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(rdbtnNewRadioButton_3.isSelected()mycard.previous(panel_4);/此处是用与判断名片的两种状态,如果是查看已有名片,则将可编辑区的属性设置为不可编辑/如果是添加现有名片,则可以编辑class cardseechangelistener implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = rdbtnNewRadioButton)nametext.setEnabled(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);

    注意事项

    本文(java程序设计实验五(共26页).doc)为本站会员(飞****2)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

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

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

    收起
    展开