【发布时间】:2020-10-12 23:50:56
【问题描述】:
我正在尝试设置它,以便命令行输出转到我的 GUI 的 Javafx TextArea,使用以下代码将命令行输出附加到 TextArea:
import javafx.scene.control.TextArea;
import javax.swing.*;
import java.io.OutputStream;
public class CustomOutputStream extends OutputStream {
private static TextArea textArea = new TextArea();
public CustomOutputStream(TextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b){
textArea.appendText(String.valueOf((char)b));
}
}
和
PrintStream printStream = new PrintStream(new CustomOutputStream(textArea));
System.setOut(printStream);
但这似乎会导致 GUI 停止响应,并给出以下越界错误:
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:373)
at java.base/java.util.ArrayList.get(ArrayList.java:426)
at javafx.base/com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.get(VetoableListDecorator.java:306)
at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1704)
at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1648)
at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501)
at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBoundsImpl(RegionHelper.java:78)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBounds(RegionHelper.java:62)
at javafx.graphics/javafx.scene.layout.Region.doComputeGeomBounds(Region.java:3289)
at javafx.graphics/javafx.scene.layout.Region$1.doComputeGeomBounds(Region.java:168)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.computeGeomBoundsImpl(RegionHelper.java:89)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:115)
at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3843)
at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3805)
at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3753)
at javafx.graphics/javafx.scene.Node.intersectsBounds(Node.java:5288)
at javafx.graphics/javafx.scene.Node$1.intersectsBounds(Node.java:549)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.intersectsBounds(NodeHelper.java:262)
at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Region.java:3158)
at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Region.java:184)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(RegionHelper.java:104)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(NodeHelper.java:128)
at javafx.graphics/javafx.scene.Node.pickNode(Node.java:5198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.pickNode(Scene.java:4033)
at javafx.graphics/javafx.scene.Scene.pick(Scene.java:2065)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3843)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1885)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
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(AccessController.java:391)
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:832)
【问题讨论】:
-
这个
@Override public void write(int b){ textArea.appendText(String.valueOf((char)b)); }看起来不正确。我认为int b应该是某种KeyEvent。
标签: java user-interface javafx textarea outputstream