修正resizable的bug
This commit is contained in:
@@ -7,6 +7,7 @@ import cn.edu.scau.biubiusuisui.factory.FXBuilder;
|
||||
import cn.edu.scau.biubiusuisui.factory.FXControllerFactory;
|
||||
import cn.edu.scau.biubiusuisui.function.FXWindowParser;
|
||||
import cn.edu.scau.biubiusuisui.utils.ClassUtils;
|
||||
import javafx.application.Application;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.HashSet;
|
||||
|
||||
@@ -5,16 +5,24 @@ import javafx.beans.property.Property;
|
||||
|
||||
/**
|
||||
* 将Controller中的JavaFX的field包装成FXFieldWrapper
|
||||
*
|
||||
* @Author jack
|
||||
* @Date:2019/6/28 10:03
|
||||
*/
|
||||
public class FXFieldWrapper {
|
||||
|
||||
private FXField fxField;
|
||||
private Class type;
|
||||
|
||||
private Property property;
|
||||
|
||||
private Class type;
|
||||
public FXFieldWrapper() {
|
||||
}
|
||||
|
||||
public FXFieldWrapper(FXField fxField, Class type) {
|
||||
this.fxField = fxField;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Class getType() {
|
||||
return type;
|
||||
@@ -36,7 +44,7 @@ public class FXFieldWrapper {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(Property property){
|
||||
public void setProperty(Property property) {
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package cn.edu.scau.biubiusuisui.example.actionDemo;
|
||||
package cn.edu.scau.biubiusuisui.example.bindDemo;
|
||||
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXScan;
|
||||
import cn.edu.scau.biubiusuisui.config.FXPlusApplication;
|
||||
@@ -9,10 +9,10 @@ import javafx.stage.Stage;
|
||||
* @Author jack
|
||||
* @Date:2019/7/27 1:43
|
||||
*/
|
||||
@FXScan(base = "cn.edu.scau.biubiusuisui.example.actionDemo")
|
||||
public class Demo extends Application {
|
||||
@FXScan(base = "cn.edu.scau.biubiusuisui.example.bindDemo")
|
||||
public class BindDemo extends Application {
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
FXPlusApplication.start(Demo.class);
|
||||
FXPlusApplication.start(BindDemo.class);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package cn.edu.scau.biubiusuisui.example.actionDemo;
|
||||
package cn.edu.scau.biubiusuisui.example.bindDemo;
|
||||
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXBind;
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXController;
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXData;
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
|
||||
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
@@ -9,6 +10,7 @@ import javafx.beans.value.ObservableValue;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.PasswordField;
|
||||
import javafx.scene.control.TextField;
|
||||
|
||||
import java.net.URL;
|
||||
@@ -18,23 +20,58 @@ import java.util.ResourceBundle;
|
||||
* @Author jack
|
||||
* @Date:2019/7/27 1:43
|
||||
*/
|
||||
@FXController(path = "actionDemo/actionDemo.fxml")
|
||||
@FXWindow(title = "actionDemo", mainStage = true)
|
||||
@FXController(path = "bindDemo/bindDemo.fxml")
|
||||
@FXWindow(title = "bindDemo", mainStage = true)
|
||||
public class MainController extends FXBaseController implements Initializable {
|
||||
// View bind to View
|
||||
@FXML
|
||||
@FXBind("text=${@toUs(time.text)}")
|
||||
@FXBind("text=${inputTF.text}")
|
||||
private Label inputLabel;
|
||||
|
||||
@FXML
|
||||
private TextField inputTF;
|
||||
|
||||
// View bind to a Java Bean
|
||||
@FXML
|
||||
private Label usernameLabel;
|
||||
|
||||
@FXML
|
||||
private Label userPswLabel;
|
||||
|
||||
@FXML
|
||||
private TextField usernameTF;
|
||||
|
||||
@FXML
|
||||
private PasswordField pswPF;
|
||||
|
||||
@FXData
|
||||
@FXBind({
|
||||
"name=${usernameTF.text}",
|
||||
"password=${pswPF.text}"
|
||||
})
|
||||
private User user = new User();
|
||||
|
||||
// View bind to Expression
|
||||
@FXML
|
||||
private TextField money;
|
||||
|
||||
@FXML
|
||||
@FXBind("text=${@toUs(money.text)}")
|
||||
private Label us;
|
||||
|
||||
@FXML
|
||||
@FXBind("text=${@toJp(time.text)}")
|
||||
@FXBind("text=${@toJp(money.text)}")
|
||||
private Label jp;
|
||||
|
||||
@FXML
|
||||
@FXBind("text=${@toUk(time.text)}")
|
||||
@FXBind("text=${@toUk(money.text)}")
|
||||
private Label uk;
|
||||
|
||||
@FXML
|
||||
private TextField time;
|
||||
public void clickToShowInfo() {
|
||||
usernameLabel.setText(user.getName());
|
||||
userPswLabel.setText(user.getPassword());
|
||||
}
|
||||
|
||||
public String toUs(String value) {
|
||||
double money = Double.valueOf(value);
|
||||
@@ -56,14 +93,14 @@ public class MainController extends FXBaseController implements Initializable {
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
time.setText("0");
|
||||
time.textProperty().addListener(new ChangeListener<String>() {
|
||||
money.setText("0");
|
||||
money.textProperty().addListener(new ChangeListener<String>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
||||
if (null == newValue || "".equals(newValue)) {
|
||||
time.setText("0");
|
||||
money.setText("0");
|
||||
} else if (!newValue.matches("^[0-9]*$")) {
|
||||
time.setText(oldValue);
|
||||
money.setText(oldValue);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
package cn.edu.scau.biubiusuisui.example.actionDemo;
|
||||
package cn.edu.scau.biubiusuisui.example.bindDemo;
|
||||
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXEntity;
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXField;
|
||||
@@ -47,7 +47,7 @@ public class MainController extends FXBaseController implements Initializable {
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
student = (Student) FXEntityFactory.wrapFxBean(Student.class); // 从工厂中拿到将JavaBean转换得到的JavaFXBean
|
||||
student = (Student) FXEntityFactory.wrapFXBean(Student.class); // 从工厂中拿到将JavaBean转换得到的JavaFXBean
|
||||
Property listProperty = FXPlusContext.getEntityPropertyByName(student, "list");
|
||||
list.itemsProperty().bind(listProperty);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ public class LoginController extends FXBaseController {
|
||||
|
||||
@FXML
|
||||
public void registerClick() {
|
||||
System.out.println("点击注册.....");
|
||||
redirectToRegister();
|
||||
}
|
||||
|
||||
@@ -40,4 +39,11 @@ public class LoginController extends FXBaseController {
|
||||
public String redirectToDialog() {
|
||||
return "DialogController";
|
||||
}
|
||||
|
||||
@FXML
|
||||
@FXRedirect //登录成功
|
||||
public String redirectToSuccess() {
|
||||
|
||||
return "SuccessController";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.edu.scau.biubiusuisui.example.resizableDemo;
|
||||
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXScan;
|
||||
import cn.edu.scau.biubiusuisui.config.FXPlusApplication;
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
* @author suiyu_yang
|
||||
* @description 缩放和拖拽的示例
|
||||
* @date 2020/4/5 00:04
|
||||
* @email suiyu_yang@163.com
|
||||
*/
|
||||
@FXScan(base = "cn.edu.scau.biubiusuisui.example.resizableDemo")
|
||||
public class Demo extends Application {
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
FXPlusApplication.start(Demo.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.edu.scau.biubiusuisui.example.resizableDemo;
|
||||
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXController;
|
||||
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
|
||||
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.stage.StageStyle;
|
||||
|
||||
/**
|
||||
* @author suiyu_yang
|
||||
* @description 主控制器
|
||||
* @date 2020/4/5 00:05
|
||||
* @email suiyu_yang@163.com
|
||||
*/
|
||||
@FXController(path = "resizableDemo/resizableDemo.fxml")
|
||||
@FXWindow(mainStage = true, title = "resizableDemo", draggable = true, resizable = true, style = StageStyle.UNDECORATED)
|
||||
public class MainController extends FXBaseController {
|
||||
|
||||
@FXML
|
||||
public void closeWindowClick() {
|
||||
this.closeStage();
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class FXControllerFactory {
|
||||
|
||||
|
||||
private static final BeanBuilder BEAN_BUILDER = new FXBuilder();
|
||||
private static FXWindowParser windowAnnotationParser = new FXWindowParser();
|
||||
private static FXWindowParser fxWindowAnnotationParser = new FXWindowParser();
|
||||
|
||||
|
||||
/**
|
||||
@@ -154,7 +154,7 @@ public class FXControllerFactory {
|
||||
double preHeight = fxWindow.preHeight() == 0 ? fxBaseControllerProxy.getPrefHeight() : fxWindow.preHeight();
|
||||
Scene scene = new Scene(fxBaseControllerProxy, preWidth, preHeight);
|
||||
stage.setScene(scene);
|
||||
windowAnnotationParser.parse(stage, fxBaseControllerProxy, fxWindow);
|
||||
fxWindowAnnotationParser.parse(stage, fxBaseControllerProxy, fxWindow);
|
||||
|
||||
StageManager.getInstance().registerWindow(fxBaseControllerProxy); //注册舞台
|
||||
if (fxWindow.mainStage() == true) { //当是主舞台时,先show为敬
|
||||
@@ -252,7 +252,7 @@ public class FXControllerFactory {
|
||||
//建立代理
|
||||
try {
|
||||
Object fieldValue = field.get(fxControllerObject);
|
||||
Object fieldValueProxy = FXEntityFactory.wrapFxBean(fieldValue);
|
||||
Object fieldValueProxy = FXEntityFactory.wrapFXBean(fieldValue);
|
||||
field.set(fxControllerObject, fieldValueProxy);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -23,26 +23,26 @@ public class FXEntityFactory {
|
||||
private FXEntityFactory() {
|
||||
}
|
||||
|
||||
public static Object wrapFxBean(Class clazz) {
|
||||
return wrapFxBean(clazz, new FXBuilder());
|
||||
public static Object wrapFXBean(Class clazz) {
|
||||
return wrapFXBean(clazz, new FXBuilder());
|
||||
}
|
||||
|
||||
public static Object wrapFxBean(Class clazz, BeanBuilder beanBuilder) {
|
||||
public static Object wrapFXBean(Class clazz, BeanBuilder beanBuilder) {
|
||||
Object object = null;
|
||||
object = beanBuilder.getBean(clazz);
|
||||
if (object != null) {
|
||||
return wrapFxBean(object);
|
||||
return wrapFXBean(object);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object wrapFxBean(Object object) {
|
||||
public static Object wrapFXBean(Object object) {
|
||||
FXEntityProxy fxEntityProxy = new FXEntityProxy();
|
||||
Object objectProxy = null;
|
||||
try {
|
||||
objectProxy = fxEntityProxy.getInstance(object); // 初始化代理类
|
||||
processFXEntityProxy(object, objectProxy, fxEntityProxy);
|
||||
processFXEntityProxyFields(object, objectProxy, fxEntityProxy); //处理FXEntity上的@FXField
|
||||
FXPlusContext.setProxyByBeanObject(objectProxy, fxEntityProxy);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
@@ -50,7 +50,7 @@ public class FXEntityFactory {
|
||||
return objectProxy;
|
||||
}
|
||||
|
||||
private static void processFXEntityProxy(Object entity, Object proxy, FXEntityProxy fxEntityProxy) throws IllegalAccessException {
|
||||
private static void processFXEntityProxyFields(Object entity, Object proxy, FXEntityProxy fxEntityProxy) throws IllegalAccessException {
|
||||
Map<String, FXFieldWrapper> fxFieldWrapperMap = new HashMap<>();
|
||||
Field[] fields = entity.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
@@ -59,9 +59,7 @@ public class FXEntityFactory {
|
||||
Property property = null;
|
||||
field.setAccessible(true);
|
||||
FXField fxField = (FXField) annotation;
|
||||
FXFieldWrapper fieldWrapper = new FXFieldWrapper();
|
||||
fieldWrapper.setFxField(fxField);
|
||||
fieldWrapper.setType(field.getType());
|
||||
FXFieldWrapper fieldWrapper = new FXFieldWrapper(fxField, field.getType());
|
||||
if (field.get(entity) == null) {
|
||||
property = getFieldDefaultProperty(field);
|
||||
} else {
|
||||
@@ -136,6 +134,4 @@ public class FXEntityFactory {
|
||||
}
|
||||
return property;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package cn.edu.scau.biubiusuisui.function;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.Cursor;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
@@ -25,77 +24,82 @@ public class DragWindowHandlerImpl implements EventHandler<MouseEvent> {
|
||||
boolean isBottomRight;// 是否处于右下角调整窗口状态
|
||||
boolean isBottom;// 是否处于下边界调整窗口状态
|
||||
private Pane pane;
|
||||
private boolean fix = false;
|
||||
private boolean resizable; //是否拉伸
|
||||
private boolean draggable; //是否拖拽
|
||||
|
||||
public DragWindowHandlerImpl(Stage primaryStage, Pane pane,boolean fix) { //构造器
|
||||
|
||||
public DragWindowHandlerImpl(Stage primaryStage, Pane pane, boolean draggable, boolean resizable) { //构造器
|
||||
this.stage = primaryStage;
|
||||
this.pane = pane;
|
||||
this.fix = fix;
|
||||
this.draggable = draggable;
|
||||
this.resizable = resizable;
|
||||
}
|
||||
|
||||
public DragWindowHandlerImpl(Stage stage, double MIN_WIDTH, double MIN_HEIGHT, Pane pane, boolean fix) {
|
||||
public DragWindowHandlerImpl(Stage stage, double MIN_WIDTH, double MIN_HEIGHT, Pane pane, boolean draggable, boolean resizable) {
|
||||
this.stage = stage;
|
||||
this.MIN_WIDTH = MIN_WIDTH;
|
||||
this.MIN_HEIGHT = MIN_HEIGHT;
|
||||
this.pane = pane;
|
||||
this.fix = fix;
|
||||
this.draggable = draggable;
|
||||
this.resizable = resizable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MouseEvent e) {
|
||||
|
||||
if (e.getEventType() == MouseEvent.MOUSE_PRESSED) { //鼠标按下的事件
|
||||
//鼠标按下时记录坐标
|
||||
this.oldStageX = this.stage.getX();
|
||||
this.oldStageY = this.stage.getY();
|
||||
this.oldScreenX = e.getScreenX();
|
||||
this.oldScreenY = e.getScreenY();
|
||||
|
||||
} else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) { //鼠标拖动的事件
|
||||
//
|
||||
double nextX = stage.getX();
|
||||
double nextY = stage.getY();
|
||||
double nextWidth = stage.getWidth();
|
||||
double nextHeight = stage.getHeight();
|
||||
if(!fix) {
|
||||
double x = e.getSceneX();
|
||||
double y = e.getSceneY();
|
||||
// 保存窗口改变后的x、y坐标和宽度、高度,用于预判是否会小于最小宽度、最小高度
|
||||
|
||||
|
||||
if (isRight || isBottomRight) {// 所有右边调整窗口状态
|
||||
nextWidth = x;
|
||||
}
|
||||
if (isBottomRight || isBottom) {// 所有下边调整窗口状态
|
||||
nextHeight = y;
|
||||
}
|
||||
if (nextWidth <= MIN_WIDTH) {// 如果窗口改变后的宽度小于最小宽度,则宽度调整到最小宽度
|
||||
nextWidth = MIN_WIDTH;
|
||||
}
|
||||
if (nextHeight <= MIN_HEIGHT) {// 如果窗口改变后的高度小于最小高度,则高度调整到最小高度
|
||||
nextHeight = MIN_HEIGHT;
|
||||
}
|
||||
double x = e.getSceneX();
|
||||
double y = e.getSceneY();
|
||||
// 保存窗口改变后的x、y坐标和宽度、高度,用于预判是否会小于最小宽度、最小高度
|
||||
if (isRight || isBottomRight) {// 所有右边调整窗口状态
|
||||
nextWidth = x;
|
||||
}
|
||||
if (isBottomRight || isBottom) {// 所有下边调整窗口状态
|
||||
nextHeight = y;
|
||||
}
|
||||
if (nextWidth <= MIN_WIDTH) {// 如果窗口改变后的宽度小于最小宽度,则宽度调整到最小宽度
|
||||
nextWidth = MIN_WIDTH;
|
||||
}
|
||||
if (nextHeight <= MIN_HEIGHT) {// 如果窗口改变后的高度小于最小高度,则高度调整到最小高度
|
||||
nextHeight = MIN_HEIGHT;
|
||||
}
|
||||
|
||||
// 最后统一改变窗口的x、y坐标和宽度、高度,可以防止刷新频繁出现的屏闪情况
|
||||
if(isBottom ||isBottomRight ||isRight) {
|
||||
stage.setX(nextX);
|
||||
stage.setY(nextY);
|
||||
if (draggable) {
|
||||
if (isBottom || isBottomRight || isRight) {
|
||||
stage.setX(nextX);
|
||||
stage.setY(nextY);
|
||||
} else {
|
||||
this.stage.setX(e.getScreenX() - this.oldScreenX + this.oldStageX);
|
||||
this.stage.setY(e.getScreenY() - this.oldScreenY + this.oldStageY);
|
||||
}
|
||||
}
|
||||
if (resizable) {
|
||||
stage.setWidth(nextWidth);
|
||||
stage.setHeight(nextHeight);
|
||||
}else {
|
||||
this.stage.setX(e.getScreenX() - this.oldScreenX + this.oldStageX);
|
||||
this.stage.setY(e.getScreenY() - this.oldScreenY + this.oldStageY);
|
||||
}
|
||||
} else if (e.getEventType() == MouseEvent.MOUSE_MOVED) {
|
||||
if(!fix) {
|
||||
e.consume();
|
||||
double x = e.getSceneX();
|
||||
double y = e.getSceneY();
|
||||
double width = stage.getWidth();
|
||||
double height = stage.getHeight();
|
||||
Cursor cursorType = Cursor.DEFAULT;// 鼠标光标初始为默认类型,若未进入调整窗口状态,保持默认类型
|
||||
// 先将所有调整窗口状态重置
|
||||
isRight = isBottomRight = isBottom = false;
|
||||
} else if (e.getEventType() == MouseEvent.MOUSE_MOVED) { //鼠标移动
|
||||
e.consume();
|
||||
double x = e.getSceneX();
|
||||
double y = e.getSceneY();
|
||||
double width = stage.getWidth();
|
||||
double height = stage.getHeight();
|
||||
Cursor cursorType = Cursor.DEFAULT;// 鼠标光标初始为默认类型,若未进入调整窗口状态,保持默认类型
|
||||
// 先将所有调整窗口状态重置
|
||||
isRight = isBottomRight = isBottom = false;
|
||||
|
||||
if (resizable) {
|
||||
if (y >= height - RESIZE_WIDTH) {
|
||||
if (x <= RESIZE_WIDTH) {// 左下角调整窗口状态
|
||||
|
||||
@@ -111,7 +115,6 @@ public class DragWindowHandlerImpl implements EventHandler<MouseEvent> {
|
||||
cursorType = Cursor.E_RESIZE;
|
||||
}
|
||||
// 最后改变鼠标光标
|
||||
|
||||
pane.setCursor(cursorType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@ public class FXWindowParser {
|
||||
stage.setTitle(fxWindow.title());
|
||||
|
||||
if (fxWindow.resizable()) {
|
||||
stage.setResizable(false);
|
||||
stage.setResizable(true);
|
||||
}
|
||||
|
||||
if (fxWindow.draggable()) {
|
||||
final int RESIZE_WIDTH = 5;// 判定是否为调整窗口状态的范围与边界距离
|
||||
EventHandler dragWindowHandler = new DragWindowHandlerImpl(stage, fxWindow.minWidth(), fxWindow.minHeight(), fxControllerProxy, fxWindow.resizable());
|
||||
if (fxWindow.draggable() || fxWindow.resizable()) {
|
||||
EventHandler dragWindowHandler = new DragWindowHandlerImpl(stage, fxWindow.minWidth(), fxWindow.minHeight(), fxControllerProxy, fxWindow.draggable(), fxWindow.resizable());
|
||||
fxControllerProxy.setOnMousePressed(dragWindowHandler);
|
||||
fxControllerProxy.setOnMouseDragged(dragWindowHandler);
|
||||
fxControllerProxy.setOnMouseMoved(dragWindowHandler);
|
||||
}
|
||||
stage.initStyle(fxWindow.style());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user