大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) 創(chuàng)建一個(gè)簡單的Java文本編輯器

創(chuàng)建一個(gè)簡單的Java文本編輯器

更新時(shí)間:2022-03-29 12:27:55 來源:動(dòng)力節(jié)點(diǎn) 瀏覽2944次

創(chuàng)建簡單文本編輯器:

首先,我們將創(chuàng)建一個(gè)名為“editor”的框架并應(yīng)用金屬外觀并在其中設(shè)置海洋主題。

我們將添加一個(gè)文本區(qū)域和一個(gè)菜單欄,其中包含三個(gè)菜單文件、編輯和關(guān)閉。

“文件”選項(xiàng)有 4 個(gè)新菜單項(xiàng),打開、保存和打印。

“編輯”有 3 個(gè)菜單項(xiàng)剪切、復(fù)制和粘貼。我們將為所有菜單項(xiàng)添加一個(gè)動(dòng)作偵聽器(使用 addActionListener() 函數(shù))以檢測(cè)任何動(dòng)作。

我們將使用 add() 函數(shù)將菜單項(xiàng)添加到菜單和菜單欄,我們將使用 addJMenuBar() 函數(shù)將菜單欄添加到框架。

我們將使用 add 函數(shù)將文本區(qū)域添加到框架中,使用 setSize(500,500) 函數(shù)將框架的大小設(shè)置為 500,500,然后使用 show 函數(shù)顯示框架。

以下是菜單功能的調(diào)用方式:

在選擇剪切、復(fù)制、粘貼和打印菜單項(xiàng)時(shí),將調(diào)用文本區(qū)域 cut()、copy()、paste() 和 print() 的內(nèi)置函數(shù)。

選擇“保存”菜單項(xiàng)時(shí),將打開一個(gè)文件選擇器,在選擇文件后顯示保存對(duì)話框,文件寫入器(緩沖寫入器)會(huì)將文本區(qū)域的內(nèi)容寫入文件并關(guān)閉文件寫入器和緩沖寫入器。

在選擇“打開”菜單項(xiàng)時(shí),將打開一個(gè)文件選擇器,在選擇文件后將顯示打開對(duì)話框,文件閱讀器和緩沖閱讀器將讀取文件并將文本區(qū)域的文本設(shè)置為文件的內(nèi)容。

如果選擇“新”菜單項(xiàng),則文本區(qū)域的文本將設(shè)置為空白。如果選擇“關(guān)閉”菜單項(xiàng),則使用函數(shù) isVisible(false) 關(guān)閉框架。

程序:

