如何在JPanel 上使用GridLayout 显示图片?

How to display images using GridLayout on JPanel?

JPanel 上使用 GridLayout(2,2) 显示 4 张图像的最合适方式是什么?

问题解决!!我就是这样做的。它可能效率不高,但它易于阅读并且有效:) 请随时告诉我如何改进!我一直在寻找改进编码方式的方法!

//      Create panel and set layout
        pFlag= new JPanel();
        pFlag.setLayout(new GridLayout(2,2,10,10)); 

//      Get image       
        flag1Img = getImage(getCodeBase(), "croatia.png");
        flag2Img = getImage(getCodeBase(), "eng.png");
        flag3Img = getImage(getCodeBase(), "romania.png");
        flag4Img = getImage(getCodeBase(), "spain.png");        

//      Set as icon     
        flag1Icon = new ImageIcon(flag1Img);
        flag2Icon = new ImageIcon(flag2Img);
        flag3Icon = new ImageIcon(flag3Img);
        flag4Icon = new ImageIcon(flag4Img);

//      Create JLabel       
        flag1Label = new JLabel();
        flag2Label = new JLabel();
        flag3Label = new JLabel();
        flag4Label = new JLabel();

//      Set JLabel alignment        
        flag1Label.setHorizontalAlignment(JLabel.CENTER);
        flag1Label.setVerticalAlignment(JLabel.CENTER);
        flag2Label.setHorizontalAlignment(JLabel.CENTER);
        flag2Label.setVerticalAlignment(JLabel.CENTER);
        flag3Label.setHorizontalAlignment(JLabel.CENTER);
        flag3Label.setVerticalAlignment(JLabel.CENTER);
        flag4Label.setHorizontalAlignment(JLabel.CENTER);
        flag4Label.setVerticalAlignment(JLabel.CENTER);     

//      Set JLabels as icons
        flag1Label.setIcon(flag1Icon);
        flag2Label.setIcon(flag2Icon);
        flag3Label.setIcon(flag3Icon);
        flag4Label.setIcon(flag4Icon);

//      Assign icons to images
        pFlag.add(flag1Label);
        pFlag.add(flag2Label);
        pFlag.add(flag3Label);
        pFlag.add(flag4Label);

        con.add(pFlag);

只需将您的图像放入 JLabels。

JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new GridLayout(2, 2));
frame.setContentPane(panel);

frame.setVisible(true);
JLabel label1 = new JLabel();
panel.add(label1);
JLabel label2 = new JLabel();
panel.add(label2);
JLabel label3 = new JLabel();
panel.add(label3);
JLabel label4 = new JLabel();
panel.add(label4);

try {
    BufferedImage myPicture = ImageIO.read(new File("test.jpg"));

    label1.setIcon(new ImageIcon(myPicture));
    label2.setIcon(new ImageIcon(myPicture));
    label3.setIcon(new ImageIcon(myPicture));
    label4.setIcon(new ImageIcon(myPicture));
} catch (Exception e) {
    e.printStackTrace();
}

frame.pack();
frame.setMinimumSize(frame.getPreferredSize());

更新 根据 Andrew Thompson 的建议更新

这是一个非常简单的小程序,可显示 URL 中的 4 张图像。

public class Main extends JApplet {

    public void paint(Graphics g) {
        JPanel panel = new JPanel(new GridLayout(2, 2));
        add(panel);

        JLabel label1 = new JLabel();
        panel.add(label1);
        JLabel label2 = new JLabel();
        panel.add(label2);
        JLabel label3 = new JLabel();
        panel.add(label3);
        JLabel label4 = new JLabel();
        panel.add(label4);

        try {
            URL url = new URL("YOU_IMAGE_URL.jpg");
            Image myPicture = getImage(url);

            label1.setIcon(new ImageIcon(myPicture));
            label2.setIcon(new ImageIcon(myPicture));
            label3.setIcon(new ImageIcon(myPicture));
            label4.setIcon(new ImageIcon(myPicture));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}