[英]How to change app between dark mode and light mode depending on the time of day?
[英]Allowing user to switch between light and dark mode Gluon
我正在開發一個基於 Gluon/JavaFx 的應用程序,我希望允許用戶在明暗模式之間切換。 但是,如您所知,不僅 Gluon mobile dev 中的每個類都沒有 args 參數,而且在明暗之間切換的方法也需要您從主類中傳遞 Scene 場景。 這使得我的明/暗模式切換想法成為一個棘手的編碼。
有人可以幫我嗎? 我是 Gluon 的新手並且渴望學習:)
編輯:我已經看過javadoc並且知道如何在postInit
方法中自己更改它,我要問的是如何允許用戶在應用程序中使用CheckBox 之類的內容更改它。
編輯 1:使用 @JoséPereda 提供的Node::getScene
(謝謝!),我的代碼(如下)生成以下錯誤:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: Uncompilable source code - Erroneous ctor sym type: javafx.scene.Scene.<init>
at com.mysecondapplication.views.QuaternaryView.<init>(QuaternaryView.java:63)
at com.gluonhq.impl.charm.glisten.util.CachedFactory.get(CachedFactory.java:32)
at com.gluonhq.charm.glisten.application.MobileApplication.doSwitchView(MobileApplication.java:723)
at com.gluonhq.charm.glisten.application.MobileApplication.switchView(MobileApplication.java:477)
at com.gluonhq.charm.glisten.control.NavigationDrawer$ViewItem.lambda$new$0(NavigationDrawer.java:858)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
at com.gluonhq.charm.glisten.control.NavigationDrawer$Item.setSelected(NavigationDrawer.java:745)
at com.gluonhq.charm.glisten.control.NavigationDrawer$1.lambda$new$0(NavigationDrawer.java:312)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base/javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:100)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at com.gluonhq.charm.glisten.control.NavigationDrawer.setSelectedItem(NavigationDrawer.java:318)
at com.gluonhq.impl.charm.glisten.control.skin.NavigationDrawerSkin.lambda$new$0(NavigationDrawerSkin.java:46)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3563)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3865)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
這是我的代碼:
package com.mysecondapplication.views;
import com.gluonhq.charm.glisten.animation.BounceInRightTransition;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.control.BottomNavigation;
import com.gluonhq.charm.glisten.control.FloatingActionButton;
import com.gluonhq.charm.glisten.mvc.View;
import com.mysecondapplication.views.Session;
import com.mysecondapplication.views.Sessions;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.gluonhq.charm.glisten.visual.Theme.DARK;
import static com.gluonhq.charm.glisten.visual.Theme.LIGHT;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.control.Slider;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class QuaternaryView extends View {
boolean light = true;
Scene scene;
public QuaternaryView() {
getStylesheets().add(QuaternaryView.class.getResource("quaternary.css").toExternalForm());
Font font = new Font("Comfortaa", 15);
Font font1 = new Font("Comfortaa", 24);
// Use a GridPane to create a login interface insights
VBox grid = new VBox();
grid.setAlignment(Pos.CENTER);
grid.setPadding(new Insets(25, 25, 25, 25));
//grid.setStyle("-fx-background-color: black;");
Label scenetitle = new Label("Settings");
scenetitle.setFont(font1);
//scenetitle.setAlignment(Pos.TOP_LEFT);
grid.getChildren().add(scenetitle);
//scenetitle.setStyle("-fx-text-fill: grey;");
Label h2 = new Label("This code\nis a\nplaceholder.");
h2.setFont(font);
//scenetitle.setStyle("-fx-text-fill: grey;");
Pane mode = mode(Node::getScene);
grid.getChildren().addAll(h2, mode);
setCenter(grid);
}
// flag boolean based on current setting???
public Pane mode(Scene scene) {
CheckBox button;
if(light = true) {
button = new CheckBox("Dark Mode");
button.setOnMousePressed( e -> {
light = false;
DARK.assignTo(scene);
});
} else {
button = new CheckBox("Light Mode");
button.setOnMousePressed( e -> {
light = true;
LIGHT.assignTo(scene);
});
}
Pane but = new Pane(button);
return but;
}
@Override
protected void updateAppBar(AppBar appBar) {
appBar.setNavIcon(MaterialDesignIcon.DRAG_HANDLE.button(e -> `getApplication().getDrawer().open()));`
appBar.setTitleText("Settings/Appearance");
}
}
感謝@JoséPereda,這個問題已經得到解決。 對於我的情況,正確的代碼是node.getScene()
,其中 node 可以是任何節點,例如 Pane、Button 等。具體來說,我使用了DARK.assignTo(button.getScene());
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.