📄 notepad.java
字号:
String s = editArea.getText();
int pos = 0, t = 0;
while(true) {
pos = s.indexOf('\12', pos);
if(pos == -1)
break;
lineNumber[t++] = pos++;
}
int gt = 1;
try {
gt = Integer.parseInt(linenum.getText());
}
catch(NumberFormatException efe) {
JOptionPane.showMessageDialog(null, "请输入行数!", "提示",JOptionPane.WARNING_MESSAGE);
linenum.requestFocus(true);
return;
}
if(gt < 2 || gt >= totalLine) {
if(gt < 2)
editArea.setCaretPosition(0);
else
editArea.setCaretPosition(s.length());
}
else
editArea.setCaretPosition(lineNumber[gt-2] + 1);
gotoDialog.dispose();
}
});
JButton cancelButton=new JButton("取消");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
gotoDialog.dispose();
}
});
Container con=gotoDialog.getContentPane();
con.setLayout(new FlowLayout());
con.add(gotoLabel);
con.add(linenum);
con.add(okButton);
con.add(cancelButton);
gotoDialog.setSize(200,100);
gotoDialog.setResizable(false);
gotoDialog.setLocation(300,280);
gotoDialog.setVisible(true);
}//"转到"处理结束
//插入时间日期
else if(e.getSource()==editMenu_TimeDate||e.getSource()==timeButton)
{
editArea.requestFocus();
SimpleDateFormat currentDateTime = new SimpleDateFormat("HH:mm yyyy-MM-dd");
editArea.insert(currentDateTime.format(new Date()),editArea.getCaretPosition());
}
//全选
else if(e.getSource()==popupMenu_SelectAll||e.getSource()==editMenu_SelectAll)
{
editArea.selectAll();
}
//自动换行
else if(e.getSource()==formatMenu_LineWrap)
{
if(formatMenu_LineWrap.getState())
{
editArea.setLineWrap(true);
}
else
editArea.setLineWrap(false);
}
//字体设置
else if(e.getSource()==formatMenu_Font||e.getSource()==fontButton)
{
editArea.requestFocus();
new MyFont();
}
//设置字体颜色(前景色)
else if(e.getSource()==formatMenu_Color_FgColor||e.getSource()==fgcolorButton)
{
editArea.requestFocus();
Color color=JColorChooser.showDialog(this,"更改字体颜色",Color.black);
if(color!=null)
{
editArea.setForeground(color);
}
else
return;
}
//设置编辑区背景颜色
else if(e.getSource()==formatMenu_Color_BgColor||e.getSource()==bgcolorButton)
{
editArea.requestFocus();
Color color=JColorChooser.showDialog(this,"更改背景颜色",Color.white);
if(color!=null)
{
editArea.setBackground(color);
}
else
return;
}
//设置状态栏可见性
else if(e.getSource()==viewMenu_Status)
{
if(viewMenu_Status.getState())
statusLabel.setVisible(true);
else
statusLabel.setVisible(false);
}
//帮助主题
else if(e.getSource()==helpMenu_HelpTopics||e.getSource()==helpButton)
{
editArea.requestFocus();
String helpTopicsStr=new String("欢迎使用");
JEditorPane editPane=new JEditorPane("text/html",helpTopicsStr);
editPane.setEditable(false);
editPane.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent hle){
if (hle.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
{
try
{
Process process = Runtime.getRuntime().exec("cmd.exe /c start http://gxbinbin.blog.edu.cn");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
JFrame helpTopicsFrame=new JFrame("帮助主题");
helpTopicsFrame.setContentPane(new JScrollPane(editPane));
helpTopicsFrame.setSize(250,200);
helpTopicsFrame.setLocation(300,300);
helpTopicsFrame.setVisible(true);
}
//关于
else if(e.getSource()==helpMenu_About){
JOptionPane.showMessageDialog(this,
""
,"关于记事本",JOptionPane.INFORMATION_MESSAGE);
}
//工具栏"粗体"按钮事件处理
else if(e.getSource()==boldButton)
{
editArea.requestFocus();
Font tempFont=editArea.getFont();
if(editArea.getFont().getStyle()==Font.PLAIN){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.ITALIC){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD+Font.ITALIC,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.BOLD){
tempFont=new Font(editArea.getFont().getFontName(),Font.PLAIN,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==(Font.BOLD+Font.ITALIC)){
tempFont=new Font(editArea.getFont().getFontName(),Font.ITALIC,editArea.getFont().getSize());
}
editArea.setFont(tempFont);
}
//工具栏"斜体"按钮事件处理
else if(e.getSource()==italicButton)
{
editArea.requestFocus();
Font tempFont=editArea.getFont();
if(editArea.getFont().getStyle()==Font.PLAIN){
tempFont=new Font(editArea.getFont().getFontName(),Font.ITALIC,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.ITALIC){
tempFont=new Font(editArea.getFont().getFontName(),Font.PLAIN,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.BOLD){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD+Font.ITALIC,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==(Font.BOLD+Font.ITALIC)){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD,editArea.getFont().getSize());
}
editArea.setFont(tempFont);
}
}/*方法actionPerformed()结束*/
//实现DocumentListener接口中的方法
public void removeUpdate(DocumentEvent e) {
editMenu_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
editMenu_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
editMenu_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
//End of DocumentListener
//main 方法
public static void main(String args[])
{
//设置界面为WindowsLookAndFeel
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e) {
}
Notepad frame = new Notepad();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}/*main 方法结束*/
//实现了接口UndoableListener的类UndoHandler
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee){
undo.addEdit(uee.getEdit());
}
}/*End of class UndoHandler */
//用于设置字体的类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;
//构造函数MyFont
public MyFont(){
fontDialog = new JDialog(Notepad.this,"字体设置",true);
Container con=fontDialog.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
Font currentFont=editArea.getFont();
JLabel lblFont=new JLabel("字体(F):");
lblFont.setPreferredSize(new Dimension(100,20));
JLabel lblStyle=new JLabel("字形(Y):");
lblStyle.setPreferredSize(new Dimension(100,20));
JLabel lblSize=new JLabel("大小(S):");
lblSize.setPreferredSize(new Dimension(100,20));
tfFont=new JTextField(15);
tfFont.setText(currentFont.getFontName());
tfFont.selectAll();
tfFont.setPreferredSize(new Dimension(200,20));
tfStyle=new JTextField(15);
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(8);
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);
listFont.setFixedCellWidth(82);
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);
listStyle.setFixedCellWidth(99);
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(39);
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("记事本-HuangBin's Notepad");
sample.setHorizontalAlignment(SwingConstants.CENTER);
sample.setPreferredSize(new Dimension(300,50));
JPanel samplePanel=new JPanel();
samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));
samplePanel.add(sample);
con.add(lblFont);
con.add(lblStyle);
con.add(lblSize);
con.add(tfFont);
con.add(tfStyle);
con.add(tfSize);
con.add(fontOkButton);
con.add(new JScrollPane(listFont));
con.add(new JScrollPane(listStyle));
con.add(new JScrollPane(listSize));
con.add(cancelButton);
con.add(samplePanel);
updateSample();
fontDialog.setSize(350,340);
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);
}//End method updateSample
//设置文本编辑区的字体
public void actionPerformed(ActionEvent e){
if(e.getSource()==fontOkButton){
Font tempFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));
editArea.setFont(tempFont);
fontDialog.dispose();
}
}//End method actionPerformed
}/*End of class MyFont*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -