[英]JMapViewer in NetBeans: adding only map to a GUI JPanel
I am trying to learn JMapViewer
, and have a map embedded in a JPanel
, which is a part of CardLayout
. 我正在尝试学习JMapViewer
,并在JPanel
嵌入了地图,该JMapViewer
是CardLayout
的一部分。 At this point, I only want to display a map without zoom, mouse action listeners, etc. So, I created a GUI frame in NetBeans with CardLayout
and a number of JPanel
s. 此时,我只想显示一个没有缩放,鼠标动作监听器等的地图。因此,我在NetBeans中使用CardLayout
和许多JPanel
创建了一个GUI框架。 Inside one of the panels, I have added another panel where the map should be located. 在其中一个面板中,我添加了另一个面板,该面板应放置地图。 Then I added JMapViewer.jar
and JMapViewer_src.jar
. 然后我添加了JMapViewer.jar
和JMapViewer_src.jar
。 Then I added the following simple code: 然后,我添加了以下简单代码:
package viewController;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent;
import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener;
public class PanelAcars extends javax.swing.JPanel implements JMapViewerEventListener
{
public PanelAcars()
{
super();
setSize(400,400);
initComponents();
final JMapViewer map = new JMapViewer();
pnlAcarsMapView.add(map);
}
@Override
public void processCommand(JMVCommandEvent command) {
if (command.getCommand().equals(JMVCommandEvent.COMMAND.ZOOM) ||
command.getCommand().equals(JMVCommandEvent.COMMAND.MOVE)) {
//updateZoomParameters();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pnlAcarsMapView = new javax.swing.JPanel();
...
}
private javax.swing.JPanel pnlAcarsMapView;
}
The panel is empty. 面板是空的。 I have the Demo.java
code and it works if I simply copy and paste it in a new project. 我有Demo.java
代码,如果我简单地将其复制并粘贴到新项目中,它将起作用。 But I would like to modify it starting from building a simple map and adding it to my panel. 但我想从构建一个简单的地图并将其添加到我的面板开始对其进行修改。 What am I missing? 我想念什么?
Thank you! 谢谢!
Absent a complete example, it's not clear where your program goes awry. 没有完整的示例,尚不清楚您的程序在哪里出错。 Common problems include these: 常见问题包括:
Adding the map to an unintended container. 将地图添加到意外容器中。
Adding the map after calling pack()
, without revalidating the container. 在调用pack()
之后添加地图,而无需重新验证容器。
In general, avoid such problems by limiting the scope of the GUI editor as suggested here . 通常,通过限制此处建议的GUI编辑器的范围来避免此类问题。 In the example below, instances of CardPanel
are added to a JPanel
having CardLayout
. 在以下示例中,将CardPanel
实例添加到具有CardLayout
的JPanel
中。
cards.add(new CardPanel("London", new Coordinate(51.5072, -0.1275)));
cards.add(new CardPanel("Paris", new Coordinate(48.8566, 2.3522)));
Each such instance has a GridLayout
, but you might substitute a BorderLayout
and experiment with adding the map to the CENTER
and other components in the surrounding regions. 每个这样的实例都有一个GridLayout
,但是您可以替代BorderLayout
并尝试将地图添加到CENTER
和周围区域的其他组件中。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
/**
* @see https://stackoverflow.com/a/38934376/230513
* @see https://stackoverflow.com/a/36392696/230513
* @see https://stackoverflow.com/a/36243395/230513
*/
public class CardPanel extends JPanel {
private static final JPanel cards = new JPanel(new CardLayout());
private final String name;
public CardPanel(String name, Coordinate coordinate) {
super(new GridLayout());
this.name = name;
JMapViewer map = new JMapViewer() {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
map.setDisplayPosition(coordinate, 12);
this.add(map);
}
@Override
public String toString() {
return name;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
create();
}
});
}
private static void create() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cards.add(new CardPanel("London", new Coordinate(51.5072, -0.1275)));
cards.add(new CardPanel("Paris", new Coordinate(48.8566, 2.3522)));
JPanel control = new JPanel();
control.add(new JButton(new AbstractAction("\u22b2Prev") {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) cards.getLayout();
cl.previous(cards);
}
}));
control.add(new JButton(new AbstractAction("Next\u22b3") {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) cards.getLayout();
cl.next(cards);
}
}));
f.add(cards, BorderLayout.CENTER);
f.add(control, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.