我的代码的问题是我希望更改分配给defaultImg的字符串,以便它将更改应用程序的背景图像。运行应用程序时,我没有收到运行时或编译错误。 “client.css”文件未更新。我搞砸了什么?有一些名为“img1”的按钮 - “img6”。它们位于我的代码底部附近。在这些方法中,我打开一个文件编写器来清除和重写css文档中的代码。提前谢谢你帮助我。
//settings Application:
S_windowPane = new GridPane();
S_windowPane.setPadding(new Insets(35));
S_windowPane.setHgap(0);
S_windowPane.setVgap(0);
S_windowPane.setId("SettingsPane");
S_buttonPane = new GridPane();
S_buttonPane.setPadding(new Insets(15));
S_buttonPane.setHgap(0);
S_buttonPane.setVgap(20);
S_buttonPane.setAlignment(Pos.BOTTOM_CENTER);
S_buttonPane.setId("buttonPane");
S_BkgrndImagePane = new GridPane();
S_BkgrndImagePane.setPrefWidth(500);
S_BkgrndImagePane.setPrefHeight(300);
S_BkgrndImagePane.setHgap(10);
S_BkgrndImagePane.setVgap(20);
S_BkgrndImagePane.setAlignment(Pos.CENTER);
S_BkgrndImagePane.setId("BackgroundImagePane");
Image img1_ = new Image(getClass().getResourceAsStream("images/backgroundImg1.jpg"));
img1 = new Button();
img1.setGraphic(new ImageView(img1_));
S_BkgrndImagePane.add(img1, 0, 0);
Image img2_ = new Image(getClass().getResourceAsStream("images/backgroundImg2.jpg"));
img2 = new Button();
img2.setGraphic(new ImageView(img2_));
S_BkgrndImagePane.add(img2, 1, 0);
Image img3_ = new Image(getClass().getResourceAsStream("images/backgroundImg3.jpg"));
img3 = new Button();
img3.setGraphic(new ImageView(img3_));
S_BkgrndImagePane.add(img3, 2, 0);
Image img4_ = new Image(getClass().getResourceAsStream("images/backgroundImg4.jpg"));
img4 = new Button();
img4.setGraphic(new ImageView(img4_));
S_BkgrndImagePane.add(img4, 0, 1);
Image img5_ = new Image(getClass().getResourceAsStream("images/backgroundImg5.jpg"));
img5 = new Button();
img5.setGraphic(new ImageView(img5_));
S_BkgrndImagePane.add(img5, 1, 1);
Image img6_ = new Image(getClass().getResourceAsStream("images/backgroundImg6.jpg"));
img6 = new Button();
img6.setGraphic(new ImageView(img6_));
S_BkgrndImagePane.add(img6, 2, 1);
cancel = new Button();
cancel.setText("cancel");
cancel.setPrefWidth(70);
cancel.setPrefHeight(35);
S_buttonPane.add(cancel, 0, 0);
cancel.setId("cancel");
apply = new Button();
apply.setText("apply");
apply.setPrefWidth(70);
apply.setPrefHeight(35);
S_buttonPane.add(apply, 0, 1);
apply.setId("apply");
S_windowPane.add(S_BkgrndImagePane, 0, 0);
S_windowPane.add(S_buttonPane, 1, 0);
S_root = new StackPane();
S_root.setId("S_root");
S_root.getChildren().add(S_windowPane);
settingsScene = new Scene(S_root, 670,420);
settingsScene.getStylesheets().add("client.css");
cancel.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
primaryStage.setScene(clientScene);
}
});
//used to reset and apply changes
apply.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
primaryStage.close();
primaryStage.setScene(settingsScene);
primaryStage.show();
primaryStage.close();
primaryStage.setScene(settingsScene);
primaryStage.show();
}
});
//leaves settings menu without making physical changes
cancel.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
primaryStage.setScene(clientScene);
}
});
img1.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
System.out.println(1);
try {
PrintWriter pw = null;
pw = new PrintWriter("src/client.css");
//long string containing code needed for .css
pw.write("#C_root{-fx-background-image: url('images/background.jpg');-fx-background-repeat: repeat;}#S_root{-fx-background-image: url('images/background.jpg');-fx-background-repeat: repeat;}#IPinput{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}.button{-fx-background-color: linear-gradient(#f2f2f2, #d6d6d6),linear-gradient(#fcfcfc 0%, #d9d9d9 20%, #d6d6d6 100%),linear-gradient(#dddddd 0%, #f6f6f6 50%);-fx-background-radius: 8,7,6;-fx-background-insets: 0,1,2;-fx-text-fill: black;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#textDisplay{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );-fx-font-fill: black;}#BackgroundImagePane{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#input{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}");
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
img2.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
System.out.println(2);
try {
PrintWriter pw = null;
pw = new PrintWriter("src/client.css");
//long string containing code needed for .css
pw.write("#C_root{-fx-background-image: url('images/BackGround2.jpg');-fx-background-repeat: repeat;}#S_root{-fx-background-image: url('images/BackGround2.jpg');-fx-background-repeat: repeat;}#IPinput{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}.button{-fx-background-color: linear-gradient(#f2f2f2, #d6d6d6),linear-gradient(#fcfcfc 0%, #d9d9d9 20%, #d6d6d6 100%),linear-gradient(#dddddd 0%, #f6f6f6 50%);-fx-background-radius: 8,7,6;-fx-background-insets: 0,1,2;-fx-text-fill: black;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#textDisplay{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );-fx-font-fill: black;}#BackgroundImagePane{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#input{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}");
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
img3.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
System.out.println(3);
try {
PrintWriter pw = null;
pw = new PrintWriter("src/client.css");
//long string containing code needed for .css
pw.write("#C_root{-fx-background-image: url('images/BackGround3.jpg');-fx-background-repeat: repeat;}#S_root{-fx-background-image: url('images/BackGround3.jpg');-fx-background-repeat: repeat;}#IPinput{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}.button{-fx-background-color: linear-gradient(#f2f2f2, #d6d6d6),linear-gradient(#fcfcfc 0%, #d9d9d9 20%, #d6d6d6 100%),linear-gradient(#dddddd 0%, #f6f6f6 50%);-fx-background-radius: 8,7,6;-fx-background-insets: 0,1,2;-fx-text-fill: black;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#textDisplay{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );-fx-font-fill: black;}#BackgroundImagePane{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#input{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}");
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
img4.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
System.out.println(4);
try {
PrintWriter pw = null;
//long string containing code needed for .css
pw = new PrintWriter("src/client.css");
pw.write("#C_root{-fx-background-image: url('images/BackGround4.jpg');-fx-background-repeat: repeat;}#S_root{-fx-background-image: url('images/BackGround4.jpg');-fx-background-repeat: repeat;}#IPinput{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}.button{-fx-background-color: linear-gradient(#f2f2f2, #d6d6d6),linear-gradient(#fcfcfc 0%, #d9d9d9 20%, #d6d6d6 100%),linear-gradient(#dddddd 0%, #f6f6f6 50%);-fx-background-radius: 8,7,6;-fx-background-insets: 0,1,2;-fx-text-fill: black;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#textDisplay{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );-fx-font-fill: black;}#BackgroundImagePane{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#input{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}");
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
img5.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
System.out.println(5);
try {
PrintWriter pw = null;
pw = new PrintWriter("src/client.css");
//long string containing code needed for .css
pw.write("#C_root{-fx-background-image: url('images/BackGround5.jpg');-fx-background-repeat: repeat;}#S_root{-fx-background-image: url('images/BackGround5.jpg');-fx-background-repeat: repeat;}#IPinput{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}.button{-fx-background-color: linear-gradient(#f2f2f2, #d6d6d6),linear-gradient(#fcfcfc 0%, #d9d9d9 20%, #d6d6d6 100%),linear-gradient(#dddddd 0%, #f6f6f6 50%);-fx-background-radius: 8,7,6;-fx-background-insets: 0,1,2;-fx-text-fill: black;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#textDisplay{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );-fx-font-fill: black;}#BackgroundImagePane{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#input{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}");
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
img6.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
System.out.println(6);
try {
//long string containing code needed for .css
PrintWriter pw = null;
pw = new PrintWriter("src/client.css");
pw.write("#C_root{-fx-background-image: url('images/BackGround6.jpg');-fx-background-repeat: repeat;}#S_root{-fx-background-image: url('images/BackGround6.jpg');-fx-background-repeat: repeat;}#IPinput{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}.button{-fx-background-color: linear-gradient(#f2f2f2, #d6d6d6),linear-gradient(#fcfcfc 0%, #d9d9d9 20%, #d6d6d6 100%),linear-gradient(#dddddd 0%, #f6f6f6 50%);-fx-background-radius: 8,7,6;-fx-background-insets: 0,1,2;-fx-text-fill: black;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#textDisplay{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );-fx-font-fill: black;}#BackgroundImagePane{-fx-opacity: 1.0;-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}#input{-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );}");
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});