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

📄 s06.htm

📁 java图形设计卷2 swing
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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(&quot;Time: &quot; + seconds + &quot; second&quot;);<br>              else<br>              System.out.println(&quot;Time: &quot; + seconds + &quot; seconds&quot;);</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(&quot;Timer with Delay Ringing&quot;);<br>              }<br>              }<br>              class OneTimeListener implements ActionListener { <br>              public void actionPerformed(ActionEvent e) {<br>              System.out.println(&quot;One Time Timer Ringing&quot;);<br>              }<br>              } </p>            <hr noshade size="1">            <p>&nbsp;</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(&quot;ring ...&quot;);<br>              }<br>              public static void main(String args[]) {<br>              new Test();<br>              while(true);<br>              }<br>              }<br>            </p>            <hr noshade size="1">            <p>&nbsp;</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(&quot;ring ...&quot;);<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 &quot;MyTimer&quot;;<br>              }<br>              }</p>            <hr noshade size="1">            <p>&nbsp;</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(&quot;Time: &quot; + seconds + &quot; second&quot;);<br>              else<br>              System.out.println(&quot;Time: &quot; + seconds + &quot; seconds&quot;);<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(&quot;Second Listener&quot;);<br>              }<br>              }<br>              class ThirdListener implements ActionListener { <br>              public void actionPerformed(ActionEvent e) {<br>              System.out.println(&quot;Third Listener&quot;);<br>              }<br>              }<br>            </p>            <hr noshade size="1">            <p></p>            <p>&nbsp;</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(&quot;Timer is coalescing: &quot; + <br>              oneSecondTimer.isCoalesce());</p>            <p> oneSecondTimer.start();<br>              }<br>              public void actionPerformed(ActionEvent e) {<br>              System.out.println(&quot;ring #&quot; + 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>&nbsp; </p>            <p> 6.2 事件监听器列表</p>            <p>&nbsp;</p>            <p> 6.3 Swing实用工具</p>            <p>&nbsp;</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(&quot;Intersection: &quot; + <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(&quot;Union: &quot; + <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(&quot;Difference:&quot;);</p>            <p> for(int i=0; i &lt; 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(&quot;Times-Roman&quot;, 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(&quot;Screen: &quot; + lastScreenPt);</p>            <p> g.setColor(getForeground());<br>              g.drawString(s,10,g.getFontMetrics().getHeight());</p>            <p> SwingUtilities.convertPointFromScreen(lastScreenPt,<br>              contentPane);</p>            <p> s = &quot;Content Pane: &quot; + lastScreenPt;</p>            <p> g.drawString(s,10,g.getFontMetrics().getHeight()*2);<br>              }<br>              else {<br>              g.setColor(getForeground());<br>              g.drawString(&quot;MOVE THE MOUSE IN HERE&quot;,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">&nbsp; </p>            <p></p>            <p></p>            <p></p>            <p> 6.4 Swing常量</p>            <p>&nbsp;</p>            <p> 6.5 Borlayout和Box类</p>            <p>&nbsp;</p>            <p align="center">&nbsp; </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 + -