gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:08:01 25 4
gpt4 key购买 nike

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

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

//      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);

最佳答案

只需将您的图像放入 JLabel 中即可。

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());

更新按照安德鲁·汤普森的建议进行了更新

这是一个非常简单的 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();
}
}

}

关于java - 如何在JPanel上使用GridLayout显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36322193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com