1

我正在努力学习JMapViewer,并将地图嵌入到 中JPanel,这是CardLayout. 此时,我只想显示一个没有缩放、鼠标动作侦听器等的地图。所以,我在 NetBeans 中创建了一个 GUI 框架,其中CardLayout包含多个JPanels。在其中一个面板中,我添加了另一个面板,该面板应位于地图所在的位置。然后我添加了JMapViewer.jarJMapViewer_src.jar。然后我添加了以下简单代码:

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;

}

面板是空的。我有Demo.java代码,如果我只是将它复制并粘贴到一个新项目中,它就可以工作。但我想从构建一个简单的地图并将其添加到我的面板开始对其进行修改。我错过了什么?

谢谢!

4

1 回答 1

4

如果没有完整的示例,您的程序在哪里出错并不清楚。常见问题包括:

  • 将地图添加到意外容器。

  • 调用添加地图pack(),无需重新验证容器。

通常,通过限制 GUI 编辑器的范围来避免此类问题,如此处建议的那样。在下面的示例中,将 的实例CardPanel添加到JPanelhave 中CardLayout

cards.add(new CardPanel("London", new Coordinate(51.5072, -0.1275)));
cards.add(new CardPanel("Paris", new Coordinate(48.8566, 2.3522)));

每个这样的实例都有一个GridLayout,但您可以替换为 aBorderLayout并尝试将地图添加到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);
    }
}
于 2016-08-13T15:43:52.380 回答