【问题标题】:Background and button issue背景和按钮问题
【发布时间】:2018-09-21 09:21:44
【问题描述】:

运行此代码时遇到问题:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundPosition;
import javafx.event.ActionEvent;

import java.io.FileInputStream;

public class Main extends Application  {

@Override
public void start(Stage primaryStage) throws Exception {
    BackgroundImage backgroundImage = new BackgroundImage( new Image( getClass().getResource("src/sample/Image/backg.jpg").toExternalForm()), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
    Background background = new Background(backgroundImage);

    Button button = new Button( "Click me!");
    button.setBackground(background);

    Scene scene = new Scene(backgroundImage, 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}



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

我正在尝试在背景上显示按钮,但尽管如此,我还是面临很多问题。我不太了解“场景”类的工作原理,所以我觉得我在那里犯了错误。 我收到此错误消息:

Error:(38, 33) java: incompatible types: javafx.scene.layout.BackgroundImage cannot be converted to javafx.scene.Parent

38 和 33 分别是:

Background background = new Background(backgroundImage);
Scene scene = new Scene(backgroundImage, 600, 400);

谢谢

更新

在马特回复后,我尝试了他的代码并收到以下错误消息:

    Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
at sample.Main.start(Main.java:34)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Exception running application sample.Main

Process finished with exit code 1

【问题讨论】:

标签: java javafx


【解决方案1】:

在 javafx 中尝试一下,您通常希望将所有节点保存在一个容器中。查看我添加的 cmets

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {//Removed throws Exception
        BackgroundImage backgroundImage = new BackgroundImage( 
                new Image( getClass().getResource("src/sample/Image/backg.jpg").toExternalForm()),
                BackgroundRepeat.NO_REPEAT, 
                BackgroundRepeat.NO_REPEAT, 
                BackgroundPosition.DEFAULT, 
                BackgroundSize.DEFAULT);
        Background background = new Background(backgroundImage);

        Button button = new Button( "Click me!");
        //button.setBackground(background);Not needed

        VBox vBox = new VBox();//A container which holds all the nodes
        vBox.setBackground(background);//Set the Container Background
        vBox.getChildren().add(button);//Add Nodes

        Scene scene = new Scene(vBox, 600, 400);//Parameters are Parent and width and height of your scene

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

【讨论】:

  • 试过了,但没有运气。更新了带有错误消息的主题。
  • 我认为错误在您的文件路径中,因为它在我的运行没有错误是您的资源文件夹中的文件?
  • 查看此链接stackoverflow.com/questions/29780369/… 它谈到了在没有资源文件夹的情况下加载图像
  • 该文件位于代码所示的路径中,所以是的,我重新检查了路径,一切看起来都很合法。
  • 如果我的文件路径错误,我可以在我的机器上正常运行我得到同样的错误检查上面的链接
【解决方案2】:

希望下面的解释对你有所帮助。

backgroundImage 不是父级,但按钮是,即button.setBackground(background);

因此要显示带有背景的按钮,您需要在场景构造函数中将按钮作为参数传递:Scene scene = new Scene(button, 600, 400);。 如果您想在背景图像上显示按钮,则需要在某个窗格上设置按钮并将您拥有的图像放在窗格的背景上,以便有效地实现您的目标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多