📄 notepad.java
字号:
if(findStr != null)
{
curSearch = textPane[current].getArea().getText().indexOf(findStr,curSearch+1);
if(curSearch > -1)
textPane[current].getArea().select(curSearch,findStr.length()+curSearch) ;
}
}
if(e.getActionCommand() == "Replace")
{
String replace = JOptionPane.showInputDialog("请输入要替换的字符串:");
textPane[current].getArea().replaceSelection(replace);
}
}
}
/**
find操作面板
*/
class FindOption extends JDialog
{
private JLabel findLabel = new JLabel("Find:");
private JTextField findFld = new JTextField(13) ;
private JPanel condition = new JPanel();
private JPanel direct = new JPanel();
private JCheckBox lowUp = new JCheckBox("区分大小写");
private JCheckBox allOn = new JCheckBox("全字匹配") ;
private JButton findB = new JButton("search");
private JButton close = new JButton("close") ;
private Border border = BorderFactory.createEtchedBorder();
public FindOption()
{
condition.add(lowUp);
condition.add(allOn);
condition.setBorder(border);
findB.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int lower = 0;
int upper = 0;
findStr = findFld.getText();
sourStr = textPane[current].getArea().getText();
if(lowUp.isSelected() == true)
curSearch = sourStr.indexOf(findStr);
else
{
lower = sourStr.indexOf(findStr.toLowerCase());
upper = sourStr.indexOf(findStr.toUpperCase());
if(lower < upper && lower >= 0)
curSearch = lower;
else if(upper >= 0)
curSearch = upper;
else
curSearch = sourStr.indexOf(findStr);
}
if(curSearch > -1)
textPane[current].getArea().select(curSearch,findStr.length()+curSearch);
setVisible(false);
}
});
close.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
setVisible(false);
}
});
setLayout(new FlowLayout());
setLocation(400,300) ;
setSize(200,140);
add(findLabel) ;
add(findFld) ;
add(condition) ;
add(findB);
add(close);
}
/* public int getCur()
{
return curSearch;//curSearch的值一直是0,不变.why
}
public String getStr()
{
return findStr;
}
*/
}
/**
view菜单监听器
*/
class ViewAction implements ActionListener
{
private JColorChooser fchooser = new JColorChooser();
private JColorChooser bchooser = new JColorChooser();
private JDialog fdialog ;
private JDialog bdialog ;
private JDialog set = new FontSet();
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "Font")
{
set.setVisible(true);
}
if(e.getActionCommand() == "FontColor")
{
fdialog = fchooser.createDialog(frame,"set font's color",true,fchooser,
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textPane[current].getArea().setForeground(fchooser.getColor());
}
},null);
fdialog.setVisible(true);
}
if(e.getActionCommand() == "Background")
{
bdialog = bchooser.createDialog(frame,"set background",true,bchooser,
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textPane[current].getArea().setBackground(bchooser.getColor());
}
},null);
bdialog.setVisible(true);
}
if(e.getActionCommand() == "AutoWrap")
{
if(textPane[current].getArea().getLineWrap() == false)
textPane[current].getArea().setLineWrap(true) ;
else
textPane[current].getArea().setLineWrap(false);
}
}
}
/**
字体设置dialog
*/
class FontSet extends JDialog
{
private GraphicsEnvironment fontSet;
private Font newFont ;
private JPanel fontPane ;
private JPanel stylePane ;
private JPanel sizePane ;
private JList fontList ;
private JList styleList ;
private JList sizeList ;
private JScrollPane fscroll;
private JScrollPane tscroll;
private JScrollPane sscroll;
private JButton okB = new JButton("ok") ;
private JButton cancelB = new JButton("cancel");
private JTextField fontFld = new JTextField() ;
private JTextField styleFld = new JTextField() ;
private JTextField sizeFld = new JTextField() ;
private String[] styles = {"Plain","Bold","Italic","Bold&Italic"} ;
private String[] sizes = {"1","2","3","4","5","6","7","8","9","10",
"12","14","16","18","20","24","36","48","72","else "};
private String preFont ;//前一次font设置
private String preStyle;
private String preSize ;
public FontSet()
{
super(frame,"font",true);
preFont = "楷体" ;
preStyle= "Plain" ;
preSize = "18" ;
fontSet = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontList = new JList(fontSet.getAvailableFontFamilyNames()) ;
styleList= new JList(styles);
sizeList = new JList(sizes) ;
fscroll = new JScrollPane(fontList) ;
tscroll = new JScrollPane(styleList);
sscroll = new JScrollPane(sizeList) ;
fontPane = new JPanel(new BorderLayout());
stylePane = new JPanel(new BorderLayout());
sizePane = new JPanel(new BorderLayout());
fontFld.setText(preFont) ;
styleFld.setText(preStyle);
sizeFld.setText(preSize) ;
fontPane.add(fontFld,"North") ;
fontPane.add(fscroll,"Center") ;
stylePane.add(styleFld,"North");
stylePane.add(tscroll,"Center");
sizePane.add(sizeFld,"North") ;
sizePane.add(sscroll,"Center") ;
setLocation(400,300) ;
setSize(350,255) ;
setLayout(new FlowLayout());
getContentPane().add(fontPane) ;
getContentPane().add(stylePane);
getContentPane().add(sizePane) ;
add(okB) ;
add(cancelB);
okB.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int sty = 0;
if(styleFld.getText() == "Plain")
sty = 0;
if(styleFld.getText() == "Bold")
sty = 1;
if(styleFld.getText() == "Italic")
sty = 2;
if(styleFld.getText() == "Bold&Italic")
sty = 3;
newFont = new Font(fontFld.getText(),sty,
Integer.parseInt(sizeFld.getText()));
textPane[current].getArea().setFont(newFont);//set font
preFont = fontFld.getText() ;
preStyle= styleFld.getText();
preSize = sizeFld.getText() ;
setVisible(false);
}
});
cancelB.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
fontList.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
Object obj = fontList.getSelectedValue();
fontFld.setText(obj.toString());
}
});
styleList.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
Object obj = styleList.getSelectedValue();
styleFld.setText(obj.toString());
}
});
sizeList.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
Object obj = sizeList.getSelectedValue();
if(obj.toString() != "else ")
sizeFld.setText(obj.toString());
}
});
}
}
/**
tool菜单监听器
*/
class ToolAction implements ActionListener
{
private String str;
private JDialog dialog = new JDialog(frame);
private JTextField words = new JTextField() ;
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "NewWindow")
{
textPane[current].getArea().setText("");
}
if(e.getActionCommand() == "WordCount")
{
str = textPane[current].getArea().getText();
words.setText("count is "+str.length()) ;
words.setEditable(false) ;
dialog.add(words) ;
dialog.setLocation(400,300) ;
dialog.setSize(100,80) ;
dialog.setVisible(true) ;
}
if(e.getActionCommand() == "Calc")
{
Runtime run = Runtime.getRuntime();
try
{
Process pro = run.exec("c:\\windows\\system32\\Calc.exe");
}
catch(IOException ioe)
{
System.err.println("error");
}
}
if(e.getActionCommand() == "Command")
{
Runtime run = Runtime.getRuntime();
try
{
Process pro = run.exec("c:\\windows\\system32\\cmd.exe");
}
catch(IOException ioe)
{
System.err.println("error");
}
}
if(e.getActionCommand() == "Option")
{
OptionSet option = new OptionSet();
int returnVar = JOptionPane.showOptionDialog(frame,option,
"Option",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,null,null,null);
if(JOptionPane.OK_OPTION == returnVar)
{
tab.setTabPlacement(option.getDirection());
}
}
}
}
/**
option设置
*/
class OptionSet extends JPanel
{
private String[] direction = {"up","bottom","left","right"};
private JLabel tabDirection = new JLabel("标签方向:") ;
private JComboBox tabDir = new JComboBox(direction) ;
public OptionSet()
{
add(tabDirection);
add(tabDir);
}
public int getDirection()
{
if(tabDir.getSelectedItem() == "bottom")
return JTabbedPane.BOTTOM;
if(tabDir.getSelectedItem() == "left")
return JTabbedPane.LEFT;
if(tabDir.getSelectedItem() == "right")
return JTabbedPane.RIGHT;
else
return JTabbedPane.TOP;
}
}
/**
help菜单监听器
*/
class HelpAction implements ActionListener
{
private Font font = new Font("楷体",1,36) ;
private JDialog dialog = new JDialog(frame) ;
private JTextArea topical = new JTextArea() ;
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "Topical")
{
topical.setFont(font) ;
topical.setText("Author:shya") ;
topical.setEditable(false) ;
topical.setBackground(Color.getColor(""));
JOptionPane.showConfirmDialog(frame,topical,
"Topical",JOptionPane.OK_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
if(e.getActionCommand() == "About")
{
topical.setFont(font) ;
topical.setText("SUPER NOTEPAD\nVersion:1.0");
topical.setEditable(false) ;
topical.setBackground(Color.getColor(""));
JOptionPane.showMessageDialog(frame,topical,
"About",JOptionPane.PLAIN_MESSAGE);
}
}
}
/**
TextAreaListener
*/
class TextListener implements DocumentListener
{
public void changedUpdate(DocumentEvent e)
{
if(!tab.getTitleAt(current).endsWith("*"))
tab.setTitleAt(current,tab.getTitleAt(current)+"*");
}
public void insertUpdate(DocumentEvent e)
{
if(!tab.getTitleAt(current).endsWith("*"))
tab.setTitleAt(current,tab.getTitleAt(current)+"*");
}
public void removeUpdate(DocumentEvent e)
{
if(!tab.getTitleAt(current).endsWith("*"))
tab.setTitleAt(current,tab.getTitleAt(current)+"*");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -