以下是我尝试过的方法。我尝试使用 imageView,将图像设置到它并将其添加到堆栈 Pane 。图像显得相当大,看起来不像我想要的主菜单的样子(当我最大化屏幕时背景颜色就在那里,但 logoIcon 最初占据了整个屏幕)。然后我决定摆脱它,使用样式类并将其添加到我的堆栈 Pane 中,但它似乎没有注册我添加到堆栈 Pane 中的样式类。由于这是一个简单的示例,您可以试用该程序并查看结果。
这里是相关代码
package whowantstobeamillionairetriviagame;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class WhoWantsToBeAMillionaireTriviaGame extends Application
{
@Override
public void start(Stage startingStage) throws Exception
{
Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg");
BackgroundSize backgroundSize = new BackgroundSize(100, 100, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
StackPane background = new StackPane();
background.setBackground(new Background(backgroundImage));
background.getStyleClass().add("MillionaireLogo");
Scene backgroundScene = new Scene(background);
startingStage.setScene(backgroundScene);
startingStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
MillionaireLogo.css 文件
.MillionaireLogo
{
-fx-background-image: url("http://3.bp.blogspot.com/-ujgaiwveDzc/TZQeK9gZvPI/AAAAAAAAADY/ZNVAxlaqXoY/s1600/Millionaire%2BParody%2BLogo2%2Bcopy.jpg");
-fx-background-repeat: no-repeat;
-fx-background-position: top center;
-fx-background-size: cover;
}
最佳答案
好吧,这是解决我的主要问题(将图像堆叠到背景)的解决方案
package whowantstobeamillionairetriviagame;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class WhoWantsToBeAMillionaireTriviaGame extends Application
{
@Override
public void start(Stage startingStage) throws Exception
{
StackPane backgroundSettings = new StackPane();
Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg");
ImageView background = new ImageView();
background.setImage(backgroundColor);
Image millionaireLogo = new Image(new File("MillionaireLogo.PNG").toURI().toString());
ImageView logoPicture = new ImageView();
logoPicture.setImage(millionaireLogo);
logoPicture.setPreserveRatio(true);
logoPicture.setSmooth(true);
logoPicture.setCache(true);
StackPane.setAlignment(logoPicture, Pos.TOP_CENTER);
backgroundSettings.getChildren().addAll(background, logoPicture);
Scene backgroundScene = new Scene(backgroundSettings);
startingStage.setScene(backgroundScene);
startingStage.setTitle("Who Wants to be a Millionaire");
startingStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
我现在的新问题是如何缩小图像的大小(给定我的代码)并将其放置在屏幕的顶部中央?
关于java - 如何将图像堆叠到 javafx 中的现有背景上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29459369/