Skip to content

Commit

Permalink
查明无法输入的问题出在sqlTools中,具体情况未知。
Browse files Browse the repository at this point in the history
  • Loading branch information
iammmmmmm committed Sep 9, 2024
1 parent 4c11b83 commit 6996e14
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 108 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<!-- </bundlesList>-->
<reflectionList>
<list>caidanci.HelloController</list>
<list>caidanci.SqlTools</list>
<list>com.gluonhq.attach</list>
<list>com.gluonhq.attach.util.Services</list>
<list>com.gluonhq.attach.storage.StorageService</list>
Expand Down
20 changes: 0 additions & 20 deletions src/android/AndroidManifest.xml

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/caidanci/HelloApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void start(Stage stage) throws IOException {
Scene scene = new Scene(root, size[0], size[1]);

var tm = ThemeManager.getInstance();
tm.setScene(scene);
tm.addScene(scene);

stage.setTitle("Hello!");
stage.setResizable(false);
Expand Down
84 changes: 45 additions & 39 deletions src/main/java/caidanci/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,47 @@ void inputFiledKeyRelease(KeyEvent event) {
*/
@FXML
void inputButtonClicked() {
String inputText = inputTextFiled.getText();
if (inputText != null && !inputText.isEmpty()) {
if (inputText.length() == wordLength) {
if (checkWord(inputText)) {
if (inputTime == 0) {
showAlert(Alert.AlertType.INFORMATION, "不,你失败了!", "你耗尽了机会!",
"答案:" + answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
} else {
inputTime--;
if (wordList.contains(inputText)) {
showAlert(Alert.AlertType.WARNING, "不", "你已经猜过这个单词了!",
inputText + " :\n" + sqlTools.getWordInfo(inputText));
inputTextFiled.clear();
} else {
wordList.add(inputText);
refresh(inputText);
if (inputText.equals(answerWord)) {
showAlert(Alert.AlertType.INFORMATION, "胜利!", "恭喜你,成功猜出单词!",
answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
}
}
}
} else {
showAlert(Alert.AlertType.ERROR, "错误!", "输入的单词错误!", "应该输入一个正确的单词");
}
} else {
showAlert(Alert.AlertType.WARNING, "错误!", "输入格式错误!",
"你应该输入一个单词,且该单词长度为" + wordLength + "!");
}
}
String inputText = inputTextFiled.getText().toLowerCase();
inputTextFiled.clear();
// inputCheck(inputText);

}

private void inputCheck(String inputText) {
Platform.runLater(() -> {
if (inputText.isEmpty()) {
return;
}
if (inputText.length() != wordLength) {
showAlert(Alert.AlertType.WARNING, "错误!", "输入格式错误!", "你应该输入一个单词,且该单词长度为" + wordLength + "!");
return;
}
if (!checkWord(inputText)) {
showAlert(Alert.AlertType.ERROR, "错误!", "输入的单词错误!", "应该输入一个正确的单词");
return;
}
if (inputTime == 0) {
showAlert(Alert.AlertType.INFORMATION, "不,你失败了!", "你耗尽了机会!", "答案:" + answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
return;
}
if (wordList.contains(inputText)) {
showAlert(Alert.AlertType.WARNING, "不", "你已经猜过这个单词了!", inputText + " :\n" + sqlTools.getWordInfo(inputText));
inputTextFiled.clear();
return;
}
inputTime--;
wordList.add(inputText);
refresh(inputText);
if (inputText.equals(answerWord)) {
showAlert(Alert.AlertType.INFORMATION, "胜利!", "恭喜你,成功猜出单词!", answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
}
if (inputTime == 0) {
showAlert(Alert.AlertType.INFORMATION, "不,你失败了!", "你耗尽了机会!", "答案:" + answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
}
});
}

/**
Expand All @@ -139,8 +147,7 @@ private void refresh(String inputText) {
Label label = new Label(temp);
label.setPrefSize(charSize, charSize);
label.setAlignment(Pos.CENTER); // 居中对齐
label.setBackground(
new Background(new BackgroundFill(getColor(temp, i), CornerRadii.EMPTY, null)));
label.setBackground(new Background(new BackgroundFill(getColor(temp, i), CornerRadii.EMPTY, null)));
label.setStyle("-fx-border-color: black; -fx-border-width: 1px;"); // 设置边框样式
outputGrid.add(label, i, rowIndex); // 放置在第一行的不同列
}
Expand All @@ -162,9 +169,8 @@ private boolean checkWord(String word) {
*/
@FXML
void initialize() {
Application.setUserAgentStylesheet(theme[themeFlag]);
Font a = Font.loadFont(
Objects.requireNonNull(this.getClass().getResourceAsStream("fonts/fzjt.ttf")), 20);
// Application.setUserAgentStylesheet(theme[themeFlag]);
Font a = Font.loadFont(Objects.requireNonNull(this.getClass().getResourceAsStream("fonts/fzjt.ttf")), 20);
Platform.runLater(() -> tm.setFontFamily(a.getFamily()));
tools.makeFontFamilyChooser(fontChose);
changeTheme.setGraphic(new FontIcon(BootstrapIcons.MOON));
Expand All @@ -174,8 +180,7 @@ void initialize() {
inputButton.setDisable(true);
inputTextFiled.setDisable(true);
gameIsStart = false;
SpinnerValueFactory<Integer> valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(
3, 10, wordLength);
SpinnerValueFactory<Integer> valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(3, 10, wordLength);
levelChose.setValueFactory(valueFactory);
levelChose.valueProperty().addListener((observable, oldValue, newValue) -> {
wordLength = newValue;
Expand Down Expand Up @@ -274,6 +279,7 @@ private void answerUpdate() {
*/
private void showAlert(Alert.AlertType alertType, String title, String header, String content) {
Alert alert = new Alert(alertType);
tm.addScene(alert.getDialogPane().getScene());
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/caidanci/SqlTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public SqlTools() {
System.out.println("this is check ");
} catch (SQLException e) {
System.err.println("连接数据库失败:" + e.getMessage());
e.printStackTrace();
}
}

Expand All @@ -77,9 +78,10 @@ void close() {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
System.out.println("关闭数据库连接成功!");
System.err.println("关闭数据库连接成功!");
}

String getAnswer(int limit) {
Expand All @@ -95,7 +97,8 @@ String getAnswer(int limit) {
}
}
} catch (SQLException e) {
System.out.println("查询数据库失败:" + e.getMessage());
System.err.println("查询数据库失败:" + e.getMessage());
e.printStackTrace();
}
return resultWord;
}
Expand All @@ -114,7 +117,9 @@ boolean checkWord(String word) {
}
}
} catch (SQLException e) {
System.out.println("查询数据库失败:" + e.getMessage());

System.err.println("查询数据库失败:" + e.getMessage());
e.printStackTrace();
}
return isWord;
}
Expand All @@ -131,7 +136,8 @@ public String getWordInfo(String answerWorld) {
}
}
} catch (SQLException e) {
System.out.println("查询数据库失败:" + e.getMessage());
System.err.println("查询数据库失败:" + e.getMessage());
e.printStackTrace();
}
return resultWord;
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/caidanci/ThemeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import javafx.css.PseudoClass;
import javafx.scene.Scene;

import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand All @@ -18,8 +15,8 @@ public class ThemeManager {
private static final PseudoClass USER_CUSTOM = PseudoClass.getPseudoClass("user-custom");
private final Map<String, String> customCSSDeclarations = new LinkedHashMap<>(); // -fx-property | value;
private final Map<String, String> customCSSRules = new LinkedHashMap<>(); // .foo | -fx-property: value;
private final List<Scene> scene = new ArrayList<>();
private String fontFamily = "FZKai-Z03S";
private Scene scene;

public static ThemeManager getInstance() {
return InstanceHolder.INSTANCE;
Expand Down Expand Up @@ -52,20 +49,23 @@ private void reloadCustomCSS() {
css.append(v);
css.append("}\n");
});
for (Scene scene : getScene()) {

scene.getRoot().getStylesheets().removeIf(uri -> uri.startsWith("data:text/css"));
scene.getRoot().getStylesheets().add("data:text/css;base64," + Base64.getEncoder().encodeToString(css.toString().getBytes(UTF_8)));
scene.getRoot().pseudoClassStateChanged(USER_CUSTOM, true);
}

getScene().getRoot().getStylesheets().removeIf(uri -> uri.startsWith("data:text/css"));
getScene().getRoot().getStylesheets().add(
"data:text/css;base64," + Base64.getEncoder().encodeToString(css.toString().getBytes(UTF_8))
);
getScene().getRoot().pseudoClassStateChanged(USER_CUSTOM, true);
}

public Scene getScene() {
public List<Scene> getScene() {
return scene;
}

public void setScene(Scene scene) {
this.scene = scene;
public void addScene(Scene scene) {
this.scene.add(scene);
//对新加入scene生效css
reloadCustomCSS();
}

private void setCustomDeclaration(String property, String value) {
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/caidanci/tools.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package caidanci;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.ComboBox;
Expand Down Expand Up @@ -76,21 +77,9 @@ protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!(empty || item == null)) {
setText(item);
setStyle("-fx-font-family: '" + item + "'; ");
Platform.runLater(() -> setStyle("-fx-font-family: '" + item + "'; "));
}
}
});
// Set button cell to display selected font
fontFamilyChooser.setButtonCell(new ListCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!(empty || item == null)) {
setText(item);
setStyle("-fx-font-family: '" + item + "';");
}
}
});

}
}
41 changes: 24 additions & 17 deletions src/main/resources/caidanci/hello-view.fxml
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" BorderPane.alignment="CENTER"
xmlns="http://javafx.com/javafx/17" fx:controller="caidanci.HelloController">
<StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" BorderPane.alignment="CENTER" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="caidanci.HelloController">
<StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<BorderPane prefHeight="606.0" prefWidth="337.0" BorderPane.alignment="CENTER">
<center>
<VBox alignment="TOP_CENTER" spacing="10.0" BorderPane.alignment="CENTER">
<GridPane fx:id="outputGrid" prefHeight="362.0" prefWidth="331.0">
</GridPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="333.0">
<Label text="单词长度:"/>
<Spinner fx:id="levelChose" prefHeight="23.0" prefWidth="154.0"/>
<Label text="单词长度:" />
<Spinner fx:id="levelChose" prefHeight="23.0" prefWidth="154.0" />
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="322.0">
<Button fx:id="inputButton" mnemonicParsing="false"
onMouseClicked="#inputButtonClicked" onTouchReleased="#inputButtonTouched"
text="输入" textAlignment="CENTER"/>
<TextField fx:id="inputTextFiled" onKeyReleased="#inputFiledKeyRelease"/>
<Button fx:id="inputButton" mnemonicParsing="false" onAction="#inputButtonClicked" onMouseClicked="#inputButtonClicked" onTouchReleased="#inputButtonTouched" text="输入" textAlignment="CENTER" />
<TextField fx:id="inputTextFiled" onKeyReleased="#inputFiledKeyRelease" />
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="322.0">
<Button fx:id="startGame" mnemonicParsing="false" onMouseClicked="#startGame"
text="开始游戏"/>
<Button fx:id="startGame" mnemonicParsing="false" onMouseClicked="#startGame" text="开始游戏" />
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="322.0">
<Button fx:id="changeTheme" mnemonicParsing="false" onAction="#changeTheme"/>
<Button fx:id="info" mnemonicParsing="false" onMouseClicked="#infoButtonClicked"/>
<ComboBox fx:id="fontChose" prefWidth="150.0"/>
<Button fx:id="changeTheme" mnemonicParsing="false" onAction="#changeTheme" />
<Button fx:id="info" mnemonicParsing="false" onMouseClicked="#infoButtonClicked" />
<ComboBox fx:id="fontChose" prefWidth="150.0" />
</FlowPane>
<TextArea prefHeight="79.0" prefWidth="337.0" />
</VBox>
</center>
</BorderPane>
Expand Down

0 comments on commit 6996e14

Please sign in to comment.