Created
December 26, 2011 16:31
-
-
Save aoetk/1521551 to your computer and use it in GitHub Desktop.
SwingのEDT上でMouseInfoに触れば大丈夫かどうかを確認するための簡単なサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mouseinfosample; | |
import java.awt.MouseInfo; | |
import java.awt.Point; | |
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.LabelBuilder; | |
import javafx.stage.Stage; | |
import javax.swing.SwingUtilities; | |
public class MouseInfoSample extends Application { | |
private Label lblXPos; | |
private Label lblYPos; | |
public static void main(String[] args) { | |
Application.launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
primaryStage.setTitle("Hello World!"); | |
Group root = new Group(); | |
Scene scene = new Scene(root, 300, 250); | |
final Button btn = new Button(); | |
btn.setLayoutX(100); | |
btn.setLayoutY(30); | |
btn.setText("マウス位置を取得"); | |
btn.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
final Point info = MouseInfo.getPointerInfo().getLocation(); | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
lblXPos.setText(Integer.toString(info.x)); | |
lblYPos.setText(Integer.toString(info.y)); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
final Label lblXTitle = LabelBuilder.create().text("x座標:").layoutX(20).layoutY(60).build(); | |
final Label lblYTitle = LabelBuilder.create().text("y座標:").layoutX(20).layoutY(90).build(); | |
lblXPos = LabelBuilder.create().layoutX(100).layoutY(60).build(); | |
lblYPos = LabelBuilder.create().layoutX(100).layoutY(90).build(); | |
root.getChildren().addAll(btn, lblXTitle, lblYTitle, lblXPos, lblYPos); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment