JavaFX应用举例

JavaFX目前是JDK8自带的一套UI框架(jfxrt.jar),相比AWT和SWING,它的文档相对更完整完善了,控件使用更为清晰简单,最重要的是作为Java客户端UI开发框架他和CSS兼容,作出的界面更为美观符合潮流。


oracle上有专门的DEMO程序(库)、API、控件css说明等比较详细的文档和下载。https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html。


以下是自学的示例UI程序:

 

 

package com.merrick.japanese;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javafx.application.Application;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

public class JaToCnFx extends Application {

	
	/**
	 * 控件
	 * */	
	private TabPane frametab;	
	private Tab jatocntab;
	private Tab cntojatab;	
	
	/**
	 * 方法
	 * */	
	@Override
	public void start(Stage stg) throws Exception {
		

		frametab = new TabPane();
		frametab.setPrefSize(800, 475);
		frametab.setMinSize(TabPane.USE_PREF_SIZE, TabPane.USE_PREF_SIZE);
		frametab.setMaxSize(TabPane.USE_PREF_SIZE, TabPane.USE_PREF_SIZE);
		frametab.setSide(Side.TOP);
		frametab.setRotateGraphic(false);
		frametab.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
		jatocntab = new Tab();
		jatocntab.setText("「日本語」->「中国語」");
		jatocntab.setStyle("-fx-font-size: 15px;");		
		
		
		cntojatab = new Tab("【中文】->【日文】");
		cntojatab.setStyle("-fx-font-size: 15px;");
		frametab.getTabs().add(jatocntab);
		frametab.getTabs().add(cntojatab);		
		
		
		JaToCnTab jtc = new JaToCnTab();
		jatocntab.setContent(jtc.createJAtoCNpane());	
		
		CnToJaTab ctj = new CnToJaTab();
		cntojatab.setContent(ctj.createCNtoJApane());		
						
		//Scene sc = new Scene(gp,800,475);
		Scene sc = new Scene(frametab,800,475);		
		
		/**CSS样式加入*/
		sc.getStylesheets().add(JaToCnFx.class.getResource("/com/merrick/japanese/fx.css").toExternalForm());	
						
		stg.setTitle("Vocabulary Translation test");
		stg.setScene(sc);
		stg.show();			
		
	}
	

	public static void main(String[] args) {		
		launch(args);
	}
	
	
		
	public static List getRandomIntsSerialNoRepeated(int maxelem) {
		/***
		 * 生成随机序列,不重复(和初始集合内容顺序有关)
		 * */

		int size = maxelem;
		List tmplst = new ArrayList();		
		int rx = 0;	
		Random rn = new Random();
		for (int i = 0; i < size; i++) {						
			if(tmplst.size()!=0) {				
				do {
					rx = rn.nextInt(size);				
				}while(tmplst.contains(rx));					
				tmplst.add(rx);		

			}else {				
				rx = rn.nextInt(size);			
				tmplst.add(rx);		

			}								
		}		
		return tmplst;		
	}
	
}



package com.merrick.japanese;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

public class CnToJaTab {
	public static final int CURRENT_LESSON_MAX = 9;
	
	private boolean onoff = false;//启停按钮状态flag	
	
	private String currentcn = null;//
	
	private int currentlesson = 1;	//课程词汇对应编号		
	
	private static Properties cntoja = new  Properties(); //lesson词汇文件,key:中文,value:日文(已加载,当前)	
	
	private int testorder = 0; //当前测试的词汇数量
	
	private List lst_order = null;//指定lesson,初始生成测试词汇的顺序,和词汇prop文件中行数有关
	
