C H ANG Z H OU U N I V E RSITY实验报告课程名称:JAVA程序设计学生姓名:文浩班级计算机141 学号14416122学院(系):信息数理学院指导教师:吕猛实验时间: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 ponent;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 ActionListener{Overridepublic 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 ActionListener{Overridepublic 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 ActionListener{Overridepublic 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);}if(e.getSource() == rdbtnNewRadioButton_1) {nametext.setEnabled(false);address1test.setEnabled(false);address2test.setEnabled(false);emailtest.setEnabled(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.setSelected(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);shuoshibox.setEnabled(false);qitabox.setEnabled(false);}}}//list中的事件处理器,当点击list中的选项时,将类信息显示到文本框中//与radiobutton上,此时处理不可编辑状态class SharedListSelectionHandler implements ListSelectionListener{Overridepublic void valueChanged(ListSelectionEvent e) {// TODO Auto-generated method stub//将学历及爱好的编辑状态设为不可编辑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);//获取list的index值int k = 0;k = list.getSelectedIndex();//创建PersonInf空类,用于存储从arraylist中获取的类对象PersonInf px = null;px = items.get(k);//将此类的信息显示到相应的组件上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.dance != 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(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 JFrame{JMenuBar menub=new JMenuBar();JTextArea text=new JTextArea();JMenu files=new JMenu("文件(F)");JMenu edit=new JMenu("编辑(E)");JMenu formats=new JMenu("格式(O)");JMenu look=new JMenu("查看(V)");JMenu help=new JMenu("帮助(H)");JMenuItem newFile=new JMenuItem("新建(N)");JMenuItem open=new JMenuItem("打开(O)...");JMenuItem save=new JMenuItem("保存(S)");JMenuItem saveAs=new JMenuItem("另存为(A)...");JMenuItem page=new JMenuItem("页面设置(U)...");JMenuItem print=new JMenuItem("打印(P)...");JMenuItem exit=new JMenuItem("退出(X)");JMenuItem undo=new JMenuItem("撤销(U)");JMenuItem cut=new JMenuItem("剪切(T)");JMenuItem copy=new JMenuItem("复制(C)");JMenuItem paste=new JMenuItem("粘贴(P)");JMenuItem selectAll=new JMenuItem("全选(A)");JMenuItem timeDate=new JMenuItem("时间/日期(D)"); JCheckBoxMenuItem lineWrap=new JCheckBoxMenuItem("自动换行(M)");JMenuItem fonts=new JMenuItem("字体...");JMenuItem state=new JMenuItem("状态栏(S)..."); JMenuItem lookhelp=new JMenuItem("查看帮助(H)"); JMenuItem about=new JMenuItem("关于记事本(A)"); JFrame th=this;String name;String openedPath=null;boolean opened=false;boolean reworked=false;//初始化窗体Notepad(String name){super(name);=name;int x,y;Dimension size=Toolkit.getDefaultToolkit().getScreenSize(); x=(size.width-600)/2;y=(size.height-400)/2;setSize(600,400);setLocation(x,y); //让程序界面显示在屏幕中央setMinimumSize(new Dimension(250,150)); setDefaultCloseOperation(EXIT_ON_CLOSE);}//初始化布局void init(){//为控件添加助记符files.setMnemonic('F');edit.setMnemonic('E');formats.setMnemonic('O');help.setMnemonic('H');newFile.setMnemonic('N');open.setMnemonic('O');save.setMnemonic('S');saveAs.setMnemonic('A');page.setMnemonic('U');exit.setMnemonic('X');undo.setMnemonic('U');cut.setMnemonic('T');copy.setMnemonic('C');paste.setMnemonic('P');selectAll.setMnemonic('A');timeDate.setMnemonic('D');lineWrap.setMnemonic('M');lookhelp.setMnemonic('H');about.setMnemonic('A');//为控件添加快捷键newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK)); exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK)); cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); timeDate.setAccelerator(KeyStroke.getKeyStroke("F5"));//将菜单项添加到菜单中files.add(newFile);files.add(open);files.add(save);files.add(saveAs);files.add(page);files.add(print);files.addSeparator();files.add(exit);edit.add(undo);edit.addSeparator();edit.add(cut);edit.add(copy);edit.add(paste);edit.addSeparator();edit.add(selectAll);edit.add(timeDate);formats.add(lineWrap);formats.add(fonts);look.add(state);help.add(lookhelp);help.add(about);menub.add(files);menub.add(edit);menub.add(formats);menub.add(look);menub.add(help);setJMenuBar(menub);getContentPane().add(new JScrollPane(text));//为控件添加事件侦听器Listen listen=new Listen();Listen1 listen1=new Listen1();newFile.addActionListener(listen);open.addActionListener(listen);save.addActionListener(listen);saveAs.addActionListener(listen);exit.addActionListener(listen);cut.addActionListener(listen);copy.addActionListener(listen);paste.addActionListener(listen);selectAll.addActionListener(listen); timeDate.addActionListener(listen); lineWrap.addActionListener(listen); lookhelp.addActionListener(listen);about.addActionListener(listen);open.addActionListener(listen1);save.addActionListener(listen1);saveAs.addActionListener(listen1);//暂时没有实现的功能undo.setEnabled(false);fonts.setEnabled(false);state.setEnabled(false);}class Listen implements ActionListener {//实现用于一般操作的事件侦听器public void actionPerformed(ActionEvent e) {Object source=e.getSource();if(source==newFile){text.setText("");th.setTitle(name);openedPath=null;opened=false;}else if(source==exit)System.exit(0);else if(source==selectAll)text.selectAll();else if(source==cut)text.cut();else if(source==copy)text.copy();else if(source==paste)text.paste();else if(source==lineWrap)text.setLineWrap(!text.getLineWrap());else if(source==lookhelp){String message=" ----这是一个帮助---";JOptionPane.showMessageDialog(th,message,"帮助",JOptionPane.PLAIN_MESSAGE); }else if(source==about){String message=" ----这是一个记事本----";JOptionPane.showMessageDialog(th,message,"关于",JOptionPane.PLAIN_MESSAGE); }else if(source==timeDate){Date nowTime=new Date();SimpleDateFormat times=new SimpleDateFormat("HH:mm yyyy-MM-dd");text.insert(times.format(nowTime),text.getCaretPosition());}}}class Listen1 implements ActionListener{//实现用于对文件进行操作的事件侦听器public void actionPerformed(ActionEvent e){Object source=e.getSource();//打开文件事件if(source==open){FileDialog openFile=new FileDialog(th,"打开文件...",FileDialog.LOAD);openFile.setVisible(true);String filePath=openFile.getDirectory()+openFile.getFile();try{FileInputStream fis=new FileInputStream(filePath);byte [] content=new byte[fis.available()];fis.read(content);text.setText(new String(content));text.setCaretPosition(0);if(openFile.getFile()!=null){th.setTitle(openFile.getFile()+name);openedPath=filePath;opened=true;}fis.close();catch(Exception ex){ex.printStackTrace();}opened=true;}//保存及另存为事件else if(source==save||source==saveAs){String savePath=openedPath;if(savePath==null||source==saveAs){FileDialog saveFile=new FileDialog(th,"保存文件...",FileDialog.SAVE);saveFile.setVisible(true);savePath=saveFile.getDirectory()+saveFile.getFile();}try{FileOutputStream fos=new FileOutputStream(savePath);fos.write(text.getText().getBytes());fos.close();}catch(Exception ex){ex.printStackTrace();}if(source==save)openedPath=savePath;}}}public static void main(String[] args){try {UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());}catch (Exception e) {System.err.println("Couldn't use the system look and feel:"+e);}String name=("sun- 记事本");Notepad note=new Notepad(name);note.init();note.setVisible(true);}(3)程序运行和结果分析答:(1)(2)四实验小结经过本次实验,我理解组件的分类,掌握容器、布局管理器、普通组件的使用,理解事件处理机制的工作原理,掌握事件的添加和功能代码的编写方法。