下面的代码创建一个填充了按钮的Grid . 我想知道如何添加一个方法,弹出另一个带有数字的网格框 . 在第二个弹出网格框中选择数字时,它会更改单击的原始按钮上的标签 . 以下面的示例为例,有人点击了带有文本“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();
}
}
2 回答
你可以使用
PopupControl
:在按钮侦听器中,您可以调用
popup.hide()
来关闭弹出窗口并更新按钮文本你也可以尝试这样的方式