⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookeditor.java

📁 本程序用Java描述了一个类似windows自带的记事本的功能
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			gotoDialog.setVisible(true);                 //设置对话框为可见

		}

		// 全选
		else if (e.getSource() == rightclickMenu_SelectAll || e.getSource() == mEdit_SelectAll) {
			Text.selectAll();                             //该方法即为选中全文
		}

		// 自动换行
		else if (e.getSource() == formatMenu_LineWrap) {
			if (formatMenu_LineWrap.getState()) {
				Text.setLineWrap(true);                  //该方法即为设置自动换行(true)
			} else
				Text.setLineWrap(false);                 //该方法即为关闭自动换行(false)
		}

		// 字体设置
		else if (e.getSource() == formatMenu_Font || e.getSource() == rightclickMenu_Font){
			Text.requestFocus();
			new MyFont();
		}

		// 设置字体颜色(前景色)
		else if (e.getSource() == formatMenu_Color_FgColor || e.getSource() == rightclickMenu_Color_FgColor){ 
			Text.requestFocus();                      //JColorChooser显示有模式的颜色选取器,在隐藏对话框之前一直阻塞
			Color color = JColorChooser.showDialog(this, "更改字体颜色", Color.black);
			if (color != null) {
				Text.setForeground(color);                         //setForeground即为设置字体颜色(前景色)
			} else
				return;
		}

		// 设置编辑区背景颜色
		else if (e.getSource() == formatMenu_Color_BgColor || e.getSource() == rightclickMenu_Color_BgColor){
			Text.requestFocus();
			Color color = JColorChooser.showDialog(this, "更改背景颜色", Color.white);
			if (color != null) {
				Text.setBackground(color);                           //setBackground即为设置背景色
			} else
				return;
		}

		// 设置状态栏可见性
		else if (e.getSource() == viewMenu_Status) {
			if (viewMenu_Status.getState())
				statusBar.setVisible(true);

			else
				statusBar.setVisible(false);

		}

		// 使用声明
		else if (e.getSource() == mHelp_HelpTopics){
			JOptionPane.showMessageDialog(this, "该记事本支持文本读取\n" + 
					"但由于对调试不熟悉\n"+
					"实际操作中可能会遇到不少BUG\n"+
					"请用户见谅", "使用声明", JOptionPane.INFORMATION_MESSAGE);
		}

		// 关于
		else if (e.getSource() == mHelp_About) {
			JOptionPane.showMessageDialog(this, "       简单的记事本\n" + 
					"       拥有基本的使用功能\n" +
					"     JAVA课程大作业之文本编辑器\n"+
					"       07软件3班  韦伟"+
					"      学号 200730556438", "关于记事本", JOptionPane.INFORMATION_MESSAGE);
		}

	}
	// 用于设置字体的类MyFont
	class MyFont implements ActionListener {
		final JDialog fontDialog;                         //定义设置字体对话框
		final JTextField tfFont, tfSize, tfStyle;         //定义字体样式,大小,风格
		final int fontStyleConst[] = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD + Font.ITALIC };//定义字体风格种类
		final JList listStyle, listFont, listSize;       //定义字体样式,大小,风格列表
		JLabel sample;                                   //定义样式字体参考
		JPanel pane1, pane2, pane3, pane4;               //定义对话框输出排版

		// 构造函数MyFont
		public MyFont() {
			fontDialog = new JDialog(Bookeditor.this, "字体设置", true);
			Container con = fontDialog.getContentPane();
			con.setLayout(new BoxLayout(con, BoxLayout.Y_AXIS));
			pane1 = new JPanel();
			pane2 = new JPanel();
			pane3 = new JPanel();
			pane4 = new JPanel();
			Font currentFont = Text.getFont();

			JLabel lblFont = new JLabel("字体:");
			JLabel lblStyle = new JLabel("字形:");
			JLabel lblSize = new JLabel("大小:");

			lblStyle.setHorizontalAlignment(SwingConstants.CENTER);     //将字段放在中央
			lblSize.setHorizontalAlignment(SwingConstants.CENTER);
			lblFont.setPreferredSize(new Dimension(100, 20));           //设置标签大小
			lblStyle.setPreferredSize(new Dimension(120, 20));
			lblSize.setPreferredSize(new Dimension(90, 20));
			tfFont = new JTextField(13);
			tfFont.setText(currentFont.getFontName());
			tfFont.selectAll();
			tfFont.setPreferredSize(new Dimension(200, 20));
			tfStyle = new JTextField(10);
			if (currentFont.getStyle() == Font.PLAIN)
				tfStyle.setText("常规");
			else if (currentFont.getStyle() == Font.BOLD)
				tfStyle.setText("粗体");
			else if (currentFont.getStyle() == Font.ITALIC)
				tfStyle.setText("斜体");
			else if (currentFont.getStyle() == (Font.BOLD + Font.ITALIC))
				tfStyle.setText("粗斜体");

			tfFont.selectAll();
			tfStyle.setPreferredSize(new Dimension(200, 20));
			tfSize = new JTextField(7);
			tfSize.setText(currentFont.getSize() + "");
			tfSize.selectAll();
			tfSize.setPreferredSize(new Dimension(200, 20));

			final String fontStyle[] = { "常规", "粗体", "斜体", "粗斜体" };
			listStyle = new JList(fontStyle);

			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			final String fontName[] = ge.getAvailableFontFamilyNames(); //得到一个包含此本地中所有字体系列名称的数组
			int defaultFontIndex = 0;
			for (int i = 0; i < fontName.length; i++) {
				if (fontName[i].equals(currentFont.getFontName())) {
					defaultFontIndex = i;
					break;
				}
			}
			listFont = new JList(fontName);                           //将得到的字体名全部显示到列表中
			listFont.setSelectedIndex(defaultFontIndex);              //列表框中选中默认的字体
			listFont.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //设置其为单选列表
			listFont.setVisibleRowCount(7);                           //设置列表一次显示7行选项
			listFont.setFixedCellWidth(130);                          //设置列表宽度
			listFont.setFixedCellHeight(20);                          //设置列表单行选项高度
			listFont.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent event) {  //根据选择的列表选项设置响应
					tfFont.setText(fontName[listFont.getSelectedIndex()]);  //选中框的到列表选项
					tfFont.requestFocus();                                  //将选中框设为焦点
					tfFont.selectAll();                                     //同时全部选中它
					updateSample();                            //更新字体示例的属性
				}
			});

			listStyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			if (currentFont.getStyle() == Font.PLAIN)
				listStyle.setSelectedIndex(0);
			else if (currentFont.getStyle() == Font.BOLD)
				listStyle.setSelectedIndex(1);
			else if (currentFont.getStyle() == Font.ITALIC)
				listStyle.setSelectedIndex(2);
			else if (currentFont.getStyle() == (Font.BOLD + Font.ITALIC))
				listStyle.setSelectedIndex(3);

			listStyle.setVisibleRowCount(7);                             //这里同以上的Font设定
			listStyle.setFixedCellWidth(110);
			listStyle.setFixedCellHeight(20);
			listStyle.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent event) {
					tfStyle.setText(fontStyle[listStyle.getSelectedIndex()]);
					tfStyle.requestFocus();
					tfStyle.selectAll();
					updateSample();                            //更新字体示例的属性
				}
			});

			final String fontSize[] = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" };
			listSize = new JList(fontSize);
			int defaultFontSizeIndex = 0;
			for (int i = 0; i < fontSize.length; i++) {
				if (fontSize[i].equals(currentFont.getSize() + "")) {
					defaultFontSizeIndex = i;
					break;
				}
			}
			listSize.setSelectedIndex(defaultFontSizeIndex);

			listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listSize.setVisibleRowCount(7);
			listSize.setFixedCellWidth(60);
			listSize.setFixedCellHeight(20);
			listSize.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent event) {
					tfSize.setText(fontSize[listSize.getSelectedIndex()]);
					tfSize.requestFocus();
					tfSize.selectAll();
					updateSample();                            //更新字体示例的属性
				}
			});
			fontOkButton = new JButton("确定");
			fontOkButton.addActionListener(this);
			JButton cancelButton = new JButton("取消");
			cancelButton.addActionListener(new ActionListener() {    //如果选择“取消”则撤销该窗口
				public void actionPerformed(ActionEvent e) {
					fontDialog.dispose();
				}
			});

			sample = new JLabel("小兵");
			sample.setHorizontalAlignment(SwingConstants.CENTER);
			sample.setPreferredSize(new Dimension(220, 100));

			JPanel samplePanel = new JPanel();
			samplePanel.setBorder(BorderFactory.createTitledBorder("字型示例"));
			samplePanel.add(sample);

			pane1.add(lblFont);
			pane1.add(lblStyle);
			pane1.add(lblSize);
			pane2.add(tfFont);
			pane2.add(tfStyle);
			pane2.add(tfSize);
			pane3.add(new JScrollPane(listFont));
			pane3.add(new JScrollPane(listStyle));
			pane3.add(new JScrollPane(listSize));
			pane4.add(samplePanel);
			pane4.add(fontOkButton);
			pane4.add(cancelButton);
			con.add(pane1);
			con.add(pane2);
			con.add(pane3);
			con.add(pane4);
			updateSample();                               //更新字体示例的属性

			fontDialog.pack();                            //调整此窗口的大小,以适合其子组件的首选大小和布局
			fontDialog.setLocation(200, 200);             //设置窗口左上显示坐标
			fontDialog.setResizable(false);              //设置该窗口不可改变大小
			fontDialog.setVisible(true);                 //设置该窗口可显示
		}

		// 更新示例显示的字体和风格大小等
		public void updateSample() {
			Font sampleFont = new Font(tfFont.getText(), fontStyleConst[listStyle.getSelectedIndex()], Integer.parseInt(tfSize.getText()));
			sample.setFont(sampleFont);
		}

		// 设置文本编辑区的字体
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == fontOkButton) {
				Font tempFont = new Font(tfFont.getText(), fontStyleConst[listStyle.getSelectedIndex()], Integer.parseInt(tfSize.getText()));
				Text.setFont(tempFont);
				fontDialog.dispose();
			}
		}
	}
	// 实现了接口UndoableListener的类UndoHandler
	class UndoHandler implements UndoableEditListener {
		public void undoableEditHappened(UndoableEditEvent uee) {
			undo.addEdit(uee.getEdit());
		}
	}

	public static void main(String[] args){
		Text = new JTextArea();
		new Bookeditor();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -