重叠AWT线和Swing JLabel

时间:2011-12-21 09:05:12

标签: java swing awt jlabel overlapping

我的应用程序使用行原语和JLables时遇到问题。我试着解释一下:

  • 我必须使用线条来描绘道路并使用JLabels来表示城市。我需要使用JLabels因为每个JLabel都有一个监听器,它显示一个包含城市信息的对话框。
  • 我重新定义了我的主paint()的{​​{1}}方法。在该方法中,我首先调用JPanel,然后绘制线条,最后将标签添加到super.paint()

问题在于线条与标签重叠,无论绘制它们的顺序如何。有什么建议吗?

enter image description here

3 个答案:

答案 0 :(得分:3)

您还可以覆盖paintComponent()的{​​{1}}或paintChildren()方法。 在JPanel调用您的线条绘制,然后paintChildren()绘制super

答案 1 :(得分:2)

我不确定这是否是正确的方法,但你可以试试这个:

  1. 创建2个面板。一个用于绘制线条,另一个用于绘制建筑物(标签)。
  2. LayeredPane的{​​{1}}中添加两个面板。在下层添加线条,然后在面板上添加标签。
  3. 您也可以通过其他方式使用JFrame来解决问题。点击此处了解详情:How to use Layered Panes in java

答案 2 :(得分:2)

另一种方式应该是

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class AddVertexDemo {

    public AddVertexDemo() {
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("AddVertexDemo");
        frame.getContentPane().add(new Gui().getMainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}

class DrawingPanel extends JPanel {

    private static final int RADIUS = 6;
    private static final long serialVersionUID = 1L;
    private List<Shape> vertexList = new ArrayList<Shape>();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if (vertexList.size() > 1) {
            Shape s0 = vertexList.get(0);
            Shape s1 = null;
            for (int i = 0; i < vertexList.size(); i++) {
                s1 = vertexList.get(i);
                drawConnectingLine(g, s0, s1);
                s0 = s1;
            }
            s1 = vertexList.get(0);
            //drawConnectingLine(g2, s0, s1);
        }
        for (Shape shape : vertexList) {
            g2.setColor(Color.blue);
            g2.fill(shape);
            g2.setColor(Color.blue.darker().darker());
            g2.draw(shape);
        }
    }

    private void drawConnectingLine(Graphics g, Shape s0, Shape s1) {
        Rectangle r0 = s0.getBounds();
        Rectangle r1 = s1.getBounds();
        int x0 = r0.x + r0.width / 2;
        int y0 = r0.y + r0.height / 2;
        int x1 = r1.x + r1.width / 2;
        int y1 = r1.y + r1.height / 2;
        g.drawLine(x0, y0, x1, y1);
    }

    public void addVertex(Point p) {
        int x = p.x - RADIUS;
        int y = p.y - RADIUS;
        int w = 2 * RADIUS;
        int h = w;
        vertexList.add(new Ellipse2D.Double(x, y, w, h));
        repaint();
    }

    public void removeVertex(Point p) {
        if (vertexList.size() > 0) {
            for (int i = vertexList.size() - 1; i >= 0; i--) {
                if (vertexList.get(i).contains(p)) {
                    vertexList.remove(i);
                    repaint();
                    return;
                }
            }
        }
    }
}

 class Gui {

    private static final Dimension DRAWING_PANEL_SIZE = new Dimension(600, 500);
    private JPanel mainPanel = new JPanel(new BorderLayout());
    private DrawingPanel drawingPanel = new DrawingPanel();
    private JToggleButton addVertexBtn = new JToggleButton("Add Vertex");
    private JToggleButton removeVertexBtn = new JToggleButton("Remove Vertex");

    Gui() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addVertexBtn);
        buttonPanel.add(removeVertexBtn);
        DrawPanelMouseListener mouseListener = new DrawPanelMouseListener();
        mouseListener.setDrawingPanel(drawingPanel);
        mouseListener.setGui(this);
        drawingPanel.addMouseListener(mouseListener);
        drawingPanel.setPreferredSize(DRAWING_PANEL_SIZE);
        drawingPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        mainPanel.add(drawingPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public boolean isAddingVertex() {
        return addVertexBtn.isSelected();
    }

    public boolean isRemovingVertex() {
        return removeVertexBtn.isSelected();
    }

    public void setAddingVertex(boolean addingVertex) {
        addVertexBtn.setSelected(addingVertex);
    }

    public void setRemovingVertex(boolean removingVertex) {
        removeVertexBtn.setSelected(removingVertex);
    }
}

class DrawPanelMouseListener extends MouseAdapter {

    private DrawingPanel drawingPanel;
    private Gui gui;

    public DrawPanelMouseListener() {
    }

    public void setGui(Gui gui) {
        this.gui = gui;
    }

    public void setDrawingPanel(DrawingPanel drawingPanel) {
        this.drawingPanel = drawingPanel;
    }

    @Override
    public void mousePressed(MouseEvent me) {
        if (gui.isAddingVertex() && gui.isRemovingVertex()) {
            gui.setAddingVertex(false);
            gui.setRemovingVertex(false);
            return;
        }
        if (gui.isAddingVertex()) {
            drawingPanel.addVertex(me.getPoint());
            gui.setAddingVertex(false);
        }
        if (gui.isRemovingVertex()) {
            drawingPanel.removeVertex(me.getPoint());
            gui.setRemovingVertex(false);
        }
    }
}