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

时间:2016-03-31 00:34:40

标签: java image jpanel grid-layout japplet

GridLayout(2,2)上使用JPanel显示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);

1 个答案:

答案 0 :(得分:1)

只需将图片放入JLabel s。

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建议

这是一个非常简单的Applet,它显示来自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();
        }
    }

}