我有一个类扩展了Application:
public class IO extends Application {
private static JFrame frame = new JFrame("Shapes");;
private final JPanel content = new JPanel();
private final JPanel drawingArea = new JPanel();
my main/start method:
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage primaryStage) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFXPanel(); // initializes JavaFX environment
createGUI();
}
});
}
我这样做是希望解决swing/JavaFX组件之间的任何问题。然而,当我点击一个按钮,添加一个JPanel或JFXPanel到另一个JPanel,如果它是JFXPanel,它会执行一次很好,但它会失败以后任何时候,我试图再次生成一个JFXPanel,给出一个异常在线程AWT-EventQueue-0"java.lang.NullPointerException"(但实际上总共需要3次才能出现这个错误-第二次它将冻结,如果没有,它什么也不做)。以下是该按钮的相关代码:
JButton go = new JButton("Go");
go.addActionListener(new ActionListener() {
// irrelevant code
System.out.println("Shape set parameters: " + selectedShape.setParameters(shapeParams));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFXPanel(); // initializes JavaFX environment
Component shapePanel = (Component) new PaintedShape(selectedShape).getPanel();
drawingArea.add(shapePanel);
// drawingArea.add(new JLabel("Test"));
// drawingArea.repaint();
shapePanel.repaint();
shapePanel.validate();
drawingArea.repaint();
drawingArea.validate();
frame.pack();
frame.setSize(new Dimension((int) (frame.getSize().getWidth() + PaintedShape.size.getWidth()),
(int) (frame.getSize().getHeight() + PaintedShape.size.getHeight())));
frame.setLocationRelativeTo(null);
}
});
}
});
params.add(go);
content.validate();
我多次调用validate()
,因为它有时无法添加形状,其中一组调用最初修复了该错误。如果你能告诉我哪个小组更有意义,那就加分了。如果你觉得你需要剩下的代码,我在这里创建了一个github仓库。
编辑:完整的堆栈跟踪-
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.sun.javafx.tk.quantum.QuantumToolkit.isSupported(QuantumToolkit.java:1153)
at com.sun.javafx.application.PlatformImpl.isSupportedImpl(PlatformImpl.java:809)
at com.sun.javafx.application.PlatformImpl.isSupported(PlatformImpl.java:482)
at javafx.application.Platform.isSupported(Platform.java:168)
at javafx.scene.shape.Shape3D.<init>(Shape3D.java:74)
at javafx.scene.shape.Cylinder.<init>(Cylinder.java:89)
at shapes.PaintedShape.painted3DShapePanel(PaintedShape.java:96)
at shapes.PaintedShape.<init>(PaintedShape.java:51)
at shapes.IO$1$1$1.run(IO.java:215)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
编辑:进一步的调试使我发现不可能在PaintedShape.java中实例化任何新的三维javafx形状(参见堆栈跟踪-at javafx.scene.shape.Shape3D.<init>(Shape3D.java:74)
的第6行),可能是由于线程安全。我的一些修复包括在下面的回答中,但我没有提到,尽管我的代码被包装在Runnable()
中"稍后调用",但我也在相同的可运行结构中冗余嵌套了特定的JFXPanel生成代码。这可能有帮助,虽然我不确定是怎么回事。
Platform.runLater(() -> {
try {
SwingUtilities.invokeLater(() -> {
drawingPanel.add(shapePanel);
frame.pack();
});
} catch (Exception e) {
e.printStackTrace();
}
});
修复。我认为错误可能是,在从JPaneldrawingArea
中删除所有元素后,它不是立即validate()
d,因此不支持向其添加元素。我还确保JFXPanel是按类型输入的,而不是强制转换为组件。我还确保每次通过JPaneldrawing
按下按钮时都创建它所添加的JPanel,以防它由于某种原因处于先前重要的方法范围内。看到:
go.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JPanel drawing = new JPanel();
// irrelevant
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFXPanel(); // initializes JavaFX environment
if (selectedShape.getClass().getSuperclass().getSimpleName()
.equals("TwoDimensionalShape")) {
Component shapePanel = (Component) new PaintedShape(selectedShape).getPanel();
drawing.add(shapePanel);
} else {
JFXPanel shapePanel = (JFXPanel) new PaintedShape(selectedShape).getPanel();
Platform.runLater(() -> {
try {
SwingUtilities.invokeLater(() -> {
drawing.add(shapePanel);
frame.pack();
});
} catch (Exception e) {
e.printStackTrace();
}
});
}
drawingArea.removeAll();
drawingArea.validate();
drawingArea.add(drawing);
drawingArea.repaint();
frame.pack();
frame.setSize(new Dimension((int) (frame.getSize().getWidth()),
(int) (frame.getSize().getHeight() + PaintedShape.size.getHeight())));
frame.setLocationRelativeTo(null);
}
});
}
});
完整的代码和更改push到git repo,如果你更好奇的话。