📄 s06.htm
字号:
<SCRIPT LANGUAGE="JavaScript" SRC="/-fs0/sys/pop-up.js"></SCRIPT><SCRIPT LANGUAGE="JavaScript" SRC="/-fs0/sys/pop-up-all.js"></SCRIPT><html><head><title>易都网--Java 2 图形设计卷Ⅱ:SWING</title><LINK rel="stylesheet" href="../../../_public/javaa.css"><meta http-equiv="Content-Type" content="text/html; charset=GBK"><script language="JavaScript" src="../../../_public/javaa.js"></script><meta name="keywords" content="Java,JSP,ASP,PHP,J2EE,EJB,JavaScript,C/C++,ASM,CSS,HTML,XML,网络安全,MySQL,ACCESS"></head><body bgcolor="#FFFFFF"><table border=0 cellpadding=0 cellspacing=0 width="100%"> <tbody> <script language="javascript">print2()</script> <tr> <td width="100%"> <table bgcolor=#EEEEEE border=0 cellpadding=3 cellspacing=0 width="100%"> <tbody> <tr> <td class=f1 id=thetd width="100%"> <p>[<a href="index.html" target="_self">目录</a>][<a href="s05.htm">上一页</a>][<a href="s07.htm">下一页</a>]</p> <p align="center"><b>第6章 实用工具</b></p> <p> Swing包括许多实用工具,本章将介绍这些实用工具。其中有些实用工具(如计时器和由SwingUtilties类提供的static方法)在Swing内部使用,而进度监视器和进度监视器流等其他的实用工具则不是内容使用的。使用Swing的开发人员可以使用本章介绍的所有实用工具。</p> <p><b>6.1 计时器</b></p> <p align="center"><b>例6-1 使用Swing计时器</b><br> </p> <hr noshade size="1"> <p>import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*;</p> <p>public class Test implements ActionListener {<br> private int seconds=1;</p> <p> public Test() {<br> Timer oneSecondTimer = new Timer(1000, this);<br> Timer timerWithInitialDelay = new Timer(2000, <br> new TimerWithDelayListener());<br> Timer oneTimeTimer = new Timer(10000, <br> new OneTimeListener());</p> <p> timerWithInitialDelay.setInitialDelay(5000);<br> oneTimeTimer.setRepeats(false);</p> <p> oneSecondTimer.start();<br> timerWithInitialDelay.start();<br> oneTimeTimer.start();<br> }<br> public void actionPerformed(ActionEvent e) {<br> if(seconds == 0)<br> System.out.println("Time: " + seconds + " second");<br> else<br> System.out.println("Time: " + seconds + " seconds");</p> <p> seconds++;<br> }<br> public static void main(String args[]) {<br> new Test();<br> while(true);<br> }<br> }<br> class TimerWithDelayListener implements ActionListener { <br> public void actionPerformed(ActionEvent e) {<br> System.out.println("Timer with Delay Ringing");<br> }<br> }<br> class OneTimeListener implements ActionListener { <br> public void actionPerformed(ActionEvent e) {<br> System.out.println("One Time Timer Ringing");<br> }<br> } </p> <hr noshade size="1"> <p> </p><p align="center"><b>例6-2 重载构造计时器时所指定的延迟</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test implements ActionListener {<br> public Test() {<br> Timer oneSecondTimer = new Timer(1000, this);</p> <p> oneSecondTimer.setInitialDelay(10000);<br> oneSecondTimer.setRepeats(false);<br> oneSecondTimer.start();<br> }<br> public void actionPerformed(ActionEvent e) {<br> System.out.println("ring ...");<br> }<br> public static void main(String args[]) {<br> new Test();<br> while(true);<br> }<br> }<br> </p> <hr noshade size="1"> <p> </p><p align="center"><b>例6-3 计时器日志</b></p> <hr noshade size="1"> <br> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test implements ActionListener {<br> public Test() {<br> Timer.setLogTimers(true);</p> <p> Timer oneSecondTimer = new MyTimer(1000, this);<br> oneSecondTimer.start();<br> }<br> public void actionPerformed(ActionEvent e) {<br> System.out.println("ring ...");<br> }<br> public static void main(String args[]) {<br> new Test();<br> while(true);<br> }<br> }<br> class MyTimer extends Timer {<br> public MyTimer(int delay, ActionListener listener) {<br> super(delay, listener);<br> }<br> public String toString() {<br> return "MyTimer";<br> }<br> }</p> <hr noshade size="1"> <p> </p> <p align="center"><b>例6-4 与单个计时器相关联的多个动作监听器</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test implements ActionListener {<br> private int seconds=1;</p> <p> public Test() {<br> Timer oneSecondTimer = new Timer(1000, this);</p> <p> oneSecondTimer.addActionListener(new SecondListener());<br> oneSecondTimer.addActionListener(new ThirdListener());<br> oneSecondTimer.start();<br> }<br> public void actionPerformed(ActionEvent e) {<br> if(seconds == 0)<br> System.out.println("Time: " + seconds + " second");<br> else<br> System.out.println("Time: " + seconds + " seconds");<br> seconds++;<br> }<br> public static void main(String args[]) {<br> new Test();<br> while(true);<br> }<br> }<br> class SecondListener implements ActionListener { <br> public void actionPerformed(ActionEvent e) {<br> System.out.println("Second Listener");<br> }<br> }<br> class ThirdListener implements ActionListener { <br> public void actionPerformed(ActionEvent e) {<br> System.out.println("Third Listener");<br> }<br> }<br> </p> <hr noshade size="1"> <p></p> <p> </p> <p align="center"><b>例6-5 合并计时器事件</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test implements ActionListener {<br> private boolean firstRing = true;<br> private int ring = 1;</p> <p> public Test() {<br> Timer.setLogTimers(true);</p> <p> Timer oneSecondTimer = new Timer(1000, this);</p> <p> // comment out the following line for colaescing<br> oneSecondTimer.setCoalesce(false);</p> <p> System.out.println("Timer is coalescing: " + <br> oneSecondTimer.isCoalesce());</p> <p> oneSecondTimer.start();<br> }<br> public void actionPerformed(ActionEvent e) {<br> System.out.println("ring #" + ring++);</p> <p> if(firstRing) {<br> // simulate a time consuming operation by sleeping<br> // for 10 seconds ...<br> try {<br> Thread.currentThread().sleep(10000);<br> }<br> catch(InterruptedException ex) {<br> ex.printStackTrace();<br> }<br> firstRing = false;<br> }<br> }<br> public static void main(String args[]) {<br> new Test();<br> while(true);<br> }<br> }<br> </p> <hr noshade size="1"> <p></p> <p></p> <p> </p> <p> 6.2 事件监听器列表</p> <p> </p> <p> 6.3 Swing实用工具</p> <p> </p> <p align="center"><b>例6-6 计算两个矩形之间的差集、交集和并集</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test extends JApplet {<br> Rectangle r1 = new Rectangle(20,20,150,75);<br> Rectangle r2 = new Rectangle(100,40,100,150);<br> Rectangle destination;</p> <p> public Test() {<br> destination = new Rectangle(r2);</p> <p> // print out the intersection of r1 and r2 ...</p> <p> System.out.println("Intersection: " + <br> SwingUtilities.computeIntersection(r1.x,r1.y,<br> r1.width,r1.height,destination));<br> System.out.println();</p> <p> // print out the union of r1 and r2 ...</p> <p> System.out.println("Union: " + <br> SwingUtilities.computeUnion(r1.x,r1.y,<br> r1.width,r1.height,destination));<br> System.out.println();</p> <p> // print out the difference of r1 and r2 ...</p> <p> Rectangle[] difference =<br> SwingUtilities.computeDifference(r1, r2);</p> <p> System.out.println("Difference:");</p> <p> for(int i=0; i < difference.length; ++i) {<br> System.out.println(difference[i]);<br> }<br> }<br> public void paint(Graphics g) {<br> g.setColor(Color.red); <br> g.fillRect(r1.x, r1.y, r1.width, r1.height);</p> <p> g.setColor(Color.yellow); <br> g.fillRect(r2.x, r2.y, r2.width, r2.height);<br> }<br> }</p> <hr noshade size="1"> <p><br> <applet code="Test.class" archive="s06_tu02.jar" width=600 height=550></applet><br> 图6-2 转换坐标系统</p> <p align="center"><b>例6-7 转换鼠标坐标</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test extends JApplet {<br> private Point lastScreenPt = null;<br> private final Container contentPane = getContentPane();<br> private PanelWithString <br> outer = new PanelWithString(Color.orange),<br> inner = new PanelWithString(Color.red),<br> innermost = new PanelWithString(Color.yellow);</p> <p> public Test() {<br> Font font = new Font("Times-Roman", Font.ITALIC, 26);</p> <p> contentPane.setLayout(new OverlayLayout(contentPane));<br> contentPane.add(innermost);<br> contentPane.add(inner);<br> contentPane.add(outer);</p> <p> innermost.setMaximumSize(new Dimension(350,50));<br> inner.setMaximumSize(new Dimension(450,200));<br> outer.setMaximumSize(new Dimension(550,400));</p> <p> setFont(font);<br> innermost.setFont(font);<br> inner.setFont(font);<br> outer.setFont(font);</p> <p> contentPane.addMouseMotionListener(<br> new MouseMotionAdapter() {<br> public void mouseMoved(MouseEvent e) {<br> Point pt = e.getPoint();</p> <p> outer.setString(SwingUtilities.convertPoint(<br> contentPane, pt, outer).toString());</p> <p> inner.setString(SwingUtilities.convertPoint(<br> contentPane, pt, inner).toString());</p> <p> innermost.setString(SwingUtilities.convertPoint(<br> contentPane, pt, innermost).toString());</p> <p> SwingUtilities.convertPointToScreen(<br> pt, contentPane);</p> <p> lastScreenPt = pt;<br> repaint();<br> }<br> });<br> }<br> public void paint(Graphics g) {<br> super.paint(g);</p> <p> if(lastScreenPt != null) {<br> String s = new String("Screen: " + lastScreenPt);</p> <p> g.setColor(getForeground());<br> g.drawString(s,10,g.getFontMetrics().getHeight());</p> <p> SwingUtilities.convertPointFromScreen(lastScreenPt,<br> contentPane);</p> <p> s = "Content Pane: " + lastScreenPt;</p> <p> g.drawString(s,10,g.getFontMetrics().getHeight()*2);<br> }<br> else {<br> g.setColor(getForeground());<br> g.drawString("MOVE THE MOUSE IN HERE",10,<br> g.getFontMetrics().getHeight());<br> }<br> }<br> }<br> class PanelWithString extends JPanel {<br> String s;<br> Color color;</p> <p> public PanelWithString(Color color) {<br> this.color = color;<br> }<br> public void setString(String s) {<br> this.s = s;<br> }<br> public void paintComponent(Graphics g) {<br> super.paintComponent(g);</p> <p> Dimension size = getSize();</p> <p> g.setColor(color);<br> g.fillRect(0,0,size.width,size.height);</p> <p> if(s != null) {<br> g.setColor(getForeground());<br> g.drawString(s,10,g.getFontMetrics().getHeight());<br> }<br> }<br> }</p> <hr noshade size="1"> <p align="center"> </p> <p></p> <p></p> <p></p> <p> 6.4 Swing常量</p> <p> </p> <p> 6.5 Borlayout和Box类</p> <p> </p> <p align="center"> </p> <p align="center"></p> <p> 6.5.1 BoxLayout类</p> <p> <applet code="Test.class" archive="s06_tu03.jar" width=400 height=250> </applet><br> 图6-3 两个使用BoxLayout的容器</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -