📄 11232044292.java
字号:
textArea.select(newstartp,newendp); ////找到相应文本,并选择
notfindmethod();
}
//替换事件
void Replace()
{
replaceDialog = new Dialog(this,"替换"); // 创建替换对话框;
replaceDialog.setLocation(300,200);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
findtxt = new JTextField(7);
repltxt = new JTextField(7);
p1.add(new JLabel("输入原始字符:"));
p1.add(findtxt);
p2.add(new JLabel("输入替换字符:"));
p2.add(repltxt);
p3.setLayout(new FlowLayout());
p3.add(SwapNextItem);
p3.add(replb);
p3.add(cancel1);
replaceDialog.add("North",p1);
replaceDialog.add("Center",p2);
replaceDialog.add("South",p3);
replaceDialog.setSize(300,150);
replaceDialog.show();
}
//点击替换对话框的确定按钮后的事件处理
void ReplaceEnter()
{
str = textArea.getText();
startp = str.indexOf(findtxt.getText());
endp = startp + findtxt.getText().length();
textArea.replaceRange(repltxt.getText(),startp,endp);
newendp = endp; ///获取这次替换的终点
}
//替换下一个事件
void ReplaceNext()
{
nexttemp = newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall = textArea.getText();
textArea.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext = textArea.getSelectedText();
newstartp = strnext.indexOf(findtxt.getText()) + nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp = newstartp + findtxt.getText().length();
textArea.select(newstartp,newendp);
notfindmethod();
textArea.replaceRange(repltxt.getText(),newstartp,newendp);//替换字符
}
//字体窗口类
class FontFrame extends JDialog implements ActionListener,MouseListener
{
private Font font = null;
private JTextArea t;//用于显示示例中文字
private JLabel Word_style = new JLabel("字体:");
private JLabel Word_Type = new JLabel("字形:");
private JLabel Word_Big = new JLabel("大小:");
private JLabel example = new JLabel("示例:");
private JButton Ok = new JButton("确定");
private JButton Cancel = new JButton("取消");
//private javax.swing.ListModel
private java.awt.List style = new java.awt.List();
private java.awt.List WBig = new java.awt.List();
private JTextField Word = new JTextField("常规");
private JTextField localbig = new JTextField("20");
private JTextField Type_text = new JTextField("宋体");
private java.awt.List WType_list = new java.awt.List();
private String []Font_list = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
public FontFrame(JFrame fra,String str,Font font)
{
super(fra,str);
this.font = font;
setup_font();
this.setLocation(300,200);
setModal(true);
setSize(400,300);
style.select(0);
WBig.select(6);
WType_list.addMouseListener(this);
style.addMouseListener(this);
WBig.addMouseListener(this);
Ok.addActionListener(this);
Cancel.addActionListener(this);
show();
}
void setup_font()
{
for(int i = 0;i < Font_list.length;i++)
{
WType_list.add(Font_list[i]);
}
WBig.add("8");
WBig.add("9");
WBig.add("10");
WBig.add("12");
WBig.add("14");
WBig.add("16");
WBig.add("20");
WBig.add("22");
WBig.add("24");
WBig.add("26");
WBig.add("28");
WBig.add("36");
WBig.add("40");
WBig.add("50");
WBig.add("72");
style.add("常规");
style.add("粗体");
style.add("斜体");
style.add("斜粗体");
Word.setEnabled(false);Type_text.setEditable(false);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add("North",Word_style);
panel1.add("Center",Type_text);
panel1.add("South",WType_list);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add("North",Word_Type);
panel2.add("Center",Word);
panel2.add("South",style);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add("North",Word_Big);
panel3.add("Center",localbig);
panel3.add("South",WBig);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(Ok);
panel4.add(Cancel);
JPanel panel0 = new JPanel();
panel0.setLayout(new GridLayout(1,3));
panel0.add(panel1);
panel0.add(panel2);
panel0.add(panel3);
JPanel panel5 = new JPanel();
panel5.setLayout(new BorderLayout());
panel5.add("North",example);
t = new JTextArea(5,20);
t.setEditable(false);
t.append("Software Engineering By Pawpaw!\n");
t.append(" 福州大学 软件工程");
panel5.add("Center",t);
setLayout(new BorderLayout());
add("North",panel0);
add("Center",panel5);
add("South",panel4);
this.setResizable(false);
validate();
}
public void mousePressed(MouseEvent evt) {}
public void mouseReleased(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt)
{
Type_text.setText(WType_list.getSelectedItem());
Word.setText(style.getSelectedItem());
localbig.setText(WBig.getSelectedItem());
Integer i=new Integer(localbig.getText());
t.setFont(new Font(Type_text.getText(),style.getSelectedIndex(),i));
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == Ok)
{
font=new Font(Type_text.getText(),style.getSelectedIndex(),new Integer(localbig.getText()));
this.hide();
}
else this.hide();
}
public void SetFont(Font font)
{
this.font = font;
}
}
//事件驱动
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == exit){////退出事件
exit();
}
else if (e.getSource() == about_us){////关于主题
about();
}
else if (e.getSource() == linewrap){////自动换行
if (linewrap.isSelected()){
textArea.setLineWrap(true);
status_bar.setEnabled(false);
statubar.setVisible(false);
}
else{
textArea.setLineWrap(false);
status_bar.setEnabled(true);
statubar.setVisible(true);
}
}
else if(e.getSource() == newfile || e.getSource() == new_tool){////新建文件
newFile();
}
else if(e.getSource() == openItem || e.getSource() == open_tool){////打开文件
openFile();
}
else if (e.getSource() == saveItem || e.getSource() == save_tool){///保存文件
saveFile();
}
else if (e.getSource() == saveAsItem || e.getSource() == save_as_tool){////另存文件
saveAsFile();
}
else if(e.getSource() == editCut || e.getSource() == peditCut || e.getSource() == cut_tool){
textArea.cut();////剪切
}
else if(e.getSource() == editCopy || e.getSource() == peditCopy || e.getSource() == copy_tool){
textArea.copy();////复制
}
else if(e.getSource() == editPaste || e.getSource() == peditPaste || e.getSource() == paste_tool){
textArea.paste();////粘贴
}
else if(e.getSource() == editDelete || e.getSource() == peditDelete || e.getSource() == delete_tool){
textArea.replaceSelection("");////删除
}
else if(e.getSource() == editSelectAll || e.getSource() == peditSelectAll){/////////////全选
textArea.selectAll();////全选
}
else if(e.getSource() == datetime){ ////时间/日期
int inpoint = textArea.getCaretPosition();
Date dt = new Date();
String strdate = dt.toLocaleString();
textArea.insert(strdate,inpoint);
}
else if(e.getSource() == editUndo || e.getSource() == peditUndo){
}
else if(e.getSource() == status_bar){//状态栏的隐藏和显视
Get_XY();
textArea.addKeyListener(new Keylistener());
textArea.addMouseListener(new Mouselistener());
}
else if(e.getSource() == tool){////选择是否显示工具条
if(tool.isSelected())
toolbar.setVisible(true);
else
toolbar.setVisible(false);
}
else if(e.getSource() == edit_font || e.getSource() == pedit_font || e.getSource() == font_tool){////字体
FontFrame ff = new FontFrame(text_editor.this,"字体",textfont);
textArea.setFont(ff.font);
}
else if(e.getSource() == fontcolor || e.getSource() == pedit_fontcolor || e.getSource() == color_tool){////字体颜色选择
JColorChooser jColor = new JColorChooser(); //调用颜色面板,设置前景就可更改字体颜色
Color fcolor=textArea.getForeground();
textArea.setForeground( jColor.showDialog(textArea,"选择字体颜色",fcolor));
}
else if(e.getSource() == Motif){ //界面风格
this.dispose();
text_editor ww = new text_editor();
try
{ //MOTIF界面
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(ww);
}
catch ( Exception er ) {}
}
else if(e.getSource() == Metal){
this.dispose();
text_editor ww = new text_editor();
try //METAL界面
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(ww);
}
catch ( Exception er) {}
}
else if(e.getSource() == Local){
this.dispose();
text_editor ww = new text_editor();
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(ww);
}
catch ( Exception er ) {}
}
else if(e.getSource() == editFind || e.getSource() == peditFind || e.getSource() == find_tool){
FindText();
}
else if(e.getSource() == findenter){//点击查找对话框的确定按钮后的事件处理
FindEnter();
}
else if(e.getSource() == editFindNext || e.getSource() == peditFindNext || e.getSource() == findnext_tool || e.getSource() == FindNextItem){
FindNext();
}
else if(e.getSource() == editSwap || e.getSource() == peditSwap || e.getSource() == repalce_tool){
Replace();
}
else if(e.getSource() == replb){
ReplaceEnter();
}
else if(e.getSource() == editSwapNext || e.getSource() == peditSwapNext || e.getSource() == repalcenext_tool || e.getSource() == SwapNextItem){
ReplaceNext();
}
else if(e.getSource() == cancel1){
replaceDialog.dispose();
}
else if(e.getSource() == cancel2){
findDialog.dispose();
}
}
//主函数
public static void main(String [] args){
text_editor text = new text_editor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -