我正在尝试使用netbeans进行JavaFX,我试图在弹出按钮时弹出一个弹出窗口,但是弹出窗口有白色背景,我怎么能透明呢?
感谢。
以下是代码:
主要
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.image.Image;
public class Test extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.initStyle(StageStyle.TRANSPARENT);
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root, Color.TRANSPARENT);
stage.setScene(scene);
stage.show();
stage.setTitle("integrIX");
stage.getIcons().add(new Image("test/integrix_icon.png"));
}
public static void main(String[] args) {
launch(args);
}
}
FXMLController:
package test;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class FXMLDocumentController implements Initializable {
@FXML
private Button menu1;
@FXML
private Button menu2;
@FXML
private Button menu3;
@FXML
private Button menu4;
@FXML
private Button closeButton;
@FXML
private Button minimizeButton;
@FXML
private Pane paneMain;
private double xOffset;
private double yOffset;
private Stage popup;
@Override
public void initialize(URL url, ResourceBundle rb) {
Platform.runLater(new Runnable(){
private StageStyle Color;
@Override
public void run(){
Stage stage = Stage.class.cast(paneMain.getScene().getWindow());
makeDraggable(stage, paneMain);
Parent popupLoaded = null;
try {
popupLoaded = FXMLLoader.load(getClass().getResource("popup.fxml"));
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
Scene popupScene = new Scene(popupLoaded);
popup = new Stage();
popup.initStyle(StageStyle.TRANSPARENT);
popup.initModality(Modality.WINDOW_MODAL);
popup.initOwner(stage);
popup.setScene(popupScene);
}
});
}
@FXML
public void diagnosticButton(){
assert popup != null;
popup.show();
}
@FXML
public void handleClick(MouseEvent event) {
//System.err.println("BOUND OFFSET");
//xOffset = event.getSceneX();
//yOffset = event.getSceneY();
//xOffset = event.getSceneX();
//yOffset = event.getSceneY();
}
@FXML
public void handleDrag(MouseEvent event) {
//System.err.println("DRAGGING");
Stage stage = Stage.class.cast(paneMain.getScene().getWindow());
stage.setX(event.getScreenX() - xOffset);
stage.setY(event.getScreenY() - yOffset);
}
public void makeDraggable( final Stage stage, final Node byNode) {
byNode.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
xOffset = stage.getX() - mouseEvent.getScreenX();
yOffset = stage.getY() - mouseEvent.getScreenY();
byNode.setCursor(Cursor.MOVE);
}
});
byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
byNode.setCursor(Cursor.DEFAULT);
}
});
byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
stage.setX(mouseEvent.getScreenX() + xOffset);
stage.setY(mouseEvent.getScreenY() + yOffset);
} });
byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.DEFAULT);
} } });
byNode.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.DEFAULT);
} } });
}
@FXML
public void changeButtonOnOver(Event e){
menu1.getStyleClass().add("boton_over");
}
@FXML
public void changeButtonOnExited(){
menu1.getStyleClass().remove("boton_over");
}
@FXML
public void changeButtonOnOver2(Event e){
menu2.getStyleClass().add("boton_over");
}
@FXML
public void changeButtonOnExited2(){
menu2.getStyleClass().remove("boton_over");
}
@FXML
public void changeButtonOnOver3(Event e){
menu3.getStyleClass().add("boton_over");
}
@FXML
public void changeButtonOnExited3(){
menu3.getStyleClass().remove("boton_over");
}
@FXML
public void changeButtonOnOver4(Event e){
menu4.getStyleClass().add("boton_over");
}
@FXML
public void changeButtonOnExited4(){
menu4.getStyleClass().remove("boton_over");
}
@FXML
public void changeButtonOnOverClose(){
closeButton.getStyleClass().add("btn_cerrar_over");
}
@FXML
public void changeButtonOnExitedClose(){
closeButton.getStyleClass().remove("btn_cerrar_over");
}
@FXML
public void changeButtonOnOverMinimize(){
minimizeButton.getStyleClass().add("btn_cerrar_over");
}
@FXML
public void changeButtonOnExitedMinimize(){
minimizeButton.getStyleClass().remove("btn_cerrar_over");
}
@FXML
public void exit(){
Stage stage = Stage.class.cast(closeButton.getScene().getWindow());
stage.close();
}
@FXML
public void minimize(){
Stage stage = Stage.class.cast(minimizeButton.getScene().getWindow());
stage.setIconified(true);
}
}
弹出控制器:
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class PopupController implements Initializable {
@FXML
private Pane rect;
private double xOffset;
private double yOffset;
@Override
public void initialize(URL url, ResourceBundle rb) {
Platform.runLater(new Runnable(){
private Parent root;
@Override
public void run(){
Stage stage = Stage.class.cast(rect.getScene().getWindow());
makeDraggable(stage, rect);
}});
}
@FXML
public void initialize(){
System.err.println("init popup called");
assert rect != null;
Stage stage = Stage.class.cast(rect.getScene().getWindow());
makeDraggable(stage, rect);
}
public void makeDraggable( final Stage stage, final Node byNode) {
byNode.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
xOffset = stage.getX() - mouseEvent.getScreenX();
yOffset = stage.getY() - mouseEvent.getScreenY();
byNode.setCursor(Cursor.MOVE);
}
});
byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
byNode.setCursor(Cursor.DEFAULT);
}
});
byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
stage.setX(mouseEvent.getScreenX() + xOffset);
stage.setY(mouseEvent.getScreenY() + yOffset);
} });
byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.DEFAULT);
} } });
byNode.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.DEFAULT);
} } });
}
@FXML
private Button closeButton;
@FXML
public void changeButtonOnOverClose(){
closeButton.getStyleClass().add("btn_cerrar_over");
}
@FXML
public void changeButtonOnExitedClose(){
closeButton.getStyleClass().remove("btn_cerrar_over");
}
@FXML
public void exit(){
Stage stage = Stage.class.cast(closeButton.getScene().getWindow());
stage.close();
}
}
答案 0 :(得分:0)
在弹出控制器中初始化尝试:
rect.setStyle("-fx-background-color: transparent;");