【发布时间】:2016-04-09 17:34:24
【问题描述】:
下面的代码创建了一个填充有按钮的网格。我想知道如何添加一种方法来弹出另一个带有数字的网格框。在第二个弹出网格框中选择数字时,它会更改单击的原始按钮上的标签。以下面的示例为例,有人单击带有文本“1”的按钮。将弹出一个带有标记为 1 到 5 的按钮的网格。单击按钮 5。弹出的网格框消失了,上面写着“1”的按钮现在变成了“5”。
import javafx.application.*;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
public class GUI extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override public void start(Stage primaryStage)
{
final int HGAP = 2;
final int VGAP = 2;
final int BUTTONSIZE = 50;
final int INSET = 5;
final int SIZE = 4;
GridPane root = new GridPane();
root.setPadding(new Insets(INSET));
root.setHgap(HGAP);
root.setVgap(VGAP);
root.setAlignment(Pos.CENTER);
final Button[][] btn = new Button[SIZE][SIZE];
final Paint background = Color.TURQUOISE;
int index = 0;
for ( int theCol = 0; theCol < SIZE; theCol++) {
for ( int theRow = 0; theRow < SIZE; theRow++) {
btn[theRow][theCol] = new Button(""+ index);
btn[theRow][theCol].setPrefSize(BUTTONSIZE, BUTTONSIZE);
root.add(btn[theRow][theCol], theRow, theCol);
index++;
btn[theRow][theCol].setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent arg0)
{
Button b= (Button)arg0.getSource();
System.out.println(b.getText());
}
});
}
}
Scene scene = new Scene(root,background);
primaryStage.setTitle("Grid");
primaryStage.setScene(scene);
primaryStage.show();
}
}
【问题讨论】:
-
我认为有些链接是临时链接,其中包含代码示例