Java基于Swing的图形用户界面开发实验

实验五 基于Swing的图形用户界面开发实验

一 实验内容
(1)完成开发界面如图所示,实现如下功能
选中“查看已有名片”按钮,程序处于查看状态,可以在“名片列表”中选择要查看的名片,程序在“名片详实信息”栏显示该名片的详实信息,此状态中名片信息处于不可编辑状态
选中“添加新名片”按钮,程序处于添加状态,所有关于明天信息的项目都处于可编辑状态,设置完成相关信息后,单击“添加”按钮,在“名片列表”中出现新添加名片的名称。单击“清空”按钮可以清空还没提交的内容
在查看状态和添加状态下,都可以通过点击“爱好”、“学历”单选按钮以显示名片的不同附加信息
(备注:名片附加信息部分,当点击“爱好”按钮时显示的如图1所示,当点击“学历”按钮时显示如图2所示。)

  • Java基于Swing的图形用户界面开发实验_第1张图片
    图1
    Java基于Swing的图形用户界面开发实验_第2张图片

图2

(2)记事本设计与实现
如图所示(1)编程实现如图所示的界面(2)实现文件、编辑、格式、查看,帮助菜单(3)实现记事本的基本功能

二 实验目的
(1)理解组件的分类
(2)掌握容器、布局管理器、普通组件的使用
(3)理解事件处理机制的工作原理
(4)掌握事件的添加和功能代码的编写方法
三 实验过程
(1)问题分析
1.
名片的设计除了使用最基本的组件,如 JButton,Jlist,JCheckBox, JTextField, JRadioButton, CardLayout 以外,最主要的是逻辑事件的编写,在成功布局完见面后, 就需要编写事件处理。首先,由于是名片信息,保存着一个人的基本信息,所以可以 用一个类来表示,如 PersonInf 类,其次,由于是多个人的信息,可以通过 ArrayList 来存放类;其次就是当点击按钮的时候,正确添加和显示信息,此处用到 cardLayout 布局,可以减少界面的复杂度,在使用JList的时候,除了加滚动条意外,还需要listModel 获取数据等。
2.
编程实现如图所示的界面,实现文件、编辑、格式、查看,帮助菜单,实现记事本的基本功能
(2)代码编写

1.
package hh;
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 Message {
     
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 Message(String yourname, String address1, String address2, String
email,String sing, String dance,String web, String football, Stringbasketball,
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;
		}
	}
public class Nith 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();
		public static void main(String[] args) {
     
			EventQueue.invokeLater(new Runnable() {
     
				public void run() {
     
					try {
     
						Nith frame = new Nith();
						frame.setVisible(true);
					}catch (Exception e) {
     
						e.printStackTrace();
					}
				}
			});
		}
	//窗体的构造函数,用于设定相关组件并且添加事件,合理安排组建的位置
	public Nith() {
     
		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 IbINewLabel_1 = new JLabel("选择动作");
			IbINewLabel_1.setBounds(16, 6, 61,16);
			panel.add(IbINewLabel_1);
			JLabel IbINewLabel_2 = new JLabel("名片详细信息");
			IbINewLabel_2.setBounds(194, 6, 84, 16);
			panel.add(IbINewLabel_2);
			JLabel IbINewLabel_3 = new JLabel("名片附加信息"); 
			IbINewLabel_3.setBounds(191, 214, 87,16);
			panel.add(IbINewLabel_3);
			JLabel IbINewLabel_4 = new JLabel("名片列表");
			IbINewLabel_4.setBounds(16, 105, 61,16);
			panel.add(IbINewLabel_4);
		}
		public ArrayList <Message> items = new ArrayList<>();
	//当点击添加按钮时将文本编辑框及多选框中的信息保存到新建的personInf类中
		//并且将每个新建的personInf放到ArrayList中,只将名字显示到list中
	class addmessagebuttonlistener implements ActionListener {
     
		@Override
		public void actionPerformed(ActionEvent e){
     
		// TODO Auto-generated method stub
		if(e.getSource() == addmessagebutton){
     
			System.out.println("you addaddmessagebutton");
			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(xueshibox.isSelected()){
       xueshi = xueshibox.getText();  }
			if(shuoshibox.isSelected()){
      shuoshi = shuoshibox.getText();}
			if(boshibox.isSelected()){
        boshi = boshibox.getText();     }
			if(qitabox.isSelected()){
         qita = qitabox.getText();       }
			if(footballbox.isSelected()){
     football=footballbox.getText();}
			if(basketballbox.isSelected()){
     
basketball=basketballbox.getText();   }
			if(volleyballbox.isSelected()){
     
volleyball = volleyballbox.getText();   }
			Message p1 = new Message(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{
     
		@Override
		public 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{
     
		@Override
public void actionPerformed(ActionEvent e){
     
			// TODO Auto-generated method stub
	  if(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{
     
		@Override
		public 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中获取的类对象
			Message 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.
package ch_5packages;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
@SuppressWarnings("serial")
public class Txt 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 delete = new JMenuItem("删除(L)");
	JMenuItem find = new JMenuItem("查找(F)");
	JMenuItem findnext = new JMenuItem("查找下一个(N)");
	JMenuItem replace = new JMenuItem("替换(R)");
	JMenuItem go = new JMenuItem("转到(G)");
	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;
	Txt(String name) {
     
		super(name);
		this.name = name;
		int x, y;
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		x = (size.width - 700) / 2;
		y = (size.height - 500) / 2;
		setSize(700, 500);
		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');
		delete.setMnemonic('L');
		find.setMnemonic('F');
		findnext.setMnemonic('N');
		replace.setMnemonic('R');
		go.setMnemonic('G');
		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(' '));
		undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 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));
	delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
		find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
		findnext.setAccelerator(KeyStroke.getKeyStroke("F3"));
		replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
		go.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 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.add(delete);
		edit.addSeparator();
		edit.add(find);
		edit.add(findnext);
		edit.add(replace);
		edit.add(go);
		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);
		delete.addActionListener(listen);
		find.addActionListener(listen);
		findnext.addActionListener(listen);
		replace.addActionListener(listen);
		go.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 == delete)
				text.delete();
			else if (source == find)
				text.find();
			else if (source == findnext)
				text.findnext();
			else if (source == replace)
				text.replace();
			else if (source == go)
				text.go();
			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 yyy-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, "fJFT'#...", 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 thesystem look and feel:" + e);
				}
		String name = ("无标题-记事本");
		Txt note = new Txt(name);
		note.init();
		note.setVisible(true); }    }

你可能感兴趣的:(java)