How to create a partial border(with a hole)?

前端 未结 3 1922
挽巷
挽巷 2021-01-22 06:36

I wrote code in JavaFX and used CSS for setting styles for the above issue. The following code made borders for an AnchorPane that are 2 pixels wide:

    AnchorP         


        
3条回答
  •  -上瘾入骨i
    2021-01-22 06:58

    Sir, Do you strictly require CSS for this? java can do it, using Shape and Borders

    Nikitoslav.java

    public class Nikitoslav extends Application {
    
    @Override
    public void start(Stage primaryStage) throws Exception {
    
        Pane g = new Pane();        
        g.setBackground(new Background(new BackgroundFill(Color.YELLOW,
                null, null)));
        g.relocate(20, 20);
        BorderStroke bs = new BorderStroke(Color.RED, Color.RED, Color.RED, Color.RED,//the paint around th four corners
                BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID,
                null, BorderStroke.THIN, null);
        g.setBorder(new Border(bs));
        Path p = new  Path();
        p.getElements().addAll(new MoveTo(1, 0),new HLineTo(0),new VLineTo(4), new HLineTo(4), new VLineTo(0),
                //creating a rectangle
                new HLineTo(3)
        );
        g.setShape(p);
        g.setPrefSize(300, 300);
        primaryStage.setScene(new Scene(new Group(g),500,500));
        primaryStage.show();
        g.setPrefSize(400, 400);
    }
    
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void init() throws Exception {
        // TODO Auto-generated method stub
        super.init();
    }
    
    @Override
    public void stop() throws Exception {
        // TODO Auto-generated method stub
        super.stop();
     }
    
    }
    

    With this you don't need nested Backgrounds or an array of Backgrounds-(guess that will improve performance)

    To explain my code, well, i created my Borders using the BorderStroke which Paint fills for top-right-bottom-left respectively, specified the type of Color and style, after i created an open rectangle with the PathElements. From the x axis i started from 1 then to 0 the drew a vertical line to 4, the length of the rectangle and drew the bottom width also 4and drew from a vertical line from length 4 to 0 then drew from width 4 to 3 and left points 2 being the hole

    Hope it helps

提交回复
热议问题