package lab7;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
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 cool extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane bp = new BorderPane();
bp.getStyleClass().add("boop");
GridPane gp = new GridPane();
GridPane gp2 = new GridPane();
Label label1 = new Label("Reverse Multiplication Table");
label1.getStyleClass().add("label1");
HBox counterBox = new HBox();
counterBox.getStyleClass().add("label1");
counterBox.getChildren().add(label1);
Label text = new Label("Enter Answer: ");
text.getStyleClass().add("text");
TextField textx = new TextField();
gp.getStyleClass().add("textx");
Button b = new Button();
b.setText("Find Problems");
Scene sc = new Scene(bp);
sc.getStylesheets().add("Styles/Styles.css");
for (int row = 0; row < 11; row++)
for (int col = 0; col < 11; col++) {
// This will create the first column
if (row == 0) {
Label columns = new Label(" " + col);
gp2.add(columns, 3, col);
columns.getStyleClass().add("columnStyle");
}
else if (col == 0) {
// This will create the first row
Label rows = new Label("" + row);
gp2.add(rows, row, 0);
rows.getStyleClass().add("rowStyle");
} else {
final int a = row;
final int z = col;
final Label table = new Label(row + " x " + col);
table.getStyleClass().add("table");
table.setMinWidth(150);
gp2.add(table, row, col);
b.getStyleClass().add("buttons");
b.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<Event>() {
boolean Answer = true;
@Override
public void handle(Event event) {
if (Answer = true) {
int read = (Integer.parseInt(textx
.getText()));
if (read == (a * z)) {
table.setText(a + " x " + z);
table.getStyleClass().add("ans");
}
}
}
});
}
}
gp.add(text, 1, 0);
gp.add(textx, 2, 0);
gp.add(b, 3, 0);
bp.setTop(counterBox);
bp.setCenter(gp);
bp.setBottom(gp2);
primaryStage.setScene(sc);
primaryStage.show();
primaryStage.setTitle("Lab7");
}
public static void main(String args[]) {
Application.launch(args);
}
}
Styles.css中
.columnStyle{
-fx-padding: 5px;
-fx-border-width: 1px;
-fx-text-fill:gold;
}
.rowStyle{
-fx-padding: 64px;
-fx-border-width: 1px;
-fx-text-fill:gold;
}
.table{
-fx-padding: 10px;
-fx-border-width: 1px;
-fx-border-style: solid;
-fx-background-color: yellow;
-fx-text-fill:red;
}
.boop{
-fx-padding: 20px;
-fx-font-size: 100%;
-fx-background-color:black;
}
.label1{
-fx-background-color: white;
-fx-alignment: center;
-fx-text-fill:red;
}
.textx{
-fx-alignment: center;
}
.text{
-fx-text-fill:gold;
}
.ans{
-fx-background-color:white;
}
.ans2{
-fx-background-color:gold;
}
.button{
-fx-text-fill:red;
}
我收到此错误 2015年5月25日下午8:41:26 com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged 警告:资源&#34; Styles / Styles.css&#34;没找到。
我在运行程序时遇到了麻烦。现在我从Styles.css得到一个没有任何内容的普通表,而且当我提出解决方案时,它没有选择正确的乘法选择。
答案 0 :(得分:1)
sc.getStylesheets().add("Styles/Styles.css");
这是你应该添加样式表的方法:
sc.getStylesheets().add(getClass().getResource("Styles/Styles.css").toExternalForm());
如果它不起作用,请分享Styles.css的确切路径。它应该是src / Styles / Styles.css。
注意:根据约定,java中的包名称应以小写字符开头。