2015-11-08 37 views
0
public void frame(Rectangle rechteck) { 
     rechteck.setOnMouseEntered(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent e) { 
       rechteck.setStroke(Color.DARKGREY); 
      } 
     }); 
     rechteck.setOnMouseExited((MouseEvent e) -> { 
      rechteck.setStroke(Color.BLUE); 
     }); 
    } 

我试图让'悬停'效果。我有不同的矩形,当我进入矩形时,它会得到一个灰色的笔画。当光标离开矩形时,笔画应该再次消失。setOnMouseExited不适用于笔划?

  1. 为什么我的笔画总是灰色,并且在我离开矩形时不会改变。

  2. 如何禁用笔划?如果我退出矩形,我实际上不想将颜色更改为蓝色,但我希望它不再存在。

编辑:休息

@Override 
    public void start(Stage primaryStage) { 

     StackPane root = new StackPane(); 

     // Die Linie in der Mitte. 
     Rectangle line = new Rectangle(0, 0, 450, 1); 
     root.getChildren().add(line); 

     // Das abgerundete Rechteck. 
     Rectangle roundedRect = new Rectangle(0, 0, 300, 80); 
     roundedRect.setStroke(Color.BLACK); 
     roundedRect.setArcWidth(40); 
     roundedRect.setArcHeight(40); 
     roundedRect.setFill(Color.BISQUE); 
     root.getChildren().add(roundedRect); 

     // Die Farblinien. 
     Rectangle firstLine = new Rectangle(100, 0, 25, 80); 
     firstLine.setFill(Color.BROWN); 
     frame(firstLine); 
     Rectangle secondLine = new Rectangle(0, 0, 25, 80); 
     secondLine.setFill(Color.BLACK); 
     frame(secondLine); 
     Rectangle thirdLine = new Rectangle(0, 0, 25, 80); 
     thirdLine.setFill(Color.RED); 
     frame(thirdLine); 
     Rectangle fourthLine = new Rectangle(0, 0, 25, 80); 
     fourthLine.setFill(Color.GOLD); 
     frame(fourthLine); 
     HBox lines = new HBox(); 
     lines.setAlignment(Pos.CENTER); 
     lines.setPadding(new Insets(30)); 
     HBox.setMargin(firstLine, new Insets(0, 30, 0, 0)); 
     HBox.setMargin(secondLine, new Insets(0, 30, 0, 0)); 
     HBox.setMargin(thirdLine, new Insets(0, 30, 0, 0)); 
     HBox.setMargin(fourthLine, new Insets(0, 0, 0, 50)); 
     lines.getChildren().addAll(firstLine, secondLine, thirdLine, fourthLine); 

     double widerstand = (ERSTERRING + ZWEITERRING) * DRITTERRING; 

     char groesse = ' '; 
     if (widerstand >= 1000 && widerstand < 10000) { 
      widerstand = widerstand/1000; 
      groesse = 'k'; 
     } else if (widerstand >= 1000000) { 
      widerstand = widerstand/1000000; 
      groesse = 'M'; 
     } 

     VBox vbox = new VBox(); 
     Text werte = new Text(90, 210, "R = " + widerstand + groesse + "Ω mit " + VIERTERRING + " % Toleranz"); 
     VBox.setMargin(werte, new Insets(200, 0, 0, 70)); 
     vbox.getChildren().add(werte); 
     root.getChildren().add(vbox); 
     root.getChildren().add(lines); 

     Scene scene = new Scene(root, 450, 300); 
     primaryStage.setTitle("Widerstandsrechner"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

回答

1

为什么我的行程总是灰色,并没有改变,当我离开的矩形。

很难说没有你的其他代码。

如何禁用笔划?

将笔画设置为nullColor.TRANSPARENT


这里是一个似乎能够实现你想要的样本。第一张图片没有鼠标悬停在(最初不可见的)矩形上。第二张图片是鼠标悬停在矩形上。

hoveredunhovered

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 

public class RectHover extends Application { 
    @Override public void start(Stage stage) { 
     Rectangle rectangle = new Rectangle(
       100, 100, Color.TRANSPARENT 
     ); 

     rectangle.strokeProperty().bind(
       Bindings.when(rectangle.hoverProperty()) 
         .then(Color.DARKGREY) 
         .otherwise((Color) null) 
     ); 

     StackPane layout = new StackPane(rectangle); 
     layout.setPadding(new Insets(10)); 
     stage.setScene(new Scene(layout)); 
     stage.show(); 
    } 

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