当前位置: 技术问答>java相关
菜鸟问题:为什么我的这些代码通过了编译却运行不了?
来源: 互联网 发布时间:2015-07-31
本文导语: 我想让Frame 的大小能随每次导入的图片的大小一样,但下面的方法不行,为什么? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyImage { public static void main(String[] args) { ImageFrame fra...
我想让Frame 的大小能随每次导入的图片的大小一样,但下面的方法不行,为什么?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyImage
{
public static void main(String[] args)
{
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with an image panel
*/
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("ImageTest");
setSize(WIDTH, HEIGHT);
// add panel to frame
ImagePanel panel = new ImagePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
/**
A panel that displays a tiled image
*/
//////////////////////////
class ImagePanel extends JPanel//内部类
{
public ImagePanel()
{
// acquire the image
image = Toolkit.getDefaultToolkit().getImage
("5.jpg");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
try { tracker.waitForID(0); }
catch (InterruptedException exception) {}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
private Image image;
public int WIDTH = image.getWidth(this);
public int HEIGHT = image.getHeight(this);
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyImage
{
public static void main(String[] args)
{
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with an image panel
*/
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("ImageTest");
setSize(WIDTH, HEIGHT);
// add panel to frame
ImagePanel panel = new ImagePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
/**
A panel that displays a tiled image
*/
//////////////////////////
class ImagePanel extends JPanel//内部类
{
public ImagePanel()
{
// acquire the image
image = Toolkit.getDefaultToolkit().getImage
("5.jpg");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
try { tracker.waitForID(0); }
catch (InterruptedException exception) {}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
private Image image;
public int WIDTH = image.getWidth(this);
public int HEIGHT = image.getHeight(this);
}
|
ImageIcon image = new ImageIcon("5.gif");
...
ImagePanel panel = new ImagePanel(new BorderLayout(),image);
...
class ImagePanel extends JPanel {
private ImageIcon bg;
public ImagePanel(LayoutManager layout,ImageIcon icon) {
super(layout);
bg = icon;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = bg.getImage();
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
...
ImagePanel panel = new ImagePanel(new BorderLayout(),image);
...
class ImagePanel extends JPanel {
private ImageIcon bg;
public ImagePanel(LayoutManager layout,ImageIcon icon) {
super(layout);
bg = icon;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = bg.getImage();
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}