Java 图片显示
packagegui;importjava.awt.*;importjavax.swing.*;publicclassImageShow{publicstaticvoid...
package gui;
import java.awt.*;
import javax.swing.*;
public class ImageShow {
public static void main(String[] args){
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show()
}
}
class ImagePanel extends JPanel {
private Image image;
public ImagePanel(){
image = Toolkit.getDefaultToolkit().getImage("1.jpg");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 1);
try{
tracker.waitForID(1);
}
catch(Exception e){
e.printStackTrace();
}
}
public void paintComponent (Graphics g){
super.paintComponent(g);
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
int FrameWidth = getWidth();
int FrameHeight = getHeight();
int x =(FrameWidth - imageWidth)/2;
int y = (FrameHeight - imageHeight)/2;
g.drawImage(image,x,y,null);
}
}
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("ImageShow");
setSize(WIDTH,HEIGHT);
ImagePanel panel = new ImagePanel();
Container contentPance = getContentPane();
contentPance.add(panel);
}
public final static int WIDTH =300;
public static final int HEIGHT =200;
}
分号是我少写了
主要是图片绘不出来
对 貌似路径不对
但我不知道路径怎么写 展开
import java.awt.*;
import javax.swing.*;
public class ImageShow {
public static void main(String[] args){
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show()
}
}
class ImagePanel extends JPanel {
private Image image;
public ImagePanel(){
image = Toolkit.getDefaultToolkit().getImage("1.jpg");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 1);
try{
tracker.waitForID(1);
}
catch(Exception e){
e.printStackTrace();
}
}
public void paintComponent (Graphics g){
super.paintComponent(g);
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
int FrameWidth = getWidth();
int FrameHeight = getHeight();
int x =(FrameWidth - imageWidth)/2;
int y = (FrameHeight - imageHeight)/2;
g.drawImage(image,x,y,null);
}
}
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("ImageShow");
setSize(WIDTH,HEIGHT);
ImagePanel panel = new ImagePanel();
Container contentPance = getContentPane();
contentPance.add(panel);
}
public final static int WIDTH =300;
public static final int HEIGHT =200;
}
分号是我少写了
主要是图片绘不出来
对 貌似路径不对
但我不知道路径怎么写 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询