package javafxui;
import javafx.scene.control.*;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.OverrunStyle;
import javafx.scene.effect.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
import javafx.event.*;
public class LearnUI extends Application{
@Override
public void start(Stage preStage) {
//Image imageOk = new Image(getClass().getResourceAsStream("ok.png"));
Image imageOk = new Image("res/ok.png");
Pane pane = new Pane();
Label label1 = new Label();
DropShadow shadow = new DropShadow();
/* @1
*
* Button创建3种形式
* 可以创建只含有图标不含有文本的Button
* 其中文本和图标可以单独指定 setText(String text), setGraphic(Node graphic)
*
* setGraphicTextGap() 设置文本与图标之间的间距
*
* setContentDisplay(ContentDisplay.TOP) 设置图标与文本相对位置
* 相对位置有 LEFT, RIGHT TOP BOTTOM
*
*/
Button button1 = new Button();
Button button2 = new Button("Accept");
//指定文本和图标
Button button3 = new Button("Accept", new ImageView(imageOk));
Button button4 = new Button();
button4.setGraphic(new ImageView(imageOk));
button3.setContentDisplay(ContentDisplay.TOP);
/*@2
* 添加Action 处理事件
* 添加视觉效果
* javafx.scene.effect
* addEventHandler(eventType, eventHandler)
*/
button2.setOnAction((ActionEvent e) -> {
label1.setText("Accepted");
});
button3.addEventHandler(MouseEvent.MOUSE_ENTERED, e->{
button3.setEffect(shadow);
});
button3.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
button3.setEffect(null);
});
pane.getChildren().add(button3);
preStage.setTitle("ShowJavaFXUI");
Scene scene = new Scene(pane);
preStage.setScene(scene);
preStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
UI控件 -> Button
最新推荐文章于 2022-10-06 14:01:15 发布