📄 notepad.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.StyledDocument;
import java.net.URL;
//import javax.swing.ImageIcon;
public class Notepad extends JFrame{
private final Color colorvalues[] = { Color.black, Color.blue, Color.red, Color.green }; //定义颜色数组
private final Color backcolor[] = { Color.white, Color.GRAY, Color.RED, Color.BLUE ,Color.ORANGE,Color.GREEN}; //定义颜色数组
String styleNames[] = { "Bold", "Italic" };//定义风格数组
String filepath="";
String fontNames[] = { "宋体", "华文行楷", "隶书" };//字体数组
String[] sizeString = new String[30];//字号数组
String colors[] = { "Black", "Blue", "Red", "Green" };
String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" };
int[] size = new int[30];//与字号数组对应的字号整数,用于设置文字大小
private JRadioButtonMenuItem colorItems[], fonts[];
private JCheckBoxMenuItem styleItems[];
private JTextPane displayText;//定义文本编辑区
private ButtonGroup fontGroup, colorGroup;//字体组,跟字色组
private int style;//字体风格
private JScrollPane scroll;//为文本编辑区提供滚动条
private String selectText = "";//存放文本编辑区中选中的文本内容
private JComboBox styleBox,fontBox,sizeBox,fontColor;//工具栏
private JPanel pane;
private JTextPane text = null;
private JComboBox fontBackColor = null; // 文字背景颜色
private Box box = null; // 放输入组件的容器
private StyledDocument doc = null;
private JButton b_insert = null, b_remove = null, b_icon = null,b_link=null; // 插入按钮;清除按钮;插入图片按钮
// set up GUI
public Notepad()
{
super( "记事本" );//标题
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
text = new JTextPane();
displayText = new JTextPane();
text.setEditable(false); // 不可录入
JScrollPane scrollPane = new JScrollPane(text);
fontBox = new JComboBox(fontNames); // 字体名称
fontColor = new JComboBox( colors); // 颜色
fontBackColor = new JComboBox(str_BackColor); // 背景颜色
b_insert = new JButton("发布"); // 插入
b_icon = new JButton("图片"); // 插入图片
b_link=new JButton("超链接");
//发布到外面
b_insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setFont( new Font( displayText.getFont().getName(),
displayText.getFont().getStyle(), displayText.getFont().getSize() ) );
Document docm=text.getStyledDocument();
SimpleAttributeSet attrset = new SimpleAttributeSet();
text.setForeground(displayText.getForeground());
insert(displayText.getText(), attrset,docm);
if(!filepath.equals(""))
{
text.setCaretPosition(docm.getLength()); // 设置插入位置
text.insertIcon(new ImageIcon(filepath)); // 插入图片
insert("",new SimpleAttributeSet(),docm);// 这样做可以换行
filepath="";
}
displayText.setText("");
}
});
box = Box.createVerticalBox(); // 竖结构
Box box_1 = Box.createHorizontalBox(); // 横结构
Box box_2 = Box.createHorizontalBox(); // 横结构
box.add(box_1);
box.add(Box.createVerticalStrut(8)); // 两行的间距
box.add(box_2);
box.setBorder(BorderFactory.createEmptyBorder(8, 8, 16, 16)); // 8个的边距
//初始化字体大小数组
for(int i = 0 ; i<size.length;i++)
{
sizeString[i] = "" + (i+5) * 2;
size[i] = (i+5)*2;
}
fontBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
if( event.getStateChange() == ItemEvent.SELECTED){
displayText.setFont( new Font( fontNames[fontBox.getSelectedIndex()],
displayText.getFont().getStyle(), displayText.getFont().getSize() ) );
}
}
});
fontBackColor.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
displayText.setBackground(backcolor[fontBackColor.getSelectedIndex()]);
}
}
);
fontColor.addItemListener( new ItemListener(){
public void itemStateChanged(ItemEvent event){
displayText.setForeground(colorvalues[fontColor.getSelectedIndex()]);
}
}
);
String style_name[] = {"常规","倾斜","粗体","倾斜加粗体"};//字体风格
styleBox = new JComboBox(style_name);
styleBox.addItemListener( new ItemListener(){
public void itemStateChanged(ItemEvent event){
if( event.getStateChange() == ItemEvent.SELECTED){
if(styleBox.getSelectedIndex()==0) style = Font.PLAIN;
if(styleBox.getSelectedIndex()==1) style = Font.ITALIC;
if(styleBox.getSelectedIndex()==2) style = Font.BOLD;
if(styleBox.getSelectedIndex()==3) style = Font.ITALIC+Font.BOLD;
displayText.setFont( new Font( displayText.getFont().getName(),
style, displayText.getFont().getSize() ) );
}
}
});
sizeBox = new JComboBox(sizeString);
sizeBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
if( event.getStateChange() == ItemEvent.SELECTED){
displayText.setFont( new Font( displayText.getFont().getName(),
displayText.getFont().getStyle(), size[sizeBox.getSelectedIndex()] ) );
}
}
} );
// 插入图片
b_icon.addActionListener(new ActionListener() { // 插入图片事件
public void actionPerformed(ActionEvent arg0) {
JFileChooser f = new JFileChooser(); // 查找文件
f.showOpenDialog(null);
filepath=f.getSelectedFile().getPath();
insertIcon(f.getSelectedFile()); // 插入图片
}
});
b_link.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel swapPanel = new JPanel();
JLabel lookupLabel = new JLabel("请输入网址:");
JTextField inputText = new JTextField(20);
swapPanel.add( lookupLabel );
swapPanel.add( inputText );
JOptionPane.showMessageDialog(null,swapPanel);
String t = inputText.getText();//获得整个文本内容
try{
URL address=new URL(t);
text.setPage(address);
}catch(MalformedURLException e){
System.out.println("Malformed URL:"+e);
}catch(IOException e){
System.out.println("IOException:"+e);
}
text.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
try{
text.setPage(e.getURL());
} catch(Exception ex){
ex.printStackTrace();
}
}
}
});
}
});
// set up File menu and its menu items
JMenu fileMenu = new JMenu( "文件(F)" );
fileMenu.setMnemonic( 'F' );
// set up About... menu item
JMenuItem aboutItem = new JMenuItem( "关于(A)..." );
aboutItem.setMnemonic( 'A' );
fileMenu.add( aboutItem );
aboutItem.addActionListener(
new ActionListener() { // anonymous inner class
// display message dialog when user selects About...
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( Notepad.this,
"多功能编辑器——盛文巍,张航,高焕国/n",
"关于", JOptionPane.PLAIN_MESSAGE );
}
} // end anonymous inner class
); // end call to addActionListener
// set up Exit menu item退出菜单
JMenuItem exitItem = new JMenuItem( "Exit" );
exitItem.setMnemonic( 'x' );
fileMenu.add( exitItem );
exitItem.addActionListener(
new ActionListener() { // anonymous inner class
// terminate application when user clicks exitItem
public void actionPerformed( ActionEvent event )
{
System.exit( 0 );
}
} // end anonymous inner class
); // end call to addActionListener
// create menu bar and attach it to MenuTest windowg
JMenuBar bar = new JMenuBar();
setJMenuBar( bar );
bar.add( fileMenu );
JMenu editMenu = new JMenu( "编辑(E)" );
editMenu.setMnemonic( 'E' );
//复制菜单选项
JMenuItem copyItem = new JMenuItem( "复制(C)" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -