package fx.com;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
public class Main extends Application {
public static void main(String[] args) {
launch(Main.class, args);
}
@Override
public void start(Stage primaryStage) throws IOException{
Group group = new Group();
Scene scene = new Scene(group);
primaryStage.setScene(scene);
URL qunlogoUrl = new URL("http://www.haotuo.net.cn/Resources/cq/qunlogo.png");
primaryStage.setWidth(800);
primaryStage.setHeight(500);
primaryStage.getIcons().add(new Image(qunlogoUrl.toExternalForm()));
primaryStage.setTitle("JavaFX");
TextField textField = new TextField();
textField.setStyle(
"-fx-pref-width: 200;" +
"-fx-pref-height: 50;" +
"-fx-font-size: 20;"
);
textField.setLayoutX(100);
textField.setLayoutY(100);
Tooltip tip = new Tooltip("这是提示");
textField.setTooltip(tip);
textField.setPromptText("请输入7个文字");
textField.setFocusTraversable(false);
//监听文本输入的内容
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if(newValue.length()>7){
System.out.println("超出范围");
textField.setText(oldValue);
}
}
});
//监听文本框里面选择 的内容
textField.selectedTextProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(newValue);
}
});
PasswordField passwordField = new PasswordField();
passwordField.setStyle(
"-fx-pref-width: 200;" +
"-fx-pref-height: 50;" +
"-fx-font-size: 20;"
);
passwordField.setLayoutX(100);
passwordField.setLayoutY(200);
Label label = new Label("你在就好了");
label.setStyle(
"-fx-pref-width: 200;" +
"-fx-pref-height: 50;" +
"-fx-font-size: 20;"
);
label.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println(13);
}
});
group.getChildren().addAll(textField,passwordField,label);
primaryStage.show();
}
}