我正在开发基于 Gluon/JavaFx 的应用程序,我希望在其中允许用户在明暗模式之间切换。但是,如您所知,不仅 Gluon mobile dev 中的每个类都没有 args 参数,而且在明暗之间切换的方法还需要您从 main 类中传递 Scene 场景。这使我的明暗模式切换想法成为一个棘手的编码。
有人可以帮我吗?我是 Gluon 的新手,渴望学习 :)
编辑:我已经看过 javadoc 并且知道如何在 postInit
方法中自己更改它,我要问的是如何允许用户在应用程序中更改它类似于 CheckBox。
Edit1:使用@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");
}
}
答案 0 :(得分:0)
感谢@JoséPereda,这个问题已经得到解决。我的情况的正确代码是 node.getScene()
,其中节点可以是任何节点,例如窗格、按钮等。具体来说,我使用了 DARK.assignTo(button.getScene());
。