java
1package application; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Scene; 6import javafx.scene.layout.Pane; 7import javafx.stage.Stage; 8 9 10public class Main extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 Pane root = FXMLLoader.load(getClass().getResource("Sample.fxml")); 15 Scene scene = new Scene(root,400,400); 16 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 17 primaryStage.setScene(scene); 18 primaryStage.setTitle("テスト"); 19 primaryStage.show(); 20 } catch(Exception e) { 21 e.printStackTrace(); 22 } 23 } 24 25 public static void main(String[] args) { 26 launch(args); 27 } 28} 29
java
1package application; 2 3import java.net.URL; 4import java.util.ResourceBundle; 5 6import javafx.fxml.FXML; 7import javafx.fxml.Initializable; 8import javafx.scene.canvas.Canvas; 9import javafx.scene.canvas.GraphicsContext; 10import javafx.scene.control.Button; 11import javafx.scene.control.Label; 12 13public class SampleController implements Initializable { 14 15 @FXML Button button1; 16 @FXML Label label1; 17 @FXML Canvas canvas1; 18 19 20 @Override 21 public void initialize(URL location, ResourceBundle resources) { 22 // TODO 自動生成されたメソッド・スタブ 23 24 button1.setOnAction((ActionEvent) -> { 25 System.out.println("テスト"); 26 27 label1.setText("ラベルのテスト"); 28 29 GraphicsContext gc = canvas1.getGraphicsContext2D(); 30 31 gc.setLineWidth(2.0); 32 33 34 35 36 }); 37 } 38 39} 40
FXML
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.canvas.*?> 4<?import java.lang.*?> 5<?import javafx.scene.control.*?> 6<?import javafx.scene.layout.*?> 7<?import javafx.scene.layout.AnchorPane?> 8 9<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" 10 minHeight="-Infinity" minWidth="-Infinity" prefHeight="139.0" 11 prefWidth="259.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 12 fx:controller="application.SampleController"> 13 <top> 14 <Label fx:id="label1" text="Label" BorderPane.alignment="CENTER" /> 15 </top> 16 <bottom> 17 <Button fx:id="button1" mnemonicParsing="false" text="Button" 18 BorderPane.alignment="CENTER" /> 19 </bottom> 20 <center> 21 <Canvas fx:id="canvas1" height="200.0" width="200.0" 22 BorderPane.alignment="CENTER" /> 23 </center> 24</BorderPane> 25
上記のコードでフォーム上に線を引きたいのですがどのようにしたらいいのでしょうか?
初めから表示させる方法とイベントで表示させる方法2種類お願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。