栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现日记本功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现日记本功能


import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import java.awt.FileDialog;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.scene.input.*;
public class Java extends Application
{
	private TextField tf=new TextField();
	private FileDialog fd1,fd2;
	private PasswordField pf=new PasswordField();
	private TextArea ta=new TextArea("可以编写你的日记了");
	private String path = null;// 装打开的文件的路径
	boolean save = false; // 没有保存
	int startIndex = 0; ///字符串开始的位置
	@Override
	public void start(Stage stage)
	{
		MenuBar menuB=new MenuBar();
		BorderPane rootBP=new BorderPane();
		rootBP.setTop(menuB);
		rootBP.setCenter(ta);
		Menu fileM=new Menu("文件");
		fileM.setMnemonicParsing(true);
		MenuItem newMI=new MenuItem("新建");
		MenuItem openMI=new MenuItem("打开");
		openMI.setAccelerator(KeyCombination.keyCombination("Ctrl+o"));
		MenuItem saveMI=new MenuItem("保存");
		MenuItem exitMI=new MenuItem("退出");
		exitMI.setAccelerator(KeyCombination.keyCombination("Ctrl+x"));
		EventHandlerMEHandler=new EventHandler()
		{
			public void handle(ActionEvent ae)
			{
				String name=((MenuItem) ae.getTarget()).getText();
				if(name.equals("退出"))
					Platform.exit();
				if(name.equals("打开"))
				{
					openMethod();
				}
				if(name.equals("新建"))
				{
					newMethod();
				}
				if(name.equals("保存"))
				{
					saveMethod();
				}
			}
		};
		exitMI.setOnAction(MEHandler);
		newMI.setOnAction(MEHandler);
		openMI.setOnAction(MEHandler);
		saveMI.setOnAction(MEHandler);
		fileM.getItems().addAll(newMI,openMI,saveMI,exitMI);
		Menu styleM=new Menu("格式");
		Menu fontM=new Menu("字体");
		CheckMenuItem boldMI=new CheckMenuItem("粗体");
		boldMI.setSelected(true);
		CheckMenuItem italicMI=new CheckMenuItem("斜体");
		fontM.getItems().addAll(boldMI,italicMI);
		Menu rgbM=new Menu("颜色");
		RadioMenuItem rMI=new RadioMenuItem("红色");
		RadioMenuItem gMI=new RadioMenuItem("绿色");
		RadioMenuItem bMI=new RadioMenuItem("蓝色");
		rMI.setOnAction(e->ta.setStyle("-fx-text-fill:red"));
		gMI.setOnAction(e->ta.setStyle("-fx-text-fill:green"));
		bMI.setOnAction(e->ta.setStyle("-fx-text-fill:blue"));
		boldMI.setOnAction(e->ta.setStyle("-fx-text-weight:blod"));
		italicMI.setOnAction(e->ta.setStyle("-fx-text-decoration:italic"));
		bMI.setSelected(true);
		ToggleGroup rgbG=new ToggleGroup();
		rMI.setToggleGroup(rgbG);
		gMI.setToggleGroup(rgbG);
		bMI.setToggleGroup(rgbG);
		rgbM.getItems().addAll(rMI,gMI,bMI);
		styleM.getItems().addAll(fontM,new SeparatorMenuItem(),rgbM);
		menuB.getMenus().addAll(fileM,styleM);
		GridPane rootGP=new GridPane();
		BorderPane bp=new BorderPane();
		final Label lab1=new Label("用户名:");
		final Label lab2=new Label("密码:");
		tf.setPromptText("请输入用户名");
		pf.setPromptText("请输入密码");
		Button bt=new Button("登录");
		bt.setPrefWidth(150);
		BorderPane.setAlignment(bt,Pos.CENTER);
		rootGP.add(lab1, 1, 1);
		rootGP.add(tf, 2, 1);
		rootGP.add(lab2, 1, 3);
		rootGP.add(pf, 2, 3);
		rootGP.setStyle("-fx-padding:20");
		bp.setCenter(rootGP);
		bp.setBottom(bt);
		bp.setStyle("-fx-padding:40");
		ta.setPrefColumnCount(15);
		ta.setEditable(false);
		bt.setOnAction(e->
		{
			if(tf.getText().equals("18704485473")&&pf.getText().equals("YX31818307996845"))
			{
				ta.setEditable(true);
				 Stage secondStage = new Stage();
				 StackPane secondPane = new StackPane(rootBP);
				 Scene secondScene = new Scene(secondPane, 600, 600);
				 secondStage.setTitle("韩绍堂的日记本");
	             secondStage.setScene(secondScene);
                 secondStage.show();      
			}
			else
			{
				 JOptionPane.showMessageDialog(null,"用户名或密码错误,请重新输入","错误",JOptionPane.INFORMATION_MESSAGE);     
				 tf.setText("");
				 pf.setText("");
			}
		}
	);
		Scene scene=new Scene(bp,390,180);
		stage.setTitle("我的日记本");
		stage.setScene(scene);
		stage.show();
	}
	private void openMethod() 
	{
		FileChooser fc = new FileChooser(); 
		fc.setTitle("选择文件-打开");
		fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("TXT", "*.txt"));
		Window stage = null;
		File file = fc.showOpenDialog(stage); 
		if (file != null && file.exists())
		{ 
			try 
			{
				FileInputStream in = new FileInputStream(file); 
				byte[] bs = new byte[(int) file.length()];
				in.read(bs);
				ta.setText(new String(bs));
				in.close();
				path = file.getPath();
				int lastIndex = path.lastIndexOf("\"); 
				String title = path.substring(lastIndex + 1); /
				setTitle(title + "-我的记事本"); 
			} catch (Exception e) {
			}
		}
	}
	private void setTitle(String string) {
		// TODO Auto-generated method stub
		
	}
	private void newMethod() {
		if (save) 
		{  
			ta.setText("");
			setTitle("新文件-我的记事本");
			path = null;
		} 
		else if (!save && !ta.getText().isEmpty()) 
		{
			Stage newStage = new Stage();
			VBox vBox = new VBox();
			vBox.setSpacing(5);
			Label label1 = new Label("你还没有保存内容!!!");
			Label label2 = new Label("按“确定”则新建一个文本,");
			Label label3= new Label("按“取消”则退出该窗口。");
			vBox.getChildren().addAll(label1,label2,label3);
			Button ok = new Button("确定");
			Button cancel = new Button("取消");
			HBox hBox = new HBox();
			hBox.setSpacing(30);
			hBox.getChildren().addAll(ok,cancel);
			BorderPane rootNode = new BorderPane();
			rootNode.setTop(vBox);
			rootNode.setCenter(hBox);
			Scene scene = new Scene(rootNode,300,150);
			newStage.setTitle("提示");
			newStage.setScene(scene);
			newStage.show();
			ok.setOnAction(event->{
				ta.setText("");
				setTitle("新文件-我的记事本");
				path = null;
				newStage.close();
			});
			
			cancel.setOnAction(event->{
				newStage.close(); 
			});
			
		} 	
	}
	private void saveMethod()
	{
		if (path == null) {
			
			FileChooser fc = new FileChooser();
			fc.setTitle("选择文件-保存");
			Window stage = null;
			File file = fc.showSaveDialog(stage);
			if (file != null && !file.exists())
			{
				try
				{
					FileOutputStream out = new FileOutputStream(file);
					out.write(ta.getText().getBytes());
					out.flush();
					out.close();
					save = true; 
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		}
		else 
		{
			try
			{
				FileOutputStream out = new FileOutputStream(path);
				out.write(ta.getText().getBytes());
				out.flush();
				out.close();
				save = true;
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872648.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号