【问题标题】:java - images in javafxjava - javafx中的图像
【发布时间】:2016-05-07 01:56:29
【问题描述】:

我对 java 还是很陌生,我还在学习。我以前从未使用过图像,所以如果我可以帮助添加图像是可能的。我不确定添加一个需要做什么。谢谢 enter image description here

代码如下:

import javafx.application.Application;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

class User {
    private StringProperty order = new SimpleStringProperty();

    public String getOrder() {
        return order.get();
    }

    public void setOrder(String order) {
        this.order.set(order);
    }

    public StringProperty orderProperty() {
        return order;
    }
}

public class pizza extends Application {

    private User user = new User();

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Pizza System");
        Button btn = new Button();
        btn.setText("place order");
        BorderPane pane = new BorderPane();
        pane.setBottom(btn);
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
             public void handle(ActionEvent event) {
                btn.setText("Order has been placed. Please wait at least 30 minutes.");
                }
        });





        RadioButton tomatoButton = new RadioButton("Tomato");
        RadioButton pepperButton = new RadioButton("Pepper");
        RadioButton mushroomButton = new RadioButton("Mushrooms");

        ChoiceBox<String> pizzaType = new ChoiceBox<String>();
        pizzaType.getItems().addAll("", "Small", "Medium", "Large");
        pizzaType.getSelectionModel().selectFirst();

       HBox topHBox = new HBox(15.0, tomatoButton, pepperButton, mushroomButton, pizzaType);

        // create custom Binding that binds selection of radio buttons and choice box
        StringBinding orderBinding = createOrderBinding(tomatoButton.selectedProperty(), pepperButton.selectedProperty(), mushroomButton.selectedProperty(), pizzaType.getSelectionModel().selectedItemProperty());
        // bind orderBinding to orderProperty of User
        user.orderProperty().bind(orderBinding);

        TextArea orderArea = new TextArea();
        // bind orderProperty of User to textProperty of TextArea
        orderArea.textProperty().bindBidirectional(user.orderProperty());

       BorderPane root = new BorderPane();
        root.setTop(topHBox);
        root.setCenter(orderArea);
        root.setBottom(btn);

        Scene scene = new Scene(root, 400, 300);
        stage.setScene(scene);
        stage.show();

    }

    public StringBinding createOrderBinding(BooleanProperty tomato, BooleanProperty pepper, BooleanProperty mushroom, ReadOnlyObjectProperty<String> selectedPizzaType) {
        StringBinding binding = new StringBinding() {
            {
                // bind 4 provided properties.
                super.bind(tomato, pepper, mushroom, selectedPizzaType);
            }

            @Override
            protected String computeValue() {
                StringBuilder sb = new StringBuilder("Pizza content:\n");

                if (tomato.get())
                    sb.append("\tTomato\n");
                if (pepper.get())
                    sb.append("\tPepper\n");
                if (mushroom.get())
                    sb.append("\tMushroom\n");

                sb.append("Pizza type:\n").append("\t" + selectedPizzaType.get());
                return sb.toString();
            }
        };
        return binding;
    }

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

【问题讨论】:

  • 你想把图片放在哪里?我没有看到您的代码中定义了 Image
  • 我要放在右边的中间

标签: java image javafx


【解决方案1】:

JavaFX 使用Image 加载图像文件,它有一个名为ImageView 的节点来将该图像放置在屏幕图形上。

考虑到图像与您的类文件位于同一位置,您可以使用这个:

// Load Image
Image image = new Image(getClass().getResource("image.jpg").toExternalForm());
// Set the Image on the ImageView
ImageView imageView = new ImageView(image);
// specify a size
imageView.setFitWidth(200);
imageView.setFitHeight(200);
// Place ImageView in a container
root.setRight(imageView);

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2016-06-26
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多