Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将缩放和平移功能添加到JPanel?_Java_Swing_Gridbaglayout_Zooming_Pan - Fatal编程技术网

Java 如何将缩放和平移功能添加到JPanel?

Java 如何将缩放和平移功能添加到JPanel?,java,swing,gridbaglayout,zooming,pan,Java,Swing,Gridbaglayout,Zooming,Pan,所以我有一个Java程序,它使用网格袋布局来创建单元格(如图形纸),如果鼠标在其中一个单元格上,那么单元格将改变其颜色。我想实现一个缩放功能,使用鼠标滚轮。此外,我还想添加一个功能,当您放大时,您可以通过单击并拖动放大的画布进行平移。要指定左右平移,请考虑单击并拖动以在放大的地图周围移动,如谷歌地图 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Eve

所以我有一个Java程序,它使用网格袋布局来创建单元格(如图形纸),如果鼠标在其中一个单元格上,那么单元格将改变其颜色。我想实现一个缩放功能,使用鼠标滚轮。此外,我还想添加一个功能,当您放大时,您可以通过单击并拖动放大的画布进行平移。要指定左右平移,请考虑单击并拖动以在放大的地图周围移动,如谷歌地图

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class Testing {

    public int RowI = 10;
    public int ColI = 10;

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            for (int row = 0; row < RowI; row++) {
                for (int col = 0; col < ColI; col++) {
                    gbc.gridx = col;
                    gbc.gridy = row;

                    CellPane cellPane = new CellPane();
                    Border border = null;
                    if (row < 4) { 
                        if (col < 4) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                        }
                    } else {
                        if (col < 4) {
                            border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }
                    cellPane.setBorder(border);
                    add(cellPane, gbc);
                }
            }
        }
    }

    public class CellPane extends JPanel { //The CellPane class changes the color of an individual cell based on whether or no the mouse I on a cell.

        private Color defaultBackground; //This is a private color that is only used by the mouseListener.

        public CellPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) { //If mouse is on cell turn cell the given color(in this case green).
                    defaultBackground = getBackground();
                    setBackground(Color.GREEN);
                }

                @Override
                public void mouseExited(MouseEvent e) {  //If mouse is not on cell revert to default background.
                    setBackground(defaultBackground);
                }
            });
        }

        @Override
        public Dimension getPreferredSize() { 
            return new Dimension(50, 50); //Cell size on x and y axis.
        }
    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.border.border;
导入javax.swing.border.MatteBorder;
公共类测试{
公共int RowI=10;
公众指数=10;
公共静态void main(字符串[]args){
新测试();
}
公共测试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
公共测试窗格(){
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
对于(int row=0;row

可能会回答您所寻找的。

可能会回答您所寻找的。

好吧,根据您的示例,我能想到的简单解决方案之一是简单地“缩放”单元窗格,从而更改组件的
首选大小。将其与
JScollPane
相结合,您基本上就可以满足您的需求了。同时,也可以检查该方法。根据您的示例,在我能想到的简单解决方案中,可以简单地“缩放”
单元格窗格
,从而更改组件的
首选大小
。将其与一个
JScollPane
结合,您基本上就有了自己的需求,也可以查看方法。