我试图从框架的左侧创建一个菜单滑块。 它需要浮动在内容之上,我可以使用鼠标监听器来访问它(当鼠标靠近左边缘时打开菜单)。
好吧,我将我的GlassPane(我的JPanel)设置为setOpaue(false),它漂浮在内容之上。 但玻璃窗总是位于中心位置,我需要有可能移动它,滑动它,但没有运气。
setBounds和setLocation对我不起作用。
任何人都可以帮我这个吗?
代码的一部分:
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);
}
}
}
答案 0 :(得分:2)
glassPane
将始终覆盖窗口的整个可视区域(内容),这就是它的设计方式。
您要做的是将MyGlass
面板添加到框架的glassPane
。这将至少为您提供移动它的控制权。
现在,说过,我建议看看
看看它是否有帮助。
作为概念的狂野证明......
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());
}
}
}