Java 将滚动条添加到jframe网格

Java 将滚动条添加到jframe网格,java,swing,graphics,awt,Java,Swing,Graphics,Awt,我有一个矩形网格,我将用值填充它。但是,根据输入的不同,网格可能相当大,因此我想在图像中添加一个滚动条选项。下面的代码似乎不符合我的要求?感谢您的帮助 class Cube extends JComponent { public void paint(Graphics g) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) {

我有一个矩形网格,我将用值填充它。但是,根据输入的不同,网格可能相当大,因此我想在图像中添加一个滚动条选项。下面的代码似乎不符合我的要求?感谢您的帮助

   class Cube extends JComponent 
   {
public void paint(Graphics g) 
{   

    for (int i = 0; i < 10; i++) 
    {
        for (int j = 0; j < 10; j++) 
        {
            g.setColor(Color.GRAY);
            g.fillRect(i*40, j*40, 40, 40);
        }
    }

    for (int i = 0; i < 50; i++) 
    {
        for (int j = 0; j < 50; j++) 
        {
            g.setColor(Color.BLACK);
            g.drawRect(i*40, j*40, 40, 40);
        }
    }

}
public static void main(String[] a) 
{
    // CREATE SCROLLBAR
    JScrollPane scroller = new JScrollPane();
    JFrame window = new JFrame();
    window.setSize(200,200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(new Cube());
    //ADD THE SCROLLBAR 
    window.getContentPane().add(scroller, BorderLayout.CENTER);
    window.setVisible(true);
}
类多维数据集扩展JComponent
{
公共空间涂料(图g)
{   
对于(int i=0;i<10;i++)
{
对于(int j=0;j<10;j++)
{
g、 setColor(颜色为灰色);
g、 fillRect(i*40,j*40,40,40);
}
}
对于(int i=0;i<50;i++)
{
对于(int j=0;j<50;j++)
{
g、 设置颜色(颜色为黑色);
g、 drawRect(i*40,j*40,40,40);
}
}
}
公共静态void main(字符串[]a)
{
//创建滚动条
JScrollPane scroller=新的JScrollPane();
JFrame窗口=新JFrame();
设置窗口大小(200200);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(新多维数据集());
//添加滚动条
window.getContentPane().add(滚动条,BorderLayout.CENTER);
window.setVisible(true);
}
}一些提示:

  • 您需要将多维数据集添加到滚动窗格。你可能会觉得有用
  • 我重写你的程序如下

    class Cube extends JComponent
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    g.setColor(Color.GRAY);
                    g.fillRect(i*40, j*40, 40, 40);
                }
            }
    
            for (int i = 0; i < 50; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    g.setColor(Color.BLACK);
                    g.drawRect(i*40, j*40, 40, 40);
                }
            }   
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 1000);
        }
    
        public static void main(String[] a){
            // use event dispatcher thread
            EventQueue.invokeLater(
                new Runnable() {
                    public void run() {
                        Cube cube = new Cube();
                        JScrollPane scroller = new JScrollPane(cube);
                        JFrame window = new JFrame();
                        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        // set the content pane
                        window.setContentPane(scroller);
                        window.pack();
                        window.setVisible(true);
                    }
            });
        }
    }
    
    类多维数据集扩展JComponent
    {
    公共组件(图形g)
    {
    超级组件(g);
    对于(int i=0;i<10;i++)
    {
    对于(int j=0;j<10;j++)
    {
    g、 setColor(颜色为灰色);
    g、 fillRect(i*40,j*40,40,40);
    }
    }
    对于(int i=0;i<50;i++)
    {
    对于(int j=0;j<50;j++)
    {
    g、 设置颜色(颜色为黑色);
    g、 drawRect(i*40,j*40,40,40);
    }
    }   
    }
    @凌驾
    公共维度getPreferredSize(){
    返回新维度(10001000);
    }
    公共静态void main(字符串[]a){
    //使用事件调度程序线程
    EventQueue.invokeLater(
    新的Runnable(){
    公开募捐{
    多维数据集=新多维数据集();
    JScrollPane scroller=新的JScrollPane(多维数据集);
    JFrame窗口=新JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //设置内容窗格
    setContentPane(滚动条);
    window.pack();
    window.setVisible(true);
    }
    });
    }
    }
    
    JComponent有paintComponent,第一行代码应该是super.paintComponent,在类多维数据集中重写getPreferredSize,然后删除多维数据集。setPreferredSize(新维度(10001000));,并在window.setVisible(true)之前调用JFrame.pack();