0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaFX ボタンとラベル

Posted at
Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }

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

Contololler.java

package sample;

import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class Controller {
    public Button btn01;
    public Label lb01;

    public void onButtonClicked() {
        String string;
        string = "Button clicked";

        lb01.setText(string);

    }
}


sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <Button fx:id="btn01" mnemonicParsing="false" onAction="#onButtonClicked" text="Button" GridPane.columnIndex="1" GridPane.rowIndex="1" />
   <Label fx:id="lb01" prefHeight="17.0" prefWidth="86.0" text="Label" />

</GridPane>

Mainでボタンをつけなくてよい

ボタンをおすとラベルがかわる

btn.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?