简体   繁体   中英

JavaFX - Why do buttons sometimes shrink?

In javaFX, why do my button sometimes randomly shrink when I hover over them?

在此处输入图片说明

Why is this happening and how do I stop it.

Here is my code for the button depicted, but I don't know if you need it. It seems to happen to other buttons also and it dosn't always happen to the same button.

btnNext = new Button(">");
    btnNext.setOnAction(e -> {
        switch (view) {
            case MONTH:
                setDisplay(target.plusMonths(1));
                break;
            case WEEK:
                setDisplay(target.plusWeeks(1));
                break;
        }
    });

This only happens AFTER I click it and then leave. When the program first starts, it is the normal size.

When a control shrinks, it's usually a good sign that something else is stealing the space away from it. Under normal conditions, the Scene would ask all the descendant nodes how much space it needs to render itself - this is what prefWidth and prefHeight do.

When there is a space constraint, most Pane subclasses (recall that Pane subclasses manages their children based on certain rules) will try to reduce the size of one or more children. If you do not want a particular Node to shrink below what is its calculated size, then you must specify a minWidth and/or minHeight , or set them to USE_PREF_SIZE . Setting either or both of them to USE_PREF_SIZE tells the parent that this node must not shrink.

However, if you specify USE_PREF_SIZE for all nodes, then the parent will be in trouble - it has been told to shrink but it couldn't shrink anything. I do not know exactly what will happen, but most likely, the whole UI going to look weird.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM