为什么JLabel被绘制在JLayeredPane中的更高组件上?

时间:2017-04-08 19:47:57

标签: java swing paintcomponent jlayeredpane

我有一个有四层的JLayeredPane:

  1. JPanel设置为背景
  2. 每个持有JLabel
  3. 的JPanel网格
  4. JPanel的网格,每个网格都有几个JLabel,只有在下面面板中的标签为空时才设置为可见
  5. 一个自定义组件,仅用于覆盖paintComponent()方法以绘制
  6. 下面的所有内容

    出于某种原因,如果我更改第3层中标签的背景颜色然后绘制到第4层,则第3层中的标签将绘制在第4层绘制的图形上。我试图在各种颜色上设置ignoreRepaint()组件以及不透明和代码结构,但都无济于事 有谁知道如何防止这种情况发生?

    我不会附加源代码,因为项目非常大但是我附加了一个独立程序运行的示例,当你点击"添加箭头&#时显示我的问题34;按钮。

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Polygon;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    import javax.swing.border.LineBorder;
    
    public class GraphicsTest {
    
    @SuppressWarnings("serial")
    class Painter extends JComponent {
    
        public Painter(int x, int y) {
            setBounds(0, 0, x, y);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        }
    
    }
    
    private static final int CELL_SIZE = 40;
    private static final int NOTE_SIZE = 20;
    private JFrame frame;
    private static JButton test;
    private static JButton clear;
    private static JLayeredPane pane = new JLayeredPane();
    private static JPanel back = new JPanel();
    private static JPanel[][] cellPanels = new JPanel[10][10];
    private static JLabel[][] cells = new JLabel[10][10];
    private static JPanel[][] notePanels = new JPanel[10][10];
    private static JLabel[][][] notes = new JLabel[10][10][4];
    private static Painter painter;
    
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GraphicsTest window = new GraphicsTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the application.
     */
    public GraphicsTest() {
        initialize();
    }
    
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setSize(600, 700);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
    
        pane.setBounds(50, 50, 500, 500);
        pane.setLayout(null);
        frame.getContentPane().add(pane);
    
        back.setBounds(0, 0, 500, 500);
        back.setBackground(Color.BLACK);
        pane.add(back, new Integer(100));
    
        for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++) {
                String text = "";
                if ((i % 2) == 1 && (k % 2) == 1) text = (i + k) + "";
                cellPanels[i][k] = new JPanel();
                cellPanels[i][k].setBounds(k * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                cellPanels[i][k].setBackground(Color.WHITE);
                cellPanels[i][k].setBorder(new LineBorder(Color.BLACK, 1));
                cells[i][k] = new JLabel(text);
                cells[i][k].setBounds(0, 0, CELL_SIZE, CELL_SIZE);
                cells[i][k].setOpaque(false);
                cellPanels[i][k].add(cells[i][k]);
                pane.add(cellPanels[i][k], new Integer(200));
            }
        }
    
        boolean display;
        for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++) {
                if ((i % 2) == 0 && (k % 2) == 0) {
                    display = true;
                } else {
                    display = false;
                }
                notePanels[i][k] = new JPanel();
                notePanels[i][k].setBounds(k * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                notePanels[i][k].setBackground(Color.WHITE);
                notePanels[i][k].setBorder(new LineBorder(Color.BLACK, 1));
                notePanels[i][k].setLayout(null);
                for (int m = 0; m < 2; m++) {
                    for (int p = 0; p < 2; p++) {
                        notes[i][k][(m * 2) + p] = new JLabel(30 + "");
                        notes[i][k][(m * 2) + p].setBounds(m * NOTE_SIZE, p * NOTE_SIZE, NOTE_SIZE, NOTE_SIZE);
                        notes[i][k][(m * 2) + p].setOpaque(true);
                        notePanels[i][k].add(notes[i][k][(m * 2) + p]);
                    }
                }
                if (display) {
                    notePanels[i][k].setVisible(true);
                } else {
                    notePanels[i][k].setVisible(false);
                }
                pane.add(notePanels[i][k], new Integer(300));
            }
        }
    
        painter = new Painter(500, 500);
        pane.add(painter, new Integer(400));
    
        test = new JButton("Add Arrow");
        test.setBounds(50, 600, 100, 25);
        test.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                highlightNotes();
                Arrow.drawArrow(painter.getGraphics(), 20, 20, 400, 400, 20, 30, 40, Color.BLACK, Color.GREEN);
            }
        });
        frame.getContentPane().add(test);
    
        clear = new JButton("Clear");
        clear.setBounds(175, 600, 100, 25);
        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                painter.repaint();
            }
        });
        frame.getContentPane().add(clear);
    
    
    
    }
    
    private static void highlightNotes() {
        for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++) {
                for (int n = 0; n < 4; n++) {
                    notes[i][k][n].setBackground(Color.BLUE);
                }
            }
        }
    }
    
    static class Arrow {
    
        public static void drawArrow(Graphics g, int tailx, int taily, int headx, int heady,
                int shaftw, int headw, int headh, Color outline, Color fill) {
            if ((shaftw % 2) == 0) {
                shaftw--;
            }
            if ((headw % 2) == 0) {
                headw--;
            }
            if ((headh % 2) == 0) {
                headh--;
            }
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            double length = Math.sqrt((double) (((headx - tailx) * (headx - tailx))
                    + ((heady - taily) * (heady - taily))));
            int tailLength = (int) (length - headw) + 1;
            double theta = Math.atan2(heady - taily, headx - tailx);
            Point point1 = new Point(0, -(shaftw / 2));
            point1 = getTransPoint(point1, theta);
            point1.x += tailx;
            point1.y += taily;
            Point point2 = new Point(tailLength, -(shaftw / 2));
            point2 = getTransPoint(point2, theta);
            point2.x += tailx;
            point2.y += taily;
            Point point3 = new Point(tailLength, -(headw / 2));
            point3 = getTransPoint(point3, theta);
            point3.x += tailx;
            point3.y += taily;
            Point point4 = new Point((int) length, 0);
            point4 = getTransPoint(point4, theta);
            point4.x += tailx;
            point4.y += taily;
            Point point5 = new Point(tailLength, (headw / 2));
            point5 = getTransPoint(point5, theta);
            point5.x += tailx;
            point5.y += taily;
            Point point6 = new Point(tailLength, (shaftw / 2));
            point6 = getTransPoint(point6, theta);
            point6.x += tailx;
            point6.y += taily;
            Point point7 = new Point(0, (shaftw / 2));
            point7 = getTransPoint(point7, theta);
            point7.x += tailx;
            point7.y += taily;
            //Create arrow at tail coordinates passed in
            Polygon arrow = new Polygon();
            arrow.addPoint(point1.x, point1.y);
            arrow.addPoint(point2.x, point2.y);
            arrow.addPoint(point3.x, point3.y);
            arrow.addPoint(point4.x, point4.y);
            arrow.addPoint(point5.x, point5.y);
            arrow.addPoint(point6.x, point6.y);
            arrow.addPoint(point7.x, point7.y);
            //Draw and fill the arrow
            g2.setColor(fill);
            g2.fillPolygon(arrow);
            g2.setColor(outline);
            g2.drawPolygon(arrow);
        }
    
        private static Point getTransPoint(Point point, double theta) {
            int x = (int) ((point.x * Math.cos(theta)) - (point.y * Math.sin(theta)));
            int y = (int) ((point.y * Math.cos(theta)) + (point.x * Math.sin(theta)));
            return new Point(x, y);
        }
    
    }
    

    }

0 个答案:

没有答案
相关问题