2015-04-05 38 views
0

以下是我嘗試過的方法。我試圖使用imageView,將圖像設置爲它並將其添加到堆疊面板。圖像顯得相當大,看起來並不像主菜單的樣子(當我最大化屏幕時背景顏色存在,但logoIcon最初佔據了整個屏幕)。然後我決定擺脫這一點,使用styleclass並將其添加到我的stackpane中,但它似乎沒有註冊我添加到堆棧窗格中的styleclass。由於這是一個簡單的例子,你可以試試這個程序,看看你得到了什麼。如何將圖像堆疊到javafx中的現有背景上

下面是相關代碼

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; 
} 

回答

0

嗯,這是解決我的主要問題的解決方案(堆疊的圖像背景)

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); 
} 
} 

我現在的新問題是如何縮小圖像的大小(給出我有的代碼)和plac它在屏幕的頂部中心?

+0

哦,我擺脫了CSS文件 – 2015-04-06 01:09:18

+0

另外,我怎樣才能去除圖像周圍的空白? – 2015-04-06 01:12:24

+0

好吧我能夠使用這行代碼StackPane.setAlignment(logoPicture,Pos.TOP_CENTER);現在我需要做的就是將圖像調整到合適的尺寸並擺脫空白。 – 2015-04-06 04:33:21