// Java Program to create a text editor using java
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
class editor extends JFrame implements ActionListener {
	// Text component
	JTextArea t;
	// Frame
	JFrame f;
	// Constructor
	editor()
	{
		// Create a frame
		f = new JFrame("editor");
		try {
			// Set metal look and feel
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

			// Set theme to ocean
			MetalLookAndFeel.setCurrentTheme(new OceanTheme());
		}
		catch (Exception e) {
		}
		// Text component
		t = new JTextArea();
		// Create a menubar
		JMenuBar mb = new JMenuBar();
		// Create amenu for menu
		JMenu m1 = new JMenu("File");
		// Create menu items
		JMenuItem mi1 = new JMenuItem("New");
		JMenuItem mi2 = new JMenuItem("Open");
		JMenuItem mi3 = new JMenuItem("Save");
		JMenuItem mi9 = new JMenuItem("Print");
		// Add action listener
		mi1.addActionListener(this);
		mi2.addActionListener(this);
		mi3.addActionListener(this);
		mi9.addActionListener(this);
		m1.add(mi1);
		m1.add(mi2);
		m1.add(mi3);
		m1.add(mi9);
		// Create amenu for menu
		JMenu m2 = new JMenu("Edit");
		// Create menu items
		JMenuItem mi4 = new JMenuItem("cut");
		JMenuItem mi5 = new JMenuItem("copy");
		JMenuItem mi6 = new JMenuItem("paste");
		// Add action listener
		mi4.addActionListener(this);
		mi5.addActionListener(this);
		mi6.addActionListener(this);
		m2.add(mi4);
		m2.add(mi5);
		m2.add(mi6);
		JMenuItem mc = new JMenuItem("close");
		mc.addActionListener(this);
		mb.add(m1);
		mb.add(m2);
		mb.add(mc);
		f.setJMenuBar(mb);
		f.add(t);
		f.setSize(500, 500);
		f.show();
	}
	// If a button is pressed
	public void actionPerformed(ActionEvent e)
	{
		String s = e.getActionCommand();
		if (s.equals("cut")) {
			t.cut();
		}
		else if (s.equals("copy")) {
			t.copy();
		}
		else if (s.equals("paste")) {
			t.paste();
		}
		else if (s.equals("Save")) {
			// Create an object of JFileChooser class
			JFileChooser j = new JFileChooser("f:");
			// Invoke the showsSaveDialog function to show the save dialog
			int r = j.showSaveDialog(null);
			if (r == JFileChooser.APPROVE_OPTION) {
				// Set the label to the path of the selected directory
				File fi = new File(j.getSelectedFile().getAbsolutePath());
				try {
					// Create a file writer
					FileWriter wr = new FileWriter(fi, false);
					// Create buffered writer to write
					BufferedWriter w = new BufferedWriter(wr);
					// Write
					w.write(t.getText());
					w.flush();
					w.close();
				}
				catch (Exception evt) {
					JOptionPane.showMessageDialog(f, evt.getMessage());
				}
			}
			// If the user cancelled the operation
			else
				JOptionPane.showMessageDialog(f, "the user cancelled the operation");
		}
		else if (s.equals("Print")) {
			try {
				// print the file
				t.print();
			}
			catch (Exception evt) {
				JOptionPane.showMessageDialog(f, evt.getMessage());
			}
		}
		else if (s.equals("Open")) {
			// Create an object of JFileChooser class
			JFileChooser j = new JFileChooser("f:");
			// Invoke the showsOpenDialog function to show the save dialog
			int r = j.showOpenDialog(null);
			// If the user selects a file
			if (r == JFileChooser.APPROVE_OPTION) {
				// Set the label to the path of the selected directory
				File fi = new File(j.getSelectedFile().getAbsolutePath());
				try {
					// String
					String s1 = "", sl = "";
					// File reader
					FileReader fr = new FileReader(fi);
					// Buffered reader
					BufferedReader br = new BufferedReader(fr);
					// Initialize sl
					sl = br.readLine();
					// Take the input from the file
					while ((s1 = br.readLine()) != null) {
						sl = sl + "\n" + s1;
					}
					// Set the text
					t.setText(sl);
				}
				catch (Exception evt) {
					JOptionPane.showMessageDialog(f, evt.getMessage());
				}
			}
			// If the user cancelled the operation
			else
				JOptionPane.showMessageDialog(f, "the user cancelled the operation");
		}
		else if (s.equals("New")) {
			t.setText("");
		}
		else if (s.equals("close")) {
			f.setVisible(false);
		}
	}
	// Main class
	public static void main(String args[])
	{
		editor e = new editor();
	}
}

輸出:

注意:以上程序可能無法在在線 IDE 中運(yùn)行,請(qǐng)使用離線編譯器。

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产精品欧美亚洲韩国日本99 | 四虎影永久地址www 四虎影永久在线高清免费 四虎影永久在线观看精品 四虎影永久在线观看网址 四虎影院.com | 国产成人无精品久久久 | 亚洲国产高清视频在线观看 | 精品国产调教最大网站女王 | 亚洲国产精品综合久久网络 | 色综合天天综合 | 亚洲区一 | 中文字幕一区二区视频 | 亚洲一级黄色 | 亚洲无线码一区在线观看 | 四虎精品成人免费永久 | 男人的天堂在线精品视频 | 国产精品视频ccav | 天天摸天天碰色综合网 | 天天干天天操天天做 | 99热在线精品免费播放6 | 免费鲁丝片一级观看 | 御姐色网 | 亚洲 欧美 精品 | 日本美女一区 | 免费一级a毛片免费观看欧美大片 | 国产a高清 | 久久精品国内一区二区三区 | 日本高清毛片视频在线看 | 香蕉视频黄色在线观看 | 一级看片免费视频 | 国产欧美日韩亚洲精品区2345 | 国产成人精品日本亚洲语音1 | 国产观看精品一区二区三区 | 亚洲成a人伦理 | 亚洲在线成人 | 一级中文字幕 | 国产系列在线播放 | 日日操操操 | 久久亚洲国产最新网站 | 欧美v在线 | 亚洲看片网| 国产一区二区三区成人久久片 | 九九99九九精彩 | 精产网红自拍在线 |