⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 java2 图形设计 卷II Swing
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame {
	static private JWindow window;

	public Test() {
		super("Animated Icon in a Window");

		Container contentPane = getContentPane();
		JPanel panel = new ImagePanel();
		JButton button = new JButton("Buffalo ...");

		contentPane.setLayout(new FlowLayout());
		contentPane.add(button);

		button.addActionListener(new ActionListener() {
			private Container wcp;

			public void actionPerformed(ActionEvent e) {
				if(window == null) {
					Point loc = getLocation();

					window = new JWindow();
					wcp = window.getContentPane();

					wcp.add(new ImagePanel());
					window.pack();
					window.setLocation(55, 10);
				}
				window.setVisible(true);
			}
		});
	}
	public static void main(String[] args) {
		JFrame f = new Test();
		f.setBounds(300, 300, 250, 100);
		f.setVisible(true);

		f.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e) {
				window.dispose();
				System.exit(0);
			}
		});
	}
}
class ImagePanel extends JPanel {
	ImageIcon icon = new ImageIcon("buffalo.gif");

	public ImagePanel() {
		setBorder(BorderFactory.createLineBorder(Color.black));
	}
	public void paintComponent(Graphics g) {
		Insets insets = getInsets();

		super.paintComponent(g);
		icon.paintIcon(this, g, insets.left, insets.top);
	}
	public Dimension getPreferredSize() {
		Insets insets = getInsets();

		return new Dimension(
			   icon.getIconWidth() + insets.left + insets.right, 
		       icon.getIconHeight() + insets.top + insets.bottom);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -