两个JPanel(有颜色,有大小的)置于JFrame里有一部分重叠,如何将其中一个置于上面,重叠部分能看到。
谁能帮我忙啊,先谢了!!!!!!!!!
add先后怎么显示没层次顺序的

解决方案 »

  1.   

    import javax.swing.*;
    import javax.swing.border.*;
    import javax.accessibility.*;import java.awt.*;
    import java.awt.event.*;/*
     * LayeredPaneDemo2.java is a 1.4 application that requires
     * images/dukeWaveRed.gif.
    */
    public class LayeredPaneDemo extends JPanel
                                  implements ActionListener,
                                             MouseMotionListener {
        private String[] layerStrings = { "Yellow (0)", "Magenta (1)",
                                          "Cyan (2)",   "Red (3)",
                                          "Green (4)",  "Blue (5)" };
        private Color[] layerColors = { Color.yellow, Color.magenta,
                                        Color.cyan,   Color.red,
                                        Color.green,  Color.blue };    private JLayeredPane layeredPane;
        private JLabel dukeLabel;
        private JCheckBox onTop;
        private JComboBox layerList;    //Action commands
        private static String ON_TOP_COMMAND = "ontop";
        private static String LAYER_COMMAND = "layer";    public LayeredPaneDemo()    {
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));        //Create and load the duke icon.
            final ImageIcon icon = createImageIcon("images/dukeWaveRed.gif");        //Create and set up the layered pane.
            layeredPane = new JLayeredPane();
            layeredPane.setPreferredSize(new Dimension(300, 310));
            layeredPane.setBorder(BorderFactory.createTitledBorder(
                                        "Move the Mouse to Move Duke"));
            layeredPane.addMouseMotionListener(this);        //Add several labels to the layered pane.
            layeredPane.setLayout(new GridLayout(2,3));
            for (int i = 0; i < layerStrings.length; i++) {
                JLabel label = createColoredLabel(layerStrings[i],
                                                  layerColors[i]);
                layeredPane.add(label, new Integer(i));
            }        //Create and add the Duke label to the layered pane.
            dukeLabel = new JLabel(icon);
            if (icon == null) {
                System.err.println("Duke icon not found; using black rectangle instead.");
                dukeLabel.setOpaque(true);
                dukeLabel.setBackground(Color.BLACK);
            }
            layeredPane.add(dukeLabel, new Integer(2), 0);        //Add control pane and layered pane to this JPanel.
            add(Box.createRigidArea(new Dimension(0, 10)));
            add(createControlPanel());
            add(Box.createRigidArea(new Dimension(0, 10)));
            add(layeredPane);
        }    /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = LayeredPaneDemo.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }    //Create and set up a colored label.
        private JLabel createColoredLabel(String text,
                                          Color color) {
            JLabel label = new JLabel(text);
            label.setVerticalAlignment(JLabel.TOP);
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setOpaque(true);
            label.setBackground(color);
            label.setForeground(Color.black);
            label.setBorder(BorderFactory.createLineBorder(Color.black));
            label.setPreferredSize(new Dimension(140, 140));
            return label;
        }    //Create the control pane for the top of the frame.
        private JPanel createControlPanel() {
            onTop = new JCheckBox("Top Position in Layer");
            onTop.setSelected(true);
            onTop.setActionCommand(ON_TOP_COMMAND);
            onTop.addActionListener(this);        layerList = new JComboBox(layerStrings);
            layerList.setSelectedIndex(2);    //cyan layer
            layerList.setActionCommand(LAYER_COMMAND);
            layerList.addActionListener(this);        JPanel controls = new JPanel();
            controls.add(layerList);
            controls.add(onTop);
            controls.setBorder(BorderFactory.createTitledBorder(
                                     "Choose Duke's Layer and Position"));
            return controls;
        }    //Make Duke follow the cursor.
        public void mouseMoved(MouseEvent e) {
            dukeLabel.setLocation(e.getX()-dukeLabel.getWidth()/2,
                                  e.getY()-dukeLabel.getHeight()/2);
        }
        public void mouseDragged(MouseEvent e) {} //do nothing    //Handle user interaction with the check box and combo box.
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();        if (ON_TOP_COMMAND.equals(cmd)) {
                if (onTop.isSelected())
                    layeredPane.moveToFront(dukeLabel);
                else
                    layeredPane.moveToBack(dukeLabel);        } else if (LAYER_COMMAND.equals(cmd)) {
                int position = onTop.isSelected() ? 0 : 1;
                layeredPane.setLayer(dukeLabel,
                                     layerList.getSelectedIndex(),
                                     position);
            }
        }    /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);        //Create and set up the window.
            JFrame frame = new JFrame("LayeredPaneDemo2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Create and set up the content pane.
            JComponent newContentPane = new LayeredPaneDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);        //Display the window.
            frame.pack();
            frame.setVisible(true);
        }    public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
      

  2.   

    对于上面置影片播放的JPanel就失效了,是不是因为加载速度慢了点(它出现的速度就比其它慢一拍),这个JPanel就移不到下面去,其它的JPanel一个置图片,一个置文本,不知道怎么做?
      

  3.   

    下面这个也许更简单明了,你试着改改JLabel为其他Component就行(我下线了):import javax.swing.*;
    import java.awt.*;public class JLayeredPaneTest extends JPanel
    {
    public JLayeredPaneTest() {
    setLayout( new BorderLayout() ); JLayeredPane layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(new Dimension(300, 310));
    layeredPane.setBorder(BorderFactory.createTitledBorder( "测试"));
    JLabel blueLabel = new JLabel();
    blueLabel.setVerticalAlignment(JLabel.TOP);
            blueLabel.setHorizontalAlignment(JLabel.CENTER);
    blueLabel.setOpaque(true);
    blueLabel.setBounds( 20,20,100,100 );
    blueLabel.setBackground( Color.blue ); JLabel redLabel = new JLabel();
    redLabel.setVerticalAlignment(JLabel.TOP);
            redLabel.setHorizontalAlignment(JLabel.CENTER);
    redLabel.setOpaque(true);
    redLabel.setBounds( 80,80,100,100 );
    redLabel.setBackground( Color.red ); layeredPane.add( blueLabel,new Integer(1) );
    layeredPane.add( redLabel,new Integer(5) ); add( layeredPane,BorderLayout.CENTER );
    setOpaque( true );
    } private static void createAndShowUI() {
    //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);        //Create and set up the window.
            JFrame frame = new JFrame("LayeredPaneDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane( new JLayeredPaneTest() );        //Display the window.
            frame.pack();
            frame.setVisible(true);
    } public static void main(String[] args) 
    {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowUI();
                }
            });
    }
    }