JAVA中怎样获取文件夹里的图片 并把图片在Panel里显示出来
4个回答
展开全部
这是第一个程序文件:
import java.awt.*;
import javax.swing.*;
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null),
img.getHeight(null));
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img,0,0,null);
}
}
这是第二个:
import java.awt.*;
import javax.swing.*;
public class ImageTest {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new
ImageIcon("images/01.jpg").getImage());
JFrame frame = new JFrame("多问自己,会使你少犯错误!");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null),
img.getHeight(null));
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img,0,0,null);
}
}
这是第二个:
import java.awt.*;
import javax.swing.*;
public class ImageTest {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new
ImageIcon("images/01.jpg").getImage());
JFrame frame = new JFrame("多问自己,会使你少犯错误!");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
展开全部
////////////////////////////
class PicPanel extends JPanel {
String name = "burger_cheese.jpg";
Image img;
public PicPanel(){
img = Toolkit.getDefaultToolkit().getImage(name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img , 0);
try {
tracker.waitForID(0);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
class PicPanel extends JPanel {
String name = "burger_cheese.jpg";
Image img;
public PicPanel(){
img = Toolkit.getDefaultToolkit().getImage(name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img , 0);
try {
tracker.waitForID(0);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
擦、手机不好写阿。没有合适的答案给我留言。包你懂
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1L已经说了,我也没什么可说的
用Label也行
JLabel label=new JLabel(new ImageIcon("图片名"));
panel.add(label);
用Label也行
JLabel label=new JLabel(new ImageIcon("图片名"));
panel.add(label);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询