	private String[] keyarr = null;//词汇prop的键值构成的数组
	
	
	public void loadVocabularyViaLessonnumberCNtoJA(int lesson) {
		try {
			cntoja.clear();
			cntoja.load(JaToCnFx.class.getResourceAsStream("/com/merrick/chinese/props/lessoncn"+ String.valueOf(lesson) +".properties"));
		} catch (IOException e) {
			System.err.println(e.toString());
		}
		
	}
	
	
	public Parent createCNtoJApane() {
		GridPane gp = new GridPane();				
		gp.setHgap(15);
		gp.setVgap(15);
		gp.setPadding(new Insets(35,35,35,35));
		//构建中到测试页面控件
		
		Text title = new Text("Show Chinese, fill Japanese:");	//题头	控件
		title.setStyle("-fx-fill:linear-gradient(to top left, blue,red );-fx-font-size: 15px;");
		
		Label lb_lesson = new Label("Choose Lesson:");//下拉框标签
		lb_lesson.setStyle("-fx-font-weight:bold;-fx-font-size:13px;");		
		
		ChoiceBox cb_lesson = new ChoiceBox(); //下拉框控件
		cb_lesson.setPrefWidth(100);
		ArrayList items = new ArrayList();
		for (int i = 1; i <= CURRENT_LESSON_MAX; i++) {
			items.add(String.valueOf(i));
		}
		cb_lesson.getItems().addAll(items);//可见内容
		cb_lesson.getSelectionModel().select(currentlesson-1);//初始
		cb_lesson.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue arg0, Number oldval, Number newval) {
				
				currentlesson = newval.intValue() + 1;
			}			
		});
		
		Button btn_start = new Button("Press to Start Lesson ");//开始按钮控件	
		
		Label cnname = new Label("Chinese:"); //当前中文的标签
		Text cntxt = new Text(); //当前中文(动态)控件
		cntxt.setStyle("-fx-font-weight:bold;-fx-font-size:13px;");
		cntxt.setText("");
		
		Label input = new Label("Input:"); //输入框标签
		final TextField inputtxt = new TextField();//中文输入框控件
		inputtxt.setMinWidth(150);
		inputtxt.setMinSize(330, 15);
		input.setStyle("-fx-font-size:15px;");
		
		Button btn_valid = new Button("Validate");//验证按钮控件		
		
		Label lb_prog = new Label("Progress:");//下拉框标签
		
		ProgressBar prog = new ProgressBar(0);//进度控件
		prog.setPrefWidth(200);	
		
		final Text msgtxt_current = new Text();		//状态提示文字文本控件
		msgtxt_current.setText("");		
		//msgtxt_current.setStyle("-fx-font-weight:bold;-fx-font-size:15px;");
				
		final Text msgtx_torf = new Text(); //正确与否提示控件
		
		final Text msgtxt_correct = new Text(); //正确结果展示控件	
		msgtxt_correct.setStyle("-fx-font-weight:bold;-fx-font-size:15px;");
		
		EventHandler starttestAction = (ActionEvent event)->{
			loadVocabularyViaLessonnumberCNtoJA(currentlesson);
			testorder= 0;
			lst_order = JaToCnFx.getRandomIntsSerialNoRepeated(cntoja.size());
			keyarr = cntoja.keySet().toArray(new String[0]);
			getspecifiedVocabulary(currentlesson,cntoja.size(), testorder);
			cntxt.setText(currentcn);
			msgtxt_current.setText("    [LESSON "+currentlesson +", TOTAL:"+cntoja.size()+",CURRENT:"+(testorder)+"]");//
			prog.setProgress(1.0*testorder/cntoja.size());
			onoff = !onoff;
			if(onoff) {
				btn_start.setText("Going......");					
			}else {
				btn_start.setText("Press to Start Lesson ");	//"Lesson Start"
				msgtxt_current.setText("");
				cntxt.setText("");
			}
					
		};
		btn_start.setOnAction(starttestAction);
		
		EventHandler validaction = (ActionEvent event)->{
			String userinput = inputtxt.getText();
			String rightvalue = null;
			String key = currentcn;
			if (key==null)
				return;
			
			rightvalue = (String) cntoja.get(key);//特定map获取答案
			if(userinput.equals(rightvalue)) {
				msgtx_torf.setFill(Color.GREEN);
				msgtx_torf.setText("CORRECT INPUT.");						
			}else {
				msgtx_torf.setFill(Color.RED);
				msgtx_torf.setText("WRONG INPUT: " + userinput );		
			}
			msgtxt_correct.setText("[正确的结果]: "+key+"<====>"+ rightvalue);
			
			getspecifiedVocabulary(1,cntoja.size(),testorder);	
			msgtxt_current.setText("    [LESSON "+currentlesson +", TOTAL:"+cntoja.size()+",CURRENT:"+(testorder)+"]");
			prog.setProgress(1.0*testorder/cntoja.size());
			cntxt.setText(currentcn);	
			inputtxt.clear();
			
		};
		btn_valid.setOnAction(validaction);
		inputtxt.setOnKeyPressed(new EventHandler() {//输入框响应回车键
			@Override
			public void handle(KeyEvent kev) {
				if(kev.getCode() == KeyCode.ENTER) {
					validaction.handle((ActionEvent) btn_valid.getOnKeyPressed());
				}				
			}
		});
		
		gp.add(title, 		0, 0, 3, 1);
		
		gp.add(lb_lesson, 	0, 1, 1, 1);
		gp.add(cb_lesson, 	1, 1, 1, 1);
		gp.add(btn_start, 	2, 1, 1, 1);
		
		gp.add( cnname, 	0, 2, 1, 1);		
		gp.add( cntxt, 		1, 2, 1, 1);		
		
		gp.add( input, 		0, 3, 1, 1);
		gp.add( inputtxt, 	1, 3, 1, 1);
		gp.add( btn_valid, 	2, 3, 1, 1);		
		
		gp.add( lb_prog, 	0, 4, 1, 1);
		gp.add( prog, 		1, 4, 2, 1);
		
		gp.add(msgtxt_current,  	0, 5, 3, 1);
		gp.add(msgtx_torf,  	0, 6, 3, 1);
		gp.add(msgtxt_correct, 	0, 7, 3, 1);
		
		return gp;
	}
	
	
	public void getspecifiedVocabulary(int lesson, int len, int order) {
		if(order>=len) {
			return;
		}
					
		currentcn = keyarr[lst_order.get(order)];
			
		System.out.println(currentcn);
		testorder++;
	}

}

 

 

 

 

你可能感兴趣的:(编程语言,ui,java,数据库)