如何解决如何处理 JavaBeanIntegerProperty 中的异常
我的项目分为两个模块:
在模型模块中:
我有一个类 SudokuField
会在更改时通知 SudokuBoard
。然后,如果启用提示,SudokuBoard
会抛出带有一些信息的 SudokuException
,如果无法用插入的值解决数独。
SudokuField
通知:
public class SudokuField {
private int value = 0;
private PropertyChangeSupport notifier;
public void setValue(int value) {
if (value < 0 || value > 9) {
throw new IndexOutOfBoundsException("Number: " + value + "Expected 0-9");
}
int oldValue = this.value;
this.value = value;
notifier.firePropertyChange("value",oldValue,value);
}
}
SudokuBoard
投掷:
public class SudokuBoard implements PropertyChangeListener{
public static final int SIZE = 9;
@Override
public void propertyChange(PropertyChangeEvent evt) throws SudokuException {
if (!autoCheck) {
return;
}
if (checkBoard()) {
return;
}
int oldValue = (int) evt.getOldValue();
int newValue = (int) evt.getNewValue();
SudokuField field = (SudokuField) evt.getSource();
int i=0,j=0;
outerloop:
for (i = 0; i < SudokuBoard.SIZE; i++) {
for (j = 0; j < SudokuBoard.SIZE; j++) {
if (this.getField(i,j) == field ) {
break outerloop;
}
}
}
throw new SudokuException(
"Wrong value inserted"
+ "(old: " + oldValue
+ ",new: " + newValue + ")",i,j,newValue,field);
}
}
在查看模块中:
我正在使用 JavaFX 和 JavaBean 来显示和同步 SudokuField
和 TextField
。
我的控制器的一部分:
public class Window implements Initializable {
@FXML
private GridPane sudokuPane;
private final SudokuBoard board = new SudokuBoard(new BacktrackingSudokuSolver());
private final List<List<TextField>> fields = Arrays.asList(new List[9]);
private final List<JavaBeanIntegerProperty> integerProperties =
new ArrayList<JavaBeanIntegerProperty>();
public Window() {
for (int i = 0; i < SudokuBoard.SIZE; i++) {
fields.set(i,Arrays.asList(new TextField[9]));
for (int j = 0; j < SudokuBoard.SIZE; j++) {
TextField textField = new TextField();
fields.get(i).set(j,textField);
}
}
}
@Override
public void initialize(URL url,ResourceBundle resourceBundle) {
StringConverter converter = new IntegerStringConverter();
for (int i = 0; i < SudokuBoard.SIZE; i++) {
for (int j = 0; j < SudokuBoard.SIZE; j++) {
//I generate Sudoku board and TextFields in Constructor
TextField textField = getTextField(i,j);
SudokuField field = board.getField(i,j);
JavaBeanIntegerPropertyBuilder builder = JavaBeanIntegerPropertyBuilder.create();
JavaBeanIntegerProperty integerProperty = null;
try {
integerProperty = builder.bean(field).name("value").build();
} catch (NoSuchMethodException e) {
System.out.println("Ignoring NoSuchMethodException in " + this.getClass());
}
//i need to store JavaBeanIntegerProperty,otherwise GarbageCollector
//destroys my connections
integerProperties.add(integerProperty);
//adding textField to GridPane
sudokuPane.add(textField,j);
textField.textProperty().bindBidirectional(integerProperty,converter);
}
}
}
}
现在最大的问题是:如何在 Controller 的某处处理那些 SudokuExceptions
?从此异常中,我可以轻松获取任何信息来修改所需的 textField,但是我无法处理它。我尝试添加:
Thread.setDefaultUncaughtExceptionHandler(Window::exceptionHandler);
//and
Thread.currentThread().setDefaultUncaughtExceptionHandler(Window::exceptionHandler);
exceptionHandler
控制器中的 Window
方法:
public static void exceptionHandler(Thread thread,Throwable throwable) {
for (int i = 0; i < 100; i++) {
System.out.println("working: " + i);
}
}
在 initialize
和 Controller 的构造函数中以及在 Main
和 main
方法中的 view 模块的 start
类中,但它不是调用这个方法(我用 System.out.println
检查过)。我不想将 PropertyChangeListener
添加到 SudokuBoard
,因为我想对我的 Model 模块进行尽可能少的更改。
整个项目是作为我大学课程的一部分开发的项目。
解决方法
好的,所以我找到了这个 bug report,我想正因为如此,我不能做我想要的。因此,我创建了自己的 ActionEvent
,它在 SudokuException
中被“抛出”而不是 SudokuBoard
,并将 ActionEventListener
接口添加到我的控制器。也许这不是我想要的解决方案,但我认为它更漂亮。特别感谢帮助我的朋友。我将这个问题保持开放,因为也许有人知道如何解决这个异常处理问题,而其他人可以使用这个答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。