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

📄 test.java

📁 swing 教程,与大家分享一下,哈哈,希望大家多多指教
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
	private Point lastScreenPt = null;
	private final Container contentPane = getContentPane();
	private PanelWithString 
			outer = new PanelWithString(Color.orange),
			inner = new PanelWithString(Color.red),
			innermost = new PanelWithString(Color.yellow);

	public Test() {
		Font font = new Font("Times-Roman", Font.ITALIC, 26);

		contentPane.setLayout(new OverlayLayout(contentPane));
		contentPane.add(innermost);
		contentPane.add(inner);
		contentPane.add(outer);

		innermost.setMaximumSize(new Dimension(350,50));
		inner.setMaximumSize(new Dimension(450,200));
		outer.setMaximumSize(new Dimension(550,400));

		setFont(font);
		innermost.setFont(font);
		inner.setFont(font);
		outer.setFont(font);

		contentPane.addMouseMotionListener(
									new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				Point pt = e.getPoint();

				outer.setString(SwingUtilities.convertPoint(
				contentPane, pt, outer).toString());

				inner.setString(SwingUtilities.convertPoint(
				contentPane, pt, inner).toString());

				innermost.setString(SwingUtilities.convertPoint(
				contentPane, pt, innermost).toString());

				SwingUtilities.convertPointToScreen(
										pt, contentPane);

				lastScreenPt = pt;
				repaint();
			}
		});
	}
	public void paint(Graphics g) {
		super.paint(g);

		if(lastScreenPt != null) {
			String s = new String("Screen: " + lastScreenPt);

			g.setColor(getForeground());
		 	g.drawString(s,10,g.getFontMetrics().getHeight());

			SwingUtilities.convertPointFromScreen(lastScreenPt,
											contentPane);

			s = "Content Pane: " + lastScreenPt;

		 	g.drawString(s,10,g.getFontMetrics().getHeight()*2);
		}
		else {
			g.setColor(getForeground());
		 	g.drawString("MOVE THE MOUSE IN HERE",10,
							g.getFontMetrics().getHeight());
		}
	}
}
class PanelWithString extends JPanel {
	String s;
	Color color;

	public PanelWithString(Color color) {
		this.color = color;
	}
	public void setString(String s) {
		this.s = s;
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Dimension size = getSize();

		g.setColor(color);
		g.fillRect(0,0,size.width,size.height);

		if(s != null) {
			g.setColor(getForeground());
		 	g.drawString(s,10,g.getFontMetrics().getHeight());
		}
	}
}

⌨️ 快捷键说明

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