当我缩放JPanel时,它会缩放并且其组件会缩放,但它会转换为绘制的组件,而不是其自身的组件,如果JTextField和原始组件保持其大小和位置,我无法控制它或键入它如图所示
这是我的代码
我在这个类中制作我想要放大的面板
public class designPanel extends JPanel{
public double zoomFactor = 1;
public boolean zoomer = true;
public AffineTransform at;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (zoomer == true) {
at = g2.getTransform();
at.scale(zoomFactor, zoomFactor);
zoomer = false;
}
g2.transform(at);
}
public void setZoomFactor(double factor){
if(factor<this.zoomFactor){
this.zoomFactor=this.zoomFactor/1.1;
}
else{
this.zoomFactor=factor;
}
this.zoomer=true;
}
public double getZoomFactor() {
return zoomFactor;
}
}
然后在此代码中通过鼠标滚轮放大和缩小此面板
design_gridPanel.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
design_gridPanel.setZoomFactor(1.1 * design_gridPanel.getZoomFactor());
design_gridPanel.revalidate();
design_gridPanel.repaint();
}
if (e.getWheelRotation() > 0) {
design_gridPanel.setZoomFactor(design_gridPanel.getZoomFactor() / 1.1);
design_gridPanel.revalidate();
design_gridPanel.repaint();
}
}});