swing 文本编辑器

package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

public class MainFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = 295941050339534223L;
	
	Container contentPane;
	JPanel menuPanel,ctntPanel,labelPanel,centerPanel;
	JPanel filePanel,editPanel,outLookPanel,viewPanel,helpPanel;
	JMenuBar menuBar;
	JMenu fileMenu,editMenu,outLookMenu,viewMenu,helpMenu;
	JMenuItem newFile,open,save,anotherSave,pageSetup,print,quit;
	JMenuItem undo,cut,copy,paste,del,find,findNext,replace,turnto,selectAll,datetime;
	JMenuItem auto,font,status,helptheme,aboutNotepad;
	JTextArea textArea;
	JTabbedPane pane;
	File configFile;
	InputStream configIs;
	OutputStream configOs;
	Integer height;
	Integer width;
	Integer x;
	Integer y;
	Properties properties = new Properties();
	long clickTime = 0;
	
	public MainFrame() {
		super();
		
		configFile = new File("F:/HelloworldFactory/Helloworld/ThinkinJava/j2seTest/swing/config.properties");
		try {
			configIs = new FileInputStream(configFile);
			properties.load(configIs);
			
			System.out.println(properties.get("height"));
			System.out.println(properties.get("width"));
			
			height = Integer.parseInt(properties.get("height").toString().replace(".0", ""));
			width = Integer.parseInt(properties.get("width").toString().replace(".0", ""));
			x = Integer.parseInt(properties.get("X").toString());
			y = Integer.parseInt(properties.get("Y").toString());
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public boolean checkClickTime() {
		long nowTime = (new Date()).getTime();
		if ( (nowTime - clickTime) < 300) {
			clickTime = nowTime;
			return true;
		}
		clickTime = nowTime;
		return false;
	}

	public void showContent(){
		//fileMenu
		newFile = newMenuItem(Name.XINJIAN);
		open = newMenuItem(Name.DAKAI);
		save = newMenuItem(Name.BAOCUN);
		anotherSave = newMenuItem(Name.LINGCUNWEI);
		pageSetup = newMenuItem(Name.YEMIANSHEZHI);
		print = newMenuItem(Name.DAYIN);
		quit = newMenuItem(Name.TUICHU);
		
		//editMenu
		undo = newMenuItem(Name.CHEXIAO);
		cut = newMenuItem(Name.JIANQIE);
		copy = newMenuItem(Name.FUZHI);
		paste = newMenuItem(Name.NIANTIE);
		del = newMenuItem(Name.SHANCHU);
		find = newMenuItem(Name.CHAZHAO);
		findNext = newMenuItem(Name.CHAZHAOXIAYIGE);
		replace = newMenuItem(Name.TIHUAN);
		turnto = newMenuItem(Name.ZHUANDAO);
		selectAll = newMenuItem(Name.QUANXUAN);
		datetime = newMenuItem(Name.SHIJIANRIQI);
		
		//outLookMenu
		auto = newMenuItem(Name.ZIDONGHUANHANG);
		font = newMenuItem(Name.ZITI);
		
		//viewMenu
		status = newMenuItem(Name.ZHUANGTAILAN);
//		status.setEnabled(false);
		
		//helpMenu
		helptheme = newMenuItem(Name.BANGZHUZHITI);
		aboutNotepad = newMenuItem(Name.GUANYUJISHIBEN);
		
		//tab
		pane = new JTabbedPane();
		
		fileMenu = new JMenu(Name.FILE_MENU);
		fileMenu.add(newFile);
		fileMenu.add(open);
		fileMenu.add(save);
		fileMenu.add(anotherSave);
		fileMenu.add(new JSeparator());
		fileMenu.add(pageSetup);
		fileMenu.add(print);
		fileMenu.add(new JSeparator());
		fileMenu.add(quit);
		
		editMenu = new JMenu(Name.EDIT_MENU);
		editMenu.add(undo);
		editMenu.add(new JSeparator());
		editMenu.add(cut);
		editMenu.add(copy);
		editMenu.add(paste);
		editMenu.add(del);
		editMenu.add(new JSeparator());
		editMenu.add(find);
		editMenu.add(findNext);
		editMenu.add(replace);
		editMenu.add(turnto);
		editMenu.add(new JSeparator());
		editMenu.add(selectAll);
		editMenu.add(datetime);

		outLookMenu = new JMenu(Name.OUTLOOK_MENU);
		outLookMenu.add(auto);
		outLookMenu.add(font);

		viewMenu = new JMenu(Name.VIEW_MENU);
		viewMenu.add(status);

		helpMenu = new JMenu(Name.HELP_MENU);
		helpMenu.add(helptheme);
		helpMenu.add(new JSeparator());
		helpMenu.add(aboutNotepad);
		
		
		textArea = new JTextArea();
		
		menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(outLookMenu);
		menuBar.add(viewMenu);
		menuBar.add(helpMenu);
		
		menuPanel = new JPanel(new GridLayout());
		menuPanel.add(menuBar);
		JScrollPane scrollPane = new JScrollPane(textArea);
		ctntPanel = new JPanel(new GridLayout());
		ctntPanel.add(scrollPane);
//		pane.addTab("编辑1",scrollPane);
		pane.addMouseListener(new MouseListener(){
			public void mouseReleased(MouseEvent e) {
				if(checkClickTime()){
					int index = pane.getSelectedIndex();
					System.out.println("hello:  " + index);
					if(index>=0)
					pane.removeTabAt(index);
				}
			}
			public void mouseClicked(MouseEvent e) {
				
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
		});
		
		centerPanel = new JPanel(new GridLayout());
		centerPanel.add(pane);
		
		contentPane.add(menuPanel,BorderLayout.NORTH);
		contentPane.add(centerPanel,BorderLayout.CENTER);
//		contentPane.add(labelPanel,BorderLayout.SOUTH);
		Toolkit tk=Toolkit.getDefaultToolkit();
		Image image=tk.createImage("F:/HelloworldFactory/Helloworld/ThinkinJava/j2seTest/swing/title.png"); /*image.gif是你的图标*/ 
		this.setIconImage(image);
		
//		contentPane.add(new JScrollPane(textArea),BorderLayout.CENTER);
	}
	
	public JMenuItem newMenuItem(String name){
		JMenuItem menuItem = new JMenuItem(name);
		menuItem.addActionListener(this);
		return menuItem;
	}
	
	public void init(){
		contentPane =  getContentPane(); 
		setContentPane(contentPane); 
		setTitle("CK  记事本 v0.1"); 
	}
	
	public void showFrame(){
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
                dispose(); 
                
    			properties.setProperty("height", getSize().getHeight()+"");
    			properties.setProperty("width", getSize().getWidth()+"");
    			properties.setProperty("X", getX()+"");
    			properties.setProperty("Y", getY()+"");
    			
    			try {
    				configOs = new FileOutputStream(configFile);
					properties.store(configOs, "");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
                System.exit(0); 
			}
		});
		pack();
		
        setSize(width, height);
        setLocation(x, y);
        setVisible(true); 
	}
	
	
	public void showAll(){
		init();
		showContent();
		showFrame();
	}
	
	public static void main(String[] args) {
		MainFrame frame = new MainFrame();
		frame.showAll();
	}

	@Override
	public void pack() {
		super.pack();
	}
	
	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		System.out.println(action);
		if(action.equals(Name.XINJIAN)){
			textArea.setText(null);
			pane.addTab("编辑",new JScrollPane(textArea));
			System.out.println("text area:  "+textArea.getText().length());
		}
		if(action.equals(Name.DAKAI)){
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showOpenDialog(this);
			if(fileChooser.getSelectedFile()==null){
				System.out.println("未选中文件");
			}else{
				File file = fileChooser.getSelectedFile();
				if(file.getName().endsWith(".txt")==true){
					FileOperation fo = new FileOperation();
					fo.readFile(file,textArea);
					pane.addTab(file.getAbsolutePath(),new JScrollPane(textArea));
				}else{
					JOptionPane.showMessageDialog(this, "暂只支持.txt");
				}
			}
		}
		if(action.equals(Name.BAOCUN)){
			
		}
		if(action.equals(Name.LINGCUNWEI)){
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showSaveDialog(this);
		}
		if(action.equals(Name.YEMIANSHEZHI)){
			//以后处理
		}
		if(action.equals(Name.DAYIN)){
			//以后处理
		}
		if(action.equals(Name.TUICHU)){
			dispose(); 
	        System.exit(0); 
		}
		if(action.equals(Name.ZIDONGHUANHANG)){
			auto.setText(Name.ZIDONGHUANHANG2);
			textArea.setLineWrap(true);
			status.setEnabled(false);
		}
		if(action.equals(Name.ZIDONGHUANHANG2)){
			auto.setText(Name.ZIDONGHUANHANG);
			textArea.setLineWrap(false);
			status.setEnabled(true);
		}
		if(action.equals(Name.ZITI)){
			System.out.println("1111");
			
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java Swing组件开发的简单文本编辑器的示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; public class TextEditor extends JFrame { private JTextArea textArea; private JMenuBar menuBar; private JMenu fileMenu; private JMenuItem openMenuItem; private JMenuItem saveMenuItem; private JMenuItem exitMenuItem; public TextEditor() { setTitle("Java Swing Text Editor"); setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); menuBar = new JMenuBar(); fileMenu = new JMenu("File"); openMenuItem = new JMenuItem("Open"); saveMenuItem = new JMenuItem("Save"); exitMenuItem = new JMenuItem("Exit"); openMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int option = fileChooser.showOpenDialog(TextEditor.this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedReader reader = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); textArea.setText(sb.toString()); } catch (IOException ex) { ex.printStackTrace(); } } } }); saveMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int option = fileChooser.showSaveDialog(TextEditor.this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }); exitMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); setJMenuBar(menuBar); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TextEditor textEditor = new TextEditor(); textEditor.setVisible(true); } }); } } ``` 这个文本编辑器使用了Java Swing的各种组件,包括JFrame、JTextArea、JScrollPane、JMenuBar、JMenu和JMenuItem等。它具有菜单栏,可以打开和保存文件,并且可以编辑文本内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值