如何在 JFrame 中设置 glasspane 的位置? [英] how to set the position of glasspane in JFrame?

查看:19
本文介绍了如何在 JFrame 中设置 glasspane 的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从框架的左侧创建一个菜单滑块.它需要浮动在内容上方,我可以始终通过鼠标侦听器访问它(当鼠标靠近左边缘时打开菜单).

I'm trying to create a menu slider from the left side of the frame. It need to be floating above the content, where i can access it always with a mouse listener(open the menu when the mouse is close to the left edge).

好吧,我将 GlassPane (My JPanel) 设置为 setOpaue(false),它漂浮在内容之上.但是玻璃板总是位于中心,我需要有可能移动它,滑动它,但没有运气.

Well, I set my GlassPane (My JPanel ) as setOpaue(false) and it was floating above the content. but the glasspane always positioned on the center, and I need to have a possibility to move it,slide it, but with no luck.

setBounds 和 setLocation 对我不起作用.

the setBounds and setLocation does not worked for me.

谁能帮帮我?

部分代码:

   public class MYFrame extends JFrame {


        public MYFrame(){
                this.setLayout(new BorderLayout());
                this.add(panel1,BorderLayout.NORTH);
                this.add(panel2,BorderLayout.CENTER);
                this.add(panel3,BorderLayout.EAST);
                this.getRootPane().setGlassPane(new MyGlass());
                this.getRootPane().getGlassPane().setVisible(true);
                this.setVisible(true);

             }


      public class MyGlass extends JPanel{
        ImageIcon imageIcon = new ImageIcon("BG.png");
        JLabel label = new JLabel(imageIcon);

        public MyGlass(){
          this.add(label);
          this.setOpaque(false);
          this.pack();
          this.setVisible(true);
        }

     }
  }

推荐答案

glassPane 会一直覆盖整个窗口的可视区域(内容),就是这样设计的.

The glassPane will always cover the entire viewable area (content) of the window, this is how it's designed.

您需要做的是将您的 MyGlass 面板添加到框架的 glassPane 中.至少,这将为您提供移动它的控制权.

What you will want to do is, instead, add your MyGlass panel to the frame's glassPane. This will, at least, provide you with control to move it.

现在,话虽如此,我建议您看看

Now, having said, I would recommend having a look at

看看有没有帮助.

作为一个疯狂的概念证明......

As a wild proof of concept...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.setGlassPane(new GlassPane());
                frame.getGlassPane().setVisible(true);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                add(new JLabel(new ImageIcon(ImageIO.read(new File("....jpg")))));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    public class GlassPane extends JPanel {

        private MenuPane menuPane;

        public GlassPane() {
            setOpaque(false);
            menuPane = new MenuPane();
            add(menuPane);
            setLayout(new SlidingMenuLayoutManager());

            menuPane.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    ((SlidingMenuLayoutManager)getLayout()).open();
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    ((SlidingMenuLayoutManager)getLayout()).close();
                }

            });
        }

        protected class SlidingMenuLayoutManager implements LayoutManager {

            private int offSet = 10;
            private int delta = 2;
            private Timer slider;

            public SlidingMenuLayoutManager() {
                slider = new Timer(10, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("...");
                        offSet += delta;
                        if (offSet <= 10) {
                            offSet = 10;
                            ((Timer) e.getSource()).stop();
                        } else if (offSet >= menuPane.getWidth()) {
                            offSet = menuPane.getWidth();
                            ((Timer) e.getSource()).stop();
                        }
                        menuPane.getParent().revalidate();
                        menuPane.getParent().repaint();
                    }
                });
            }

            public void open() {
                slider.stop();
                delta = 2;
                slider.start();
            }

            public void close() {
                slider.stop();
                delta = -2;
                slider.start();
            }

            @Override
            public void addLayoutComponent(String name, Component comp) {
            }

            @Override
            public void removeLayoutComponent(Component comp) {
            }

            @Override
            public Dimension preferredLayoutSize(Container parent) {
                Dimension size = menuPane.getPreferredSize();
                size.width *= 2;
                return size;
            }

            @Override
            public Dimension minimumLayoutSize(Container parent) {
                return preferredLayoutSize(parent);
            }

            @Override
            public void layoutContainer(Container parent) {
                Dimension size = menuPane.getPreferredSize();
                size.height = parent.getHeight();
                menuPane.setSize(size);

                int maxWidth = size.width;
                int xPos = offSet - maxWidth;
                menuPane.setLocation(xPos, 0);
            }

        }

    }

    public class MenuPane extends JPanel {

        public MenuPane() {
            setOpaque(false);
            setBackground(new Color(0, 0, 255, 128));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }

    }

}

这篇关于如何在 JFrame 中设置 glasspane 的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