?? loadimagedemo.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoadImageDemo extends JFrame implements ActionListener
{
private ImagePanel imagePanel; //繪制圖像的面板
private JButton button;
public LoadImageDemo()
{
super("getImage");
setSize(400, 400);
//獲取內容面板
Container container = getContentPane();
//創建用于繪制圖像的面板
imagePanel = new ImagePanel();
container.add(imagePanel, BorderLayout.CENTER);
//創建按鈕
button = new JButton("下幀圖像");
//設置字體
button.setFont(new Font("Serif", Font.PLAIN, 14));
//注冊監聽器
button.addActionListener(this);
container.add(button, BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
imagePanel.showNext();
}
public static void main(String[] args)
{
LoadImageDemo application = new LoadImageDemo();
}
class ImagePanel extends JPanel
{
private ImageIcon images;
public ImagePanel()
{
super();
setBackground(Color.WHITE);
//載入圖像
// images= new ImageIcon("boy01.png");
images= new ImageIcon("Image.jpg");
}
public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
int x = (this.getWidth() - images.getIconWidth())/2;
int y = (this.getHeight() - images.getIconHeight())/2;
images.paintIcon(this, g, x, y);
}
//該方法顯示下一幀圖像
public void showNext()
{
repaint();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -