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

📄 kodit.java

📁 一个小的文本编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.colorchooser.*;
import javax.swing.text.*;
import javax.swing.event.*;
class FontChooser extends JDialog
{
	SimpleAttributeSet attributes;
	JComboBox fontNames;
	JLabel preview;
	JTextField fontSize;
	JCheckBox bold,italic;
	JColorChooser colorChooser;
	public FontChooser(JFrame parent)
	{
		super(parent,"Font Chooser",true);
		setSize(450,450);
		fontSize=new JTextField(3);
		fontSize.setText("14");
		attributes=new SimpleAttributeSet();
		JPanel fontPanel=new JPanel();
		fontPanel.setLayout(new BorderLayout());
		preview=new JLabel("FontSample");
		preview.setFont(new Font("Arial",0,32));
		fontNames=new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
		fontNames.setSelectedItem("Times New Roman");
		fontNames.addActionListener(new UpdateListener());
		italic=new JCheckBox("italic");
		bold=new JCheckBox("bold");
		italic.addActionListener(new UpdateListener());
		bold.addActionListener(new UpdateListener());
		JPanel fontStylePanel=new JPanel();
		fontStylePanel.add(italic);
		fontStylePanel.add(bold);
		fontPanel.add(fontNames,BorderLayout.WEST);
		fontPanel.add(fontStylePanel,BorderLayout.CENTER);
		JLabel sizeLabel=new JLabel("size:");
		JPanel fontSizePanel=new JPanel();
		fontSizePanel.setLayout(new BorderLayout());
		fontSizePanel.add(sizeLabel,BorderLayout.NORTH);
		fontSizePanel.add(fontSize,BorderLayout.SOUTH);
		fontPanel.add(fontSizePanel,BorderLayout.EAST);
		getContentPane().add(fontPanel,BorderLayout.NORTH);
		
		colorChooser=new JColorChooser(Color.BLACK);
		colorChooser.getSelectionModel().addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e)
			{
				updatePreviewFont();
			}
		});
		getContentPane().add(colorChooser,BorderLayout.CENTER);
		
		JButton okButton=new JButton("OK");
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				StyleConstants.setFontSize(attributes,Integer.parseInt(fontSize.getText().trim()));
				setVisible(false);
			}
		});
		JButton cacelButton=new JButton("cacel");
		cacelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				attributes=null;
				setVisible(false);
			}
		});
		JPanel previewDonePanel=new JPanel();
		previewDonePanel.add(preview);
		previewDonePanel.add(okButton);
		previewDonePanel.add(cacelButton);
		getContentPane().add(previewDonePanel,BorderLayout.SOUTH);
	}
	private void updatePreviewFont()
	{
		String fontFamilyName=(String)fontNames.getSelectedItem();
		Color color=colorChooser.getColor();
		boolean isItalic=italic.isSelected();
		boolean isBold=bold.isSelected();
		int size=Integer.parseInt(fontSize.getText().trim());
		preview.setFont(new Font(fontFamilyName,(isItalic?Font.ITALIC:0)+(isBold?Font.BOLD:0),32));
		preview.setForeground(color);
		preview.repaint();
		StyleConstants.setFontFamily(attributes,fontFamilyName);
		StyleConstants.setFontSize(attributes,size);
		StyleConstants.setBold(attributes,isBold);
		StyleConstants.setItalic(attributes,isItalic);
		StyleConstants.setForeground(attributes,color);
	}
	public SimpleAttributeSet getAttributes(){
		return attributes;
	}
	class UpdateListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			updatePreviewFont();
		}
	}

}
class FileFilterImpl extends javax.swing.filechooser.FileFilter{
	String[] exts;
	String description;
	public FileFilterImpl(String[] extensions,String description){
		exts=new String[extensions.length];
		for(int i=0;i<exts.length;i++)
			exts[i]=extensions[i];
		this.description=description;
	}
	public boolean accept(File f){
		String fileName=f.getName();
		for(int i=0;i<exts.length;i++){
			if(fileName.endsWith(exts[i]))
				return true;
		}
		return false;
	}
	public String getDescription(){
		return description;
	}
}

class StringEdit
{
public static String StringReplace(String Source,String indexStr,String replaceWord)
	{
	int scanPoint=0;
	String backUp=Source;
	while(scanPoint!=-1&&scanPoint<backUp.length())
		{
		scanPoint=backUp.indexOf(indexStr,scanPoint);
		if(scanPoint!=-1)
			{
			String frontStr=backUp.substring(0,scanPoint);
			String backStr=backUp.substring(scanPoint+indexStr.length(),backUp.length());
			backUp=frontStr+replaceWord+backStr;
			scanPoint++;
			scanPoint+=(replaceWord.length()-indexStr.length());
			}     
		}
	return backUp;
	}

}


public class Kodit extends JFrame
{
	JTextArea editArea=new JTextArea();
	Clipboard clipBoard=getToolkit().getSystemClipboard();
	UndoManager undoManager=new UndoManager();
	File currentFile;
	public void save()
	{
		String content=editArea.getText();
		content=StringEdit.StringReplace(content,"\n","\r\n");
		if(currentFile==null)
		{
			saveAs();
			return;
		}
		try{
			
			BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentFile)));
			bw.write(content);
			bw.close();
			undoManager.discardAllEdits();
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
	public void saveAs()
	{	
		JFileChooser chooser;
		if(currentFile==null)
		{		
			chooser=new JFileChooser(".");
		}
		else
		{
			chooser=new JFileChooser(currentFile.getAbsolutePath());
		}
		int r=chooser.showOpenDialog(Kodit.this);
		if(r==JFileChooser.APPROVE_OPTION){
			File file=chooser.getSelectedFile();
			String content=editArea.getText();
			content=StringEdit.StringReplace(content,"\n","\r\n");
			try{
				currentFile=file;
				BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
				bw.write(content);
				bw.close();
				this.setTitle(file.getName());
				undoManager.discardAllEdits();
			}
			catch(IOException ioe)
			{
				ioe.printStackTrace();
			}
		}
	}
	public void saveOnNew()
	{
		if(undoManager.canUndo())
		{
		String[] options={"yes","no"};
		int choice;
		if(currentFile==null)
		choice=JOptionPane.showOptionDialog(this,"Save changes?","To be sure",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
		else
		choice=JOptionPane.showOptionDialog(this,"Save changes to  \""+currentFile.getName()+"\" ?","To be sure",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
		if(choice==JOptionPane.YES_OPTION)
		{
			undoManager.discardAllEdits();
			if(currentFile==null)
				saveAs();
			else save();
		}
		}
	}
	public void saveOnExit()
	{
		if(undoManager.canUndo())
		{
		String[] options={"yes","no"};int choice;
		if(currentFile==null)
		choice=JOptionPane.showOptionDialog(this,"Save changes?","To be sure",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
		else
		choice=JOptionPane.showOptionDialog(this,"Save changes to  \""+currentFile.getName()+"\" ?","To be sure",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
		if(choice==JOptionPane.YES_OPTION){
			if(currentFile==null)
				saveAs();
			else save();
				System.exit(0);
		}
		else if(choice==JOptionPane.NO_OPTION)
			{
			System.exit(0);
			}
		}
		else
			System.exit(0);
	}
	public Kodit()
	{
		setSize(500,309);
		setTitle("Kodit v0.01a");
		addWindowListener(new WindowAdapter() 
		{
		      public void windowClosing(WindowEvent e) 
		      {
		        saveOnExit();
		      }
		});
		JToolBar toolBar=new JToolBar();
		this.getContentPane().setLayout(new BorderLayout(10,10));
		this.getContentPane().add("North",toolBar);
		getContentPane().add(new JScrollPane(editArea));
		final JMenuItem undoItem=new JMenuItem("undo");
		final JMenuItem redoItem=new JMenuItem("redo");
		ImageIcon newIcon=new ImageIcon("ICON/new.gif");
		JButton newButton=new JButton(newIcon);
		newButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e){
				saveOnNew();
		        editArea.setText("");
		        Kodit.this.setTitle("Untitled1.txt");
		        currentFile=new File("Untitled1.txt");
			}
		});
		ImageIcon copyIcon=new ImageIcon("ICON/copy.gif");
		JButton copyButton=new JButton(copyIcon);
		copyButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{

⌨️ 快捷键说明

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