1.JAVAFX模板
package day09;
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
//1.继承Application,必须是它的子类
public class JavaFXSkel extends Application {
//2.进入主函数
public static void main(String[] args) {
//1>.可以在这里对控制台进行操作.
//2>.调用launch方法启动.7
}
//3.重写init()方法,可省略
public void init(){
}
//4.重写start()方法
@Override
public void start(Stage primaryStage) throws Exception {
//1>创建舞台title
//2>创建根节点
//3>创建场景
//4>设置舞台场景
//5>显示场景
}
//5.重写stop()方法
public void stop(){
}
}
2.Label
package day09;
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
public class JavaFXLabelDemo extends Application{
public static void main(String[] args){
launch(args);
}
public void init(){
System.out.println("开始...");
}
@Override
public void start(Stage primaryStage) throws Exception {
//1.为舞台取名字
primaryStage.setTitle("舞台测试2");
//3.创建根节点
FlowPane rootNode = new FlowPane();
//2.创建场景(将根节点添加进场景)
Scene myScene = new Scene(rootNode,500,300);
//3.设置舞台场景为这个场景
primaryStage.setScene(myScene);
//3.可以写内容了
// 1>创建个标签.
Label myLabel = new Label("这是第一个标签鸭");
// 2>将标签添加进根节点中.
rootNode.getChildren().add(myLabel);
//展示舞台
primaryStage.show();
}
}
3.Button
package day09;
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.*;
public class JavaFXEventDemo extends Application {
Label response;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//1.为舞台设置title
primaryStage.setTitle("舞台测试3");
//2.设置根节点及其元素对齐方式 添加两个数字以用来控制元素的中间间隙
FlowPane rootNode = new FlowPane(10,10);
rootNode.setAlignment(Pos.CENTER); //元素对齐方式
//3.设置场景并将根节点添加进场景
Scene myScene = new Scene(rootNode,400,200);
//4.将场景添加进舞台
primaryStage.setScene(myScene);
//5.内容!!!
response = new Label("");
// 1>创建按钮
Button btnUp = new Button("Up");
Button btnDown = new Button("Down");
// 2>为按钮添加事件处理程序
btnUp.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
response.setText("你按了Up按钮");
}
} );
btnDown.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
response.setText("你按了Down按钮");
}
});
//6.将这些按钮(组件)添加进跟节点
rootNode.getChildren().addAll(btnUp,btnDown,response);
//展示舞台
primaryStage.show();
}
}
4.CheckBox
package day09;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.geometry.*;
public class CheckboxDemo extends Application {
Label response;
Label selected;
String computers;
CheckBox cb_SmartPhone;
CheckBox cb_Tablet;
CheckBox cb_NoteBook;
CheckBox cb_Desktop;
public static void main(String[] args){
launch(args);
}
public void init() {
System.out.println("Starting...");
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("舞台测试4");
FlowPane rootNode = new FlowPane(Orientation.VERTICAL, 10, 10);
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode, 200, 200);
primaryStage.setScene(myScene);
//内容
cb_SmartPhone = new CheckBox("SmartPhone");
cb_Tablet = new CheckBox("Tablet");
cb_NoteBook = new CheckBox("NoteBook");
cb_Desktop = new CheckBox("Desktop");
response = new Label("请选择");
selected = new Label("您还未选择");
primaryStage.show();
rootNode.getChildren().add(cb_SmartPhone);
rootNode.getChildren().add(cb_Tablet);
rootNode.getChildren().add(cb_NoteBook);
rootNode.getChildren().add(cb_Desktop);
rootNode.getChildren().add(response);
rootNode.getChildren().add(selected);
cb_SmartPhone.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(cb_SmartPhone.isSelected()){
response.setText("您选择了SmartPhone");
}
else
response.setText("您取消了SmartPhone");
showAll();
}
});
cb_Tablet.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(cb_Tablet.isSelected()){
response.setText("您选择了Tablet");
}
else
response.setText("您取消了Tablet");
showAll();
}
});
cb_NoteBook.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(cb_NoteBook.isSelected()){
response.setText("您选择了NoteBook");
}
else
response.setText("您取消了Tablet");
showAll();
}
});
cb_Desktop.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event){
if(cb_Desktop.isSelected()){
response.setText("您选择了Desktop");
}
else
response.setText("您取消了Desktop");
showAll();
}
});
}
void showAll(){
computers = "";
if(cb_SmartPhone.isSelected()) computers = "SmartPhone";
if(cb_Tablet.isSelected()) computers += "Tablet";
if(cb_NoteBook.isSelected()) computers += "NoteBook";
if(cb_Desktop.isSelected()) computers += "Desktop";
selected.setText("Computers selected: " + computers);
}
}
5.ListView
package day09;
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
import javafx.beans.value.*;
import javafx.collections.*;
public class ListViewDemo extends Application{
Label response;
public static void main(String[] args){
launch(args);
}
public void init(){
System.out.println("Starting...");
}
//内容
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("list测试");
FlowPane rootNode = new FlowPane(Orientation.VERTICAL, 10, 10);
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode,400,300);
primaryStage.setScene(myScene);
//内容
response = new Label("选择计算机类型");
// **开始进行ListView创建
// 1>使用FXCollectoins类定义的静态工厂方法observableArrayList()
// static <E> ObservableList<E> observableArrayList(E...elements)
ObservableList<String> computerTypes = FXCollections.observableArrayList("Smartphone","Tablet","Notebook","Desktop");
ListView<String> lvComputers = new ListView<>(computerTypes);//这一步是生成组件!!!
// 2>设置选择的高度和/或宽度
// final void setPrefHeight(double height)
// final void setPrefWidth(double width)
// 也可以通过setPrefSize()方法
// setPrefSize(double width,double height)
lvComputers.setPrefSize(100,200);
// 3>为了监听变化时事件,必须首先获得ListView使用的选择模式.
// 这是通过对列表调用getSelectionModel()方法实现的
// final MultipleSelectionModel<T> getSelectionModel()
// 该方法返回对模式的引用
MultipleSelectionModel<String> lvSelModel = lvComputers.getSelectionModel();
// 4>上一方法返回的引用中的属性定义了选中列表中的元素将发生什么,通过调用selectedItemProperty()方法
// 5>对返回的属性调用addListener()方法,将变化监听器添加给这个属性
// void addListener(ChangeListener<? super T>listener")
lvSelModel.selectedItemProperty().addListener(new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
response.setText("Computer selected is " + newValue);
}
});
rootNode.getChildren().addAll(lvComputers, response);
primaryStage.show();
}
public void stop(){
System.out.println("Stop...");
}
}
6.TextField文本框
package day09;
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.*;
public class TextFieldDemo extends Application{
TextField tf;
Label response;
public void init(){
System.out.println("Starting...");
}
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("文本测试");
FlowPane rootNode = new FlowPane(10,10); //在根节点这里设置组件之间的间距
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode,230,140);
primaryStage.setScene(myScene);
response = new Label("输入名字: ");
Button btnGetText = new Button("获取名字");
//创建文本框
tf = new TextField();
//设置文本框中的默认提示消息
tf.setPromptText("请输入一个名字");
//设置文本框的列宽
tf.setPrefColumnCount(15);
//处理文本框的事件
tf.setOnAction((ae)->response.setText("已按下按钮,名字是" + tf.getText()) );
btnGetText.setOnAction((ae)->response.setText("已按下按钮,名字是" + tf.getText()));
//分割线
Separator separator = new Separator();
separator.setPrefWidth(180);
rootNode.getChildren().addAll(tf,btnGetText,separator,response);
primaryStage.show();
}
public void stop(){
System.out.println("Stoping...");
}
}