gpt4 book ai didi

java - 逆乘法表javafx

转载 作者:行者123 更新时间:2023-11-29 03:12:02 24 4
gpt4 key购买 nike

对于这个应用程序,我需要输入一个数字并显示这些数字,当这些数字相乘时就会得到所讨论的数字。例如,如果您输入 42,则 6*7 和 7*6 的标签会改变颜色。我想出了如何得到答案,但我不太明白如何操纵乘法表中的标签来改变颜色。给你一个想法,

enter image description here

主类

package application;

import java.util.List;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
pane.setTop(getHbox1());

HBox prompt = new HBox(15);
prompt.setPadding(new Insets(15, 15, 15, 15));
prompt.setAlignment(Pos.TOP_CENTER);
prompt.getStyleClass().add("hbox2");

Label lblProblem = new Label("Enter problem: ");
prompt.getChildren().add(lblProblem);

TextField tfProblem = new TextField();
prompt.getChildren().add(tfProblem);

Button btnFindAnswer = new Button("Find answers");
btnFindAnswer.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>() {

@Override
public void handle(Event arg0) {
int x = showFactors(tfProblem);

}

});

prompt.getChildren().add(btnFindAnswer);

pane.setCenter(prompt);
pane.setBottom(setUpGrid());

Scene scene = new Scene(pane, 550, 650);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("lab 7");
primaryStage.setScene(scene);
primaryStage.show();

}

private HBox getHbox1() {
HBox hbox = new HBox(15);
hbox.setPadding(new Insets(15, 15, 15, 15));
hbox.setAlignment(Pos.TOP_CENTER);
hbox.getStyleClass().add("hbox1");

Label lblProblem = new Label("Reverse Multiplication Table");
hbox.getChildren().add(lblProblem);

return hbox;
}

public GridPane setUpGrid() {
GridPane pane = new GridPane();
Label[][] labels = new Label[11][11];

for (int row = 0; row < 11; row++)
for (int col = 0; col < 11; col++) {
Label l = new Label();
setUpLabel(l, col, row);
labels[row][col] = l;
pane.add(l, col, row);
}

return pane;
}

public void setUpLabel(final Label l, final int col, final int row) {
l.setPrefHeight(50);
l.setPrefWidth(50);
l.setAlignment(Pos.CENTER);
l.setStyle("-fx-stroke-border: black; -fx-border-width: 1;");
String a = String.valueOf(row);
String b = String.valueOf(col);

if (row == 0 || col == 0) {
l.getStyleClass().add("gridBorders");

if(row == 0)
l.setText(b);
else if (col == 0)
l.setText(a);
} else {
l.setText(a + " * " + b);
l.getStyleClass().add("gridInside");

}
}

public int showFactors(TextField problem) {
FactorCalculator calc = new FactorCalculator();
int number = Integer.parseInt(problem.getText());
List<Integer> factors = calc.findFactor(number);

for(int i = 0; i < factors.size() - 1; i++) {
return factors.get(i);

}

return 0;
}

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

}

factorCalculator 类

package application;

import java.util.ArrayList;
import java.util.List;

public class FactorCalculator {
public List<Integer> list = new ArrayList<Integer>();

public List<Integer> findFactor(int problem) {

int incrementer = 1;

if(problem % 2 != 0) {
incrementer = 2;
}

while(incrementer <= problem) {
if(problem % incrementer == 0) {
list.add(incrementer);
}

incrementer++;
}
return list;
}

}

应用程序 CSS

{
-fx-text-alignment: center;
}

.hbox1 {
-fx-background-color: gray;


}

.hbox2 {
-fx-background-color: white;

}

.gridBorders {
-fx-background-color: gray;
-fx-text-fill:#A3FF47;
-fx-border-style: solid;
-fx-border-width: 1;
-fx-stroke-border: black;

}

.gridInside {
-fx-background-color: red;
-fx-text-fill: white;
-fx-border-style: solid;
-fx-border-width: 1;
-fx-stroke-border: black;
}

.gridAnswer {
-fx-background-color: white;
-fx-text-fill: black;

}

最佳答案

只需使用您的样式“gridAnswer”并进行设置

l.getStyleClass().add( "gridAnswer");

或删除它

l.getStyleClass().remove( "gridAnswer");

根据您的需要。


编辑:我可以建议一种不同的方法吗?

只需创建一个包含您需要的所有信息的自定义单元格。像这样:

private class AnswerCell extends Label {

int a;
int b;
int value;

public AnswerCell( int a, int b) {
this.a = a;
this.b = b;
this.value = a * b;
setText( a + " * " + b);
}

public boolean matches( int matchValue) {
return value == matchValue;
}

public void highlight() {
getStyleClass().add( "gridAnswer");
}

public void unhighlight() {
getStyleClass().remove( "gridAnswer");
}
}

在您的设置方法中,您只需添加单元格并将它们放入全局列表中:

List<AnswerCell> answerCells = new ArrayList<>();

要找到答案,您可以这样做:

for( AnswerCell cell: answerCells) {
cell.unhighlight();
}

for( AnswerCell cell: answerCells) {
if( cell.matches(number)) {
cell.highlight();
}
}

关于java - 逆乘法表javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28824672/

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