JavaFX GUI编程:从基础控件到事件处理
1. 简单控件:标签和图像
在JavaFX应用中,我们可以向窗口添加简单的控件,如标签(Label)和图像(ImageView)。以下是示例代码,展示了如何创建一个包含标签和图像的窗口。
Dinner.java
/* Displaying a Label and image
Anderson, Franceschi
*/
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Dinner extends Application
{
@Override
public void start( Stage stage )
{
try
{
// find the XML resource
URL url
= getClass( ).getResource(
"fxml_dinner.fxml" );
// load the XML resource and instantiate
// the root node