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

📄 notepad.java

📁 java实现的超强记事本
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		fontStyle.setFixedCellHeight(15);
		fontStyle.setFixedCellWidth(120);
		// 以下这步一定要做,打开字体对话框的时候选中的是现在文本所用的字体样式,假如不做这步,会出现异常
		if (text.getFont().getStyle() == Font.PLAIN)
			fontStyle.setSelectedIndex(0);
		else if (text.getFont().getStyle() == Font.BOLD)
			fontStyle.setSelectedIndex(1);
		else if (text.getFont().getStyle() == Font.ITALIC)
			fontStyle.setSelectedIndex(2);
		else if (text.getFont().getStyle() == (Font.BOLD + Font.ITALIC))
			fontStyle.setSelectedIndex(3);
		final JTextField fontstylefield = new JTextField(20);
		fontstylefield.setEditable(false);
		fontstylefield.setText((String) fontStyle.getSelectedValue());
		fontStyle.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent event) {
				fontstylefield.setText(fontStyle.getSelectedValue().toString());
				preview();
			}
		});
		// 显示选择结果
		// 前景色的设置采用颜色选择器
		fontcolorchooser = new JButton("点此选择");
		fontcolorchooser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				forecolor = JColorChooser.showDialog(null, "前景色", Color.BLACK);
				// 将forecolor设值,以便在下面更改文本前景色时使用
				if (forecolor == null)
					forecolor = Color.BLACK;
				fontPreviewLabel.setForeground(forecolor);
			}
		});
		fontPreviewLabel.setSize(20, 20);
		// 居中显示
		fontPreviewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		fontPreviewLabel.setBackground(Color.WHITE);
		fontPreviewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		fontPreviewLabel.setForeground(Color.BLACK);
		// 重要的一步,在组件上要显示颜色,一般来说要设置成透明,否则出不来效果
		fontPreviewLabel.setOpaque(true);
		fontbackcolorchooser = new JButton("点此选择");
		fontbackcolorchooser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 设置字体背景色,基本同上面的前景色设置
				backcolor = JColorChooser.showDialog(null, "背景色", Color.BLACK);
				if (backcolor == null)
					backcolor = Color.BLACK;
				fontPreviewLabel.setBackground(backcolor);
			}
		});
		// 以下是添加组件,画界面
		JPanel panel_all = new JPanel();
		panel_all.setLayout(new GridLayout(2, 3));
		JPanel panel_name = new JPanel();
		JPanel panel_size = new JPanel();
		JPanel panel_style = new JPanel();
		JPanel panel_color = new JPanel();
		JPanel panel_backcolor = new JPanel();
		JPanel panel_fontcolor = new JPanel();
		JPanel panel_preview = new JPanel();
		panel_preview.setLayout(new BorderLayout());
		panel_fontcolor.setLayout(new GridLayout(2, 1));
		JPanel panel_confirm = new JPanel(new BorderLayout());
		JLabel tiplabel = new JLabel("注意:确定前先选定文本", SwingConstants.CENTER);
		Box box = Box.createHorizontalBox();
		panel_name.setBorder(BorderFactory.createTitledBorder("字体"));
		panel_name.add(fontnamefield);
		panel_name.add(new JScrollPane(fontName));
		panel_size.setBorder(BorderFactory.createTitledBorder("字体大小"));
		panel_size.add(fontsizefield);
		panel_size.add(new JScrollPane(fontSize));
		panel_style.setBorder(BorderFactory.createTitledBorder("字体样式"));
		panel_style.add(fontstylefield);
		panel_style.add(new JScrollPane(fontStyle));
		panel_color.setBorder(BorderFactory.createTitledBorder("字体颜色"));
		panel_color.add(fontcolorchooser);
		panel_backcolor.setBorder(BorderFactory.createTitledBorder("字体背景"));
		panel_backcolor.add(fontbackcolorchooser);
		panel_fontcolor.add(panel_color);
		panel_fontcolor.add(panel_backcolor);
		panel_preview.setBorder(BorderFactory.createTitledBorder("效果预览"));
		panel_preview.add(fontPreviewLabel, BorderLayout.CENTER);
		box.add(ok);
		box.add(Box.createHorizontalStrut(30));
		box.add(cancel);
		panel_confirm.setBorder(BorderFactory.createTitledBorder("保存设置"));
		panel_confirm.add(tiplabel, BorderLayout.NORTH);
		panel_confirm.add(box, BorderLayout.CENTER);
		panel_all.add(panel_name);
		panel_all.add(panel_size);
		panel_all.add(panel_style);
		panel_all.add(panel_fontcolor);
		panel_all.add(panel_preview);
		panel_all.add(panel_confirm);
		c.add(panel_all);
		fontDia.show();
	}

	/**
	 * 更新预览框的内容和效果的方法
	 */
	public void preview() {
		int style = Font.PLAIN;
		String temp_style = (String) fontStyle.getSelectedValue();
		if (temp_style.equals("常规")) {
			style = Font.PLAIN;
		} else if (temp_style.equals("粗体")) {
			style = Font.BOLD;
		} else if (temp_style.equals("斜体")) {
			style = Font.ITALIC;
		} else if (temp_style.equals("粗斜体")) {
			style = Font.BOLD + Font.ITALIC;
		}
		fontPreviewLabel.setFont(new Font(fontName.getSelectedValue()
				.toString(), style, Integer.parseInt(fontSize
				.getSelectedValue().toString())));
	}
	public static void main(String[] args) {
		try {
			// 使用Windows的界面风格,更加友好习惯,以下是设置界面为WINDOWS系统的默认界面
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			Font font = new Font("Dialog", Font.PLAIN, 12);
			UIManager.put("MenuBar.font", font);
			UIManager.put("MenuItem.font", font);
			UIManager.put("Menu.font", font);
			UIManager.put("PopupMenu.font", font);
			UIManager.put("ToolBar.font", font);
			UIManager.put("ToolTip.font", font);
			UIManager.put("Label.font", font);
			UIManager.put("ComboBox.font", font);
			UIManager.put("Button.font", font);
			UIManager.put("TitledBorder.font", font);
			UIManager.put("OptionPane.font", font);
			UIManager.put("CheckBox.font", font);
			UIManager.put("Dialog.font", font);
			UIManager.put("Panel.font", font);
		} catch (Exception e) {
		}
		(new notepad()).setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	/**
	 * 获得字体属性的方法,非常非常重要,在JTextPane里面做出JTextArea不能做到的效果,能够局部改变字体的属性
	 * 
	 * @return FontAttrib
	 * 
	 */
	public FontAttrib getFontAttrib() {
		// 创建FontAttrib内部类的对象
		FontAttrib att = new FontAttrib();
		att.setText(text.getSelectedText());
		att.setName((String) fontName.getSelectedValue());
		att.setSize(Integer.parseInt((String) fontSize.getSelectedValue()));
		String temp_style = (String) fontStyle.getSelectedValue();
		if (temp_style.equals("常规")) {
			att.setStyle(FontAttrib.GENERAL);
		} else if (temp_style.equals("粗体")) {
			att.setStyle(FontAttrib.BOLD);
		} else if (temp_style.equals("斜体")) {
			att.setStyle(FontAttrib.ITALIC);
		} else if (temp_style.equals("粗斜体")) {
			att.setStyle(FontAttrib.BOLD_ITALIC);
		}
		att.setColor(forecolor);
		att.setBackColor(backcolor);
		return att;
	}

	/**
	 * FontAttrib内部类,用于存放和获得字体的属性,包括了一个SimpleAttributeSet对象
	 */
	private class FontAttrib {
		public static final int GENERAL = 0; // 常规

		public static final int BOLD = 1; // 粗体

		public static final int ITALIC = 2; // 斜体

		public static final int BOLD_ITALIC = 3; // 粗斜体

		private SimpleAttributeSet attrSet = null; // 属性集

		private String text = null, name = null; // 要输入的文本和字体名称

		private int style = 0, size = 0; // 样式和字号

		private Color color = null, backColor = null; // 文字颜色和背景颜色

		/**
		 * 一个空的构造函数(可当做换行使用)
		 */
		public FontAttrib() {
		}

		/**
		 * 返回属性集的方法
		 * 
		 * @return #SimpleAttributeSet
		 */
		public SimpleAttributeSet getAttrSet() {
			attrSet = new SimpleAttributeSet();
			if (name != null)
				StyleConstants.setFontFamily(attrSet, name);
			if (style == FontAttrib.GENERAL) {
				StyleConstants.setBold(attrSet, false);
				StyleConstants.setItalic(attrSet, false);
			} else if (style == FontAttrib.BOLD) {
				StyleConstants.setBold(attrSet, true);
				StyleConstants.setItalic(attrSet, false);
			} else if (style == FontAttrib.ITALIC) {
				StyleConstants.setBold(attrSet, false);
				StyleConstants.setItalic(attrSet, true);
			} else if (style == FontAttrib.BOLD_ITALIC) {
				StyleConstants.setBold(attrSet, true);
				StyleConstants.setItalic(attrSet, true);
			}
			StyleConstants.setFontSize(attrSet, size);
			if (color != null)
				StyleConstants.setForeground(attrSet, color);
			if (backColor != null)
				StyleConstants.setBackground(attrSet, backColor);
			return attrSet;
		}

		/**
		 * 设置属性集
		 * 
		 * @param attrSet
		 */
		public void setAttrSet(SimpleAttributeSet attrSet) {
			this.attrSet = attrSet;
		}

		public String getText() {
			return this.text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public Color getColor() {
			return color;
		}

		public void setColor(Color color) {
			this.color = color;
		}

		public Color getBackColor() {
			return backColor;
		}

		public void setBackColor(Color backColor) {
			this.backColor = backColor;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getSize() {
			return size;
		}

		public void setSize(int size) {
			this.size = size;
		}

		public int getStyle() {
			return style;
		}

		public void setStyle(int style) {
			this.style = style;
		}
	}

	/**
	 * ActionHandler内部类,专门响应字体设置事件
	 */
	private class ActionHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			try {
				// 取得所选字符串
				String str = text.getSelectedText();
				// 将所选字符串删除
				text.replaceSelection("");
				if (str == null)
					doc.insertString(text.getCaretPosition(), "",
							getFontAttrib().getAttrSet());
				else
					// 将新的字体属性的字符串插入,完成修改
					for (int i = 0; i < str.length(); i++) {
						char c = str.charAt(i);
						doc.insertString(text.getCaretPosition(), String
								.valueOf(c), getFontAttrib().getAttrSet());
					}
			} catch (BadLocationException k) {
				k.printStackTrace();
			}

		}

	}

	public void windowOpened(WindowEvent arg0) {
	}

	/**
	 * 窗口退出时相应方法,条用quitProg()
	 * 
	 * @see #quitProg()
	 */
	public void windowClosing(WindowEvent arg0) {
		quitProg();
	}

	public void windowClosed(WindowEvent arg0) {
	}

	public void windowIconified(WindowEvent arg0) {
	}

	public void windowDeiconified(WindowEvent arg0) {
	}

	public void windowActivated(WindowEvent arg0) {
	}

	public void windowDeactivated(WindowEvent arg0) {
	}

	public void mouseClicked(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

}

/**
 * 
 * 实现文件过滤器功能的类
 * 
 */
class JAVAFileFilter extends FileFilter {// 实现文件过滤

	String ext;

	public JAVAFileFilter(String ext) {
		this.ext = ext;
	}
	/**
	 * 文件后缀名匹配的方法
	 * 
	 * @return boolean
	 * 
	 */
	public boolean accept(File file) {
		if (file.isDirectory())
			return true;

		String fileName = file.getName();
		in

⌨️ 快捷键说明

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