我在 NetBeans 8.0 平台应用程序中创建了一个 TopComponent。我想在其上显示画布,但是当调用代码以带出 worldwind 组件并引发异常时:
“发生了 java.lang.IllegalStateException 异常。单击显示详细信息或查看位于 C:\Users\abradford\Desktop\NetBeans Projects\MTAET\MTAET\build\testuserdir\var\log 文件夹中的 messages.log 文件。”
java.lang.IllegalStateException:找不到具有preferredID EarthTopComponent 的TopComponent,有关更多详细信息,请参阅IDE 日志。
为此,TopComponent 依赖于 WorldWind.jar 文件和 Jogl 文件。而 WorldWind.jar 文件仅依赖于 Jogl 文件。我只想问是否有人可以解释为什么 worldwind 组件不能与 TopComponent 一起使用。如果有人已经有一段已经可以工作的代码来桥接或解决它,那也太棒了。自从我编写 Java 程序以来已经有一段时间了,我可能已经开始对这一切有点强烈,如果答案很明显,请原谅我。
这是我正在使用的代码: V 这是 TopComponent V
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
@TopComponent.Description(
preferredID = "EarthTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(
mode = "editor",
openAtStartup = true
)
@ActionID(
category = "Window",
id = "EarthTopComponent"
)
@ActionReference(
path = "Menu/Window"
)
@TopComponent.OpenActionRegistration(
displayName = "Earth",
preferredID = "EarthTopComponent"
)
@NbBundle.Messages({"CTL_EarthViewer=Earth View",
"HINT_EarthViewer=This is the Earth View"
})
public class EarthTopComponent extends TopComponent {
public EarthTopComponent() {
setName(Bundle.CTL_EarthViewer());
setToolTipText(Bundle.HINT_EarthViewer());
GUIWorldWind gui = new GUIWorldWind();
add(gui.getFrame());
}
}
_________________________________________________
V This is the Canvas Class V
package Earth;
//Basic Java Imports
import javax.swing.*;
import java.awt.*;
//import java.util.ArrayList;
//imports for layers
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.layers.Earth.*;
//Imports for Geometry
//import gov.nasa.worldwind.render.*;
//import gov.nasa.worldwind.geom.Position;
//import gov.nasa.worldwind.layers.RenderableLayer;
//imports for world wind libraries
import gov.nasa.worldwind.*;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.avlist.AVKey;
public class CanvasPanel extends JPanel
{
//World Wind GUI components
private LayerList layerlist; //Holds all Layers
private WorldWindowGLCanvas canvas; //Displays Model
private Model model; //world and layers
//Swing and AWT GUI components
JPanel mainPanel; //Main focus of the GUI
/*
* Constructs the panel when the class is called
*/
public CanvasPanel()
{
//canvas and layerlist variables
canvas = new WorldWindowGLCanvas();
loadPanel(); //loads the panel
loadLayers(); //loads all default layers
//sets the canvas to fill the panelspace
canvas.setPreferredSize(new Dimension((mainPanel.getWidth() - 100), (mainPanel.getHeight() - 100)));
//Creates a new model to display in the canvas
model = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
//adds all relevant layers to the model
model.setLayers(layerlist);
//adds the model to the canvas
canvas.setModel(model);
}
/**
* Sends the Panel when requested
*
* @return mainPanel, as JPanel object
*/
public JPanel getPanel()
{
return mainPanel;
}
/**
* called in the constructor, this method constructs the Swing and AWT
* components of the Panel.
*/
private void loadPanel()
{
//Creates and sets the dimensions of the Panel
mainPanel = new JPanel();
mainPanel.setSize(700, 700);
mainPanel.setBackground(Color.DARK_GRAY);
mainPanel.setBorder(
BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(0, 0, 0))
);
//Adds the canvas to the panel
mainPanel.add(canvas, BorderLayout.CENTER);
}
/**
* Adds relevant layers to layerlist to be added to the model
*/
private void loadLayers()
{
//creates a new layerlist array
layerlist = new LayerList();
//preloading all of the default layers
layerlist.add(new StarsLayer()); //Stars
layerlist.add(new SkyGradientLayer()); //Atmosphere
layerlist.add(new BMNGWMSLayer()); //BlueMarble Globe
layerlist.add(new CountryBoundariesLayer());//Political Boundaries
layerlist.add(new MSVirtualEarthLayer()); //City View
layerlist.add(new NASAWFSPlaceNameLayer()); //Names of Places
layerlist.add(new LatLonGraticuleLayer()); //Lat and Long Grid
}
}
V 这将两者放在一起 V
protected void displayGUI(final String title) { //创建框架对象 guiFrame = new JFrame(title);
//sets exit button properties
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creates gui components
menuBar = new FileMenuBar();
sideBar = new OptionSideBar();
canvasPanel = new CanvasPanel();
//adds objects to the form
guiFrame.setJMenuBar(menuBar.getMenu());
guiFrame.add(sideBar.getSideBar(), BorderLayout.WEST);
guiFrame.add(canvasPanel.getPanel(), BorderLayout.CENTER);
//loads the form
guiFrame.pack();
//determines the size of the form
Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
int scrnWidth = guiFrame.getSize().width;
int scrnHeight = guiFrame.getSize().height;
int x = (scrnSize.width - scrnWidth) / 2;
int y = (scrnSize.height - scrnHeight) / 2;
//places form on the screen
guiFrame.setLocation(x,y);
//sets for to visible
guiFrame.setVisible(true);
}