java,swing - 在JScrollPane中添加JlayeredPane只显示空白背景

时间:2017-08-08 13:10:18

标签: java swing jpanel jscrollpane jlayeredpane

我已经为此搜索了一个解决方案,但它们没有用。也许是因为我的设置不同。我试图制作一个电梯模拟器,我有这个图: diagram 我有这个代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class Test extends JFrame {

    public Test() {

        Container contentPane = getContentPane();
        GridBagConstraints constraints = new GridBagConstraints();
        JButton floor = new JButton();
        JLabel cab = new JLabel();
        JPanel floorPanel = new JPanel();
        JPanel cabPanel = new JPanel();
        JLayeredPane layeredPane = new JLayeredPane();
        JScrollPane scroll = new JScrollPane();

        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        contentPane.setBackground(Color.BLUE);
        contentPane.setLayout(new GridBagLayout());

        constraints.insets = new Insets(20, 92, 20, 92);

        floorPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        floorPanel.setPreferredSize(new Dimension(165, 165));

        floor.setPreferredSize(new Dimension(165, 165));
        floor.setBackground(Color.WHITE);
        floor.setText("1");

        cab.setPreferredSize(new Dimension(165, 165));
        cab.setText("Cab");
        cab.setBackground(Color.BLACK);
        cab.setLocation(0, 0);

        cabPanel.setLayout(null);
        cabPanel.setPreferredSize(new Dimension(165, 165));
        cabPanel.setOpaque(false);
        cabPanel.add(cab);

        layeredPane.setLayout(new BorderLayout());
        layeredPane.setPreferredSize(new Dimension(165, 165));
        layeredPane.add(floorPanel, 0);
        layeredPane.add(cabPanel, 1);

        scroll.setMinimumSize(new Dimension(183, 660));
        scroll.setMaximumSize(new Dimension(183, 660));
        scroll.setViewportView(layeredPane);

        contentPane.add(scroll, constraints);

        pack();
        setVisible(true);
    }

}

它不会显示添加到JLayeredPane的两个JPanel,只显示空白背景。我在JLayeredPane的第二层中的JPanel上尝试了setOpaque(false),但它仍然没有工作。

如果您已经在某个地方找到了解决方案,请发送链接。 PS。抱歉错误的语法

1 个答案:

答案 0 :(得分:1)

再次使用JLayeredPane,您需要设置添加到JLayeredPane的任何组件的位置和位置,因为它的行为就好像它具有空布局,但是具有图层。

例如,签出并运行此代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class LayeredExample extends JPanel {

    public LayeredExample() {
        int gridRows = 20;
        int gridCols = 10;
        JPanel panelA = new JPanel(new GridLayout(gridRows, gridCols));
        for (int i = 0; i < gridRows; i++) {
            for (int j = 0; j < gridCols; j++) {
                String labelText = String.format("[%d, %d]", j + 1, i + 1);
                JLabel label = new JLabel(labelText);

                Border outsideBorder = BorderFactory.createLineBorder(Color.BLUE);
                Border insideBorder = BorderFactory.createEmptyBorder(3, 3, 3, 3);
                label.setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
                panelA.add(label);
            }
        }
        panelA.setBorder(BorderFactory.createTitledBorder("Panel A"));
        panelA.setSize(panelA.getPreferredSize());
        panelA.setLocation(0, 0);

        JLabel dragMeLabel = new JLabel("Drag Me!");
        dragMeLabel.setFont(dragMeLabel.getFont().deriveFont(Font.BOLD, 54f));
        JPanel panelB = new JPanel(new GridBagLayout());
        panelB.add(dragMeLabel);
        panelB.setBorder(BorderFactory.createTitledBorder("Panel B"));
        panelB.setPreferredSize(new Dimension(800, 500));
        panelB.setSize(panelB.getPreferredSize());
        panelB.setLocation(200, 200);

        MouseAdapter myMouse = new MouseAdapter() {
            private Point p0;
            private Point loc0;

            @Override
            public void mousePressed(MouseEvent e) {
                p0 = e.getLocationOnScreen();
                loc0 = ((JComponent) e.getSource()).getLocation();
            }

            private void moveComponent(MouseEvent e) {
                if (p0 == null || loc0 == null) {
                    return;
                }
                Point p1 = e.getLocationOnScreen();
                JComponent comp = (JComponent)e.getSource();
                Container cont = comp.getParent();
                int x = loc0.x + p1.x - p0.x;
                int y = loc0.y + p1.y - p0.y;
                Point loc1 = new Point(x, y);
                comp.setLocation(loc1);
                cont.repaint();
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                moveComponent(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                moveComponent(e);
                p0 = null;
                loc0 = null;
            }
        };

        panelB.addMouseListener(myMouse);
        panelB.addMouseMotionListener(myMouse);

        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.add(panelA, JLayeredPane.DEFAULT_LAYER);
        layeredPane.add(panelB, JLayeredPane.PALETTE_LAYER);
        layeredPane.setPreferredSize(new Dimension(1600, 1200));

        JScrollPane scrollPane = new JScrollPane(layeredPane);
        scrollPane.getViewport().setPreferredSize(new Dimension(800, 650));

        setLayout(new BorderLayout());
        add(scrollPane);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("LayeredExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new LayeredExample());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}