gpt4 book ai didi

JavaFX:如何选择图 block 并添加数字和边框线?

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:49 24 4
gpt4 key购买 nike

我是初学者,我想制作一款数独游戏。我在 private Parent createContent() 中创建了 9x9 网格,但我不知道如何在 block 之间制作粗体线。我还尝试单击并突出显示一个单元格,然后按一个键并向其中添加数字。

<小时/>

这是我到目前为止所做的事情:

public class TestJAVAfx extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(541,700);

for(int i = 0 ; i<9; i++){
for(int j=0; j<9; j++){
Tile tile = new Tile();
tile.setTranslateX(j*60);
tile.setTranslateY(i*60);

root.getChildren().addAll(tile);
}
}

return root;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene (createContent ()));
primaryStage.show();
}

public class Tile extends StackPane{

private Text text = new Text();

public Tile(){
Rectangle border = new Rectangle(60,60);
border.setFill(null);
border.setStroke(Color.BLACK);

setAlignment(Pos.CENTER);
getChildren().addAll(border,text);


setOnMouseClicked(e->{
if(e.getButton()== MouseButton.PRIMARY){
getKey();
}
});



}

private Text getKey(){
Text key = new Text();

setOnKeyPressed(e->{
KeyCode keyCode = e.getCode();
if(keyCode.equals(KeyCode.A)){
drawONE();
}
});


return key;
}

private void drawONE(){
text.setText("1");
}
private void drawTWO(){
text.setText("2");
}
private void drawTHREE(){
text.setText("3");
}
private void drawFOUR(){
text.setText("4");
}
private void drawFIVE(){
text.setText("5");
}
private void drawSIX(){
text.setText("6");
}
private void drawSEVEN(){
text.setText("7");
}
private void drawEIGHT(){
text.setText("8");
}
private void drawNINE(){
text.setText("9");
}
}

}

最佳答案

我不确定 block 之间的粗线是什么意思。要使线条加粗,您可以将描边宽度设置为 2。要突出显示特定图 block ,您可以保留对其的引用,并在选择一个图 block 时应用样式。这样,您还将获得对要使用文本更新的图 block 的引用。

下面是您的演示的修改后的代码。这应该会给您一些关于如何继续的基本想法。

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TestJAVAfx extends Application {

private ObjectProperty<Tile> focusedTile = new SimpleObjectProperty<>();

public static void main(String[] args) {
launch(args);
}

private Parent createContent() {
Pane root = new Pane();
root.setPrefSize(542, 542);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
Tile tile = new Tile();
tile.setTranslateX(j * 60);
tile.setTranslateY(i * 60);
root.getChildren().addAll(tile);
}
}
return root;
}

@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.show();
focusedTile.addListener((obs, oldTile, tile) -> {
if (oldTile != null) {
oldTile.getBorderRectangle().setStroke(Color.BLACK);
}
if (tile != null) {
tile.getBorderRectangle().setStroke(Color.ORANGE);
tile.toFront();
}
});

scene.setOnKeyTyped(e -> {
if (focusedTile.get() != null) {
if (Character.isDigit(e.getCharacter().charAt(0))) {
focusedTile.get().getText().setText(e.getCharacter());
}
}
});
}

public class Tile extends StackPane {
private Text text = new Text();
private Rectangle borderRectangle;

public Tile() {
borderRectangle = new Rectangle(60, 60);
borderRectangle.setFill(null);
borderRectangle.setStroke(Color.BLACK);
borderRectangle.setStrokeWidth(2);
setAlignment(Pos.CENTER);
getChildren().addAll(borderRectangle, text);
setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
focusedTile.set(this);
}
});
}

public Rectangle getBorderRectangle() {
return borderRectangle;
}

public Text getText() {
return text;
}
}
}

关于JavaFX:如何选择图 block 并添加数字和边框线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52806377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com