java随机图片显示
packagegame;importjava.awt.GridLayout;importjava.awt.Image;importjava.awt.Toolkit;imp...
package game;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
import javax.swing.*;
public class Picture extends JFrame {
private JLabel picture;
public Picture() {
int n = Integer.parseInt(JOptionPane.showInputDialog("input: "));
Image[] images = new Image[6];
String photopath = "";
for (int i = 1; i <= n; i++) {
Random p = new Random();
int s[] = new int[100];
s[i] = p.nextInt(2) + 0;
if (s[i] == s[i - 1])
i--;
else {
photopath = "/image/20145850320" + s[i] + ".jpg";
images[i] = Toolkit.getDefaultToolkit().createImage(photopath);
ImageIcon icon = new ImageIcon(images[i]);
new GridLayout(1, 4, 5, 5);
add(new JLabel(icon));
}
}
}
public static void main(String[] args) {
Picture frame = new Picture();
frame.setVisible(true);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
求大神看下代码 怎么输入一个数,然后随机输出对应数目的图片 展开
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
import javax.swing.*;
public class Picture extends JFrame {
private JLabel picture;
public Picture() {
int n = Integer.parseInt(JOptionPane.showInputDialog("input: "));
Image[] images = new Image[6];
String photopath = "";
for (int i = 1; i <= n; i++) {
Random p = new Random();
int s[] = new int[100];
s[i] = p.nextInt(2) + 0;
if (s[i] == s[i - 1])
i--;
else {
photopath = "/image/20145850320" + s[i] + ".jpg";
images[i] = Toolkit.getDefaultToolkit().createImage(photopath);
ImageIcon icon = new ImageIcon(images[i]);
new GridLayout(1, 4, 5, 5);
add(new JLabel(icon));
}
}
}
public static void main(String[] args) {
Picture frame = new Picture();
frame.setVisible(true);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
求大神看下代码 怎么输入一个数,然后随机输出对应数目的图片 展开
1个回答
展开全部
参考代码. 你可以对照修改
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
public class Picture extends JFrame {
private JLabel picture;
public Picture() {
ImageIcon[] icons = new ImageIcon[4];//四张图的icon对象
String photopath = "";
for (int i = 1; i <= 4; i++) {
//这里的目录是我的图片所在的目录 1.gif~4.gif
photopath = "src/images/" + i + ".gif";
Image img = Toolkit.getDefaultToolkit().createImage(photopath);
icons[i-1] = new ImageIcon(img);
}
picture = new JLabel();
JPanel jp = new JPanel();
jp.add(picture);
add(jp,BorderLayout.CENTER);
setBounds(500, 200, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
int n = Integer.parseInt(JOptionPane.showInputDialog("input: "));
//先设置Jlabel应该显示的图片
picture.setIcon(icons[n-1]);
//然后才开始显示窗口
this.setVisible(true);
}
public static void main(String[] args) {
new Picture();
}
}
追问
你好 我的想法是 输入一个数字 随机输出对应数目的图片 比如 输入3 然后输出3张图片
追答
public class Picture extends JFrame {
public Picture() {
ImageIcon[] icons = new ImageIcon[4];//四张图的icon对象
String photopath = "";
for (int i = 1; i <= 4; i++) {
//这里的目录是我的图片所在的目录 1.gif~4.gif
photopath = "src/images/" + i + ".gif";
Image img = Toolkit.getDefaultToolkit().createImage(photopath);
icons[i-1] = new ImageIcon(img);
}
JPanel jp = new JPanel();
add(jp);
setBounds(500, 200, 300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
int n = Integer.parseInt(JOptionPane.showInputDialog("input: "));
//添加N张图片
for (int i = 0; i < n; i++) {
jp.add(new JLabel(icons[i]));
}
//然后才开始显示窗口
this.setVisible(true);
}
public static void main(String[] args) {
new Picture();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询