网格布局删除JPanels之间填充 [英] GridLayout Removing Padding between JPanels

查看:290
本文介绍了网格布局删除JPanels之间填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的计算机科学项目,我一直没能找到任何解决我的问题。我创建通过网​​格布局 的JP​​anel 取值包含图像的二维数组。我想,使其无缝流入单个图像删除所有面板之间的填充/利润率。然而,似乎是setHGap和setVGap方法不帮助我。我倒是AP preciate任何回应。谢谢你。

 进口java.awt.GridLayout中;
进口的java.util.Map;
进口javax.swing.ImageIcon中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JPanel中;公共类MapArray {    静态的JPanel [] []瓦片=新JPanel [11] [11];    公共MapArray(){
    }    保护静态的ImageIcon createImageIcon(字符串路径){
        的java.net.URL imgUrl的= Map.class.getResource(路径);
        如果(imgUrl的!= NULL){
            返回新的ImageIcon(imgUrl的);
        }其他{
            通信System.err.println(找不到文件:+路径);
            返回null;
        }
    }    公共静态无效的主要(字串[] args){
        javax.swing.SwingUtilities.invokeLater(新的Runnable(){            @覆盖
            公共无效的run(){
                JFrame的帧=新的JFrame();
                frame.setSize(325,300);
                GridLayout的布局=新的网格布局(10,10);
                frame.setLayout(布局);                对(INT I = 0; I&小于10;我++){
                    为(中间体J = 0; J&小于10; J ++){
                        瓷砖[I] [J] =新JPanel();
                        瓷砖[I] [J]。新增(新的JLabel(
                            createImageIcon(瓦1.png)));
                        frame.add(瓷砖[I] [J]);
                    }
                }                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(真);
                frame.validate();
                frame.repaint();
            }
        });
    }
}


解决方案

的几个注意事项:


  • 给每个面板网​​格布局


  • 使用数组长度for循环的限制。


  • 重系数时,常量可能。


code:

 进口java.awt.GridLayout中;
进口javax.swing.ImageIcon中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JPanel中;公共类MapArray {    私有静态最终诠释SIZE = 4;
    私有静态最后的JPanel [] [] =瓷砖新JPanel [SIZE] [SIZE]    公共静态无效的主要(字串[] args){
        javax.swing.SwingUtilities.invokeLater(新的Runnable(){            @覆盖
            公共无效的run(){
                JFrame的帧=新的JFrame();
                frame.setLayout(新网格布局(大小,SIZE));
                的for(int i = 0; I< tiles.length;我++){
                    对于(INT J = 0; J<瓷砖[0]。长度; J ++){
                        瓷砖[I] [J] =新JPanel(新的GridLayout());
                        瓷砖[I] [J]。新增(新的JLabel(
                            新的ImageIcon(Image.gif的)));
                        frame.add(瓷砖[I] [J]);
                    }
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(真);
            }
        });
    }
}

I'm working on a Computer Science project, and I haven't been able to find any solution to my problem. I'm creating a 2D array of JPanels containing images through a GridLayout. I want to remove the padding/margins between all the panels so that it flows seemlessly into a single image. However, the setHGap and setVGap methods don't seem to help me. I'd appreciate any response. Thank you.

import java.awt.GridLayout;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapArray {

    static JPanel[][] tiles = new JPanel[11][11];

    public MapArray() {
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Map.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setSize(325, 300);
                GridLayout layout = new GridLayout(10, 10);
                frame.setLayout(layout);

                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++) {
                        tiles[i][j] = new JPanel();
                        tiles[i][j].add(new JLabel(
                            createImageIcon("tile-1.png")));
                        frame.add(tiles[i][j]);
                    }
                }

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.validate();
                frame.repaint();
            }
        });
    }
}

解决方案

A few notes:

  • Give each panel a GridLayout.

  • Use the array length for loop limits.

  • Re-factor constants when possible.

Code:

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapArray {

    private static final int SIZE = 4;
    private static final JPanel[][] tiles = new JPanel[SIZE][SIZE];

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(SIZE, SIZE));
                for (int i = 0; i < tiles.length; i++) {
                    for (int j = 0; j < tiles[0].length; j++) {
                        tiles[i][j] = new JPanel(new GridLayout());
                        tiles[i][j].add(new JLabel(
                            new ImageIcon("image.gif")));
                        frame.add(tiles[i][j]);
                    }
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

这篇关于网格布局删除JPanels之间填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