我在Jlayeredpane中有三个Jpanels,我在Jlayeredpane中添加了mousewheellistener。当我创建一个事件时,所有组件(包括Jpanel中的三个Jpanel和组件)将调整大小(变大或变小),我尝试了很多方法,但是我无法解决,谁能举个例子?谢谢!
// Jlayeredpane
package test;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import test.MouseWheelListener;
public class MapView extends JPanel {
private Dimension preferredSize = new Dimension(930, 600);
private JLayeredPane jLayeredPane = new JLayeredPane();
private JPanel bgPanel = new JPanel();//bgPanel
private JPanel roomPanel = new JPanel();//roomPanel
private JPanel infoPanel = new JPanel();//infoPanel
private Rectangle2D[] rects = new Rectangle2D[50];
private ImageIcon imageIcon;
public MapView() throws IOException {
File imageFile = new File("res"+File.separator+"OK1.jpg");
final Image image = ImageIO.read(imageFile);
imageIcon = new ImageIcon(image.getScaledInstance(
getPreferredSize().width, getPreferredSize().height,
Image.SCALE_FAST));
final JLabel bg = new JLabel(imageIcon);
final Point origin = new Point(0, 0);
final Rectangle rectangle = new Rectangle(origin, getPreferredSize());
bgPanel.setBounds(rectangle);
bgPanel.add(bg);
setPreferredSize(getPreferredSize());
JButton jButton = new JButton("test");
bgPanel.setOpaque(false);
roomPanel.setOpaque(false);
infoPanel.setOpaque(false);
roomPanel.setBounds(rectangle);
infoPanel.setBounds(rectangle);
add(bgPanel, new Integer(1));
add(roomPanel, new Integer(2));
add(infoPanel, new Integer(3));
for (int i = 0; i < rects.length; i++) {
rects[i] = new Rectangle2D.Double(Math.random() * .8,
Math.random() * .8, Math.random() * .2, Math.random() * .2);
}
MouseWheelListener mouseWheelListener = new MouseWheelListener(this,
getPreferredSize());
addMouseWheelListener(mouseWheelListener);
}
public Dimension getPreferredSize() {
return preferredSize;
}
private Rectangle2D r = new Rectangle2D.Float();
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
int w = getWidth();
int h = getHeight();
for (Rectangle2D rect : rects) {
r.setRect(rect.getX() * w, rect.getY() * h, rect.getWidth() * w,
rect.getHeight() * h);
((Graphics2D) g).draw(r);
}
}
}
// mousewheellistener
package test;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseWheelEvent;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MouseWheelListener implements java.awt.event.MouseWheelListener {
private JPanel panel;
private Dimension preferredSize = new Dimension(960, 680);
public MouseWheelListener(JPanel panel,Dimension preferredSize) {
this.panel = panel;
this.preferredSize=preferredSize;
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
updatePreferredSize(e.getWheelRotation(), e.getPoint());
}
private void updatePreferredSize(int wheelRotation, Point stablePoint) {
double scaleFactor = findScaleFactor(wheelRotation);
scaleBy(scaleFactor);
Point offset = findOffset(stablePoint, scaleFactor);
offsetBy(offset);
panel.getParent().doLayout();
}
private double findScaleFactor(int wheelRotation) {
double d = wheelRotation * 1.08;
return (d > 0) ? 1 / d : -d;
}
private void scaleBy(double scaleFactor) {
int w = (int) (panel.getWidth() * scaleFactor);
int h = (int) (panel.getHeight() * scaleFactor);
preferredSize.setSize(w, h);
}
private Point findOffset(Point stablePoint, double scaleFactor) {
int x = (int) (stablePoint.x * scaleFactor) - stablePoint.x;
int y = (int) (stablePoint.y * scaleFactor) - stablePoint.y;
return new Point(x, y);
}
private void offsetBy(Point offset) {
Point location = panel.getLocation();
panel.setLocation(location.x - offset.x, location.y - offset.y);
}
}
//测试Jframe
package test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
public class Test extends JFrame {
private Container main = this.getContentPane();
public Test() {
this.setTitle("TEST");
this.setSize((int) (960), (int) (680));
this.setLocation(0, 0);
this.setResizable(false);
this.setUndecorated(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void init() throws IOException {
initContent();
this.setVisible(true);
}
private void initContent() throws IOException {
MapView mapPanel = new MapView();
JScrollPane infoPanel = new JScrollPane(mapPanel);
this.addKeyListener(new KeyListener(mapPanel));
main.add(infoPanel, BorderLayout.CENTER);
}
public static void main(String[] args) throws IOException {
Test window = new Test();
window.init();
}
}