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

📄 tij0151.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0150.html">Prev</a> | <a href="tij0152.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Introduction
to Swing
<A NAME="fnB60" HREF="#fn60">[60]</A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
I suggest placing the superscript after Swing in &#8220;Introduction to
Swing.&#8221;After working your way through this chapter and seeing the huge
changes that have occurred within the AWT (although, if you can remember back
that far, Sun claimed Java was a &#8220;stable&#8221; language when it first
appeared), you might still have the feeling that it&#8217;s not quite done.
Sure, there&#8217;s now a good event model, and JavaBeans is an excellent
component-reuse design. But the GUI components still seem rather minimal,
primitive, and awkward.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">That&#8217;s
where <A NAME="Index2274"></A>Swing
comes in. The Swing library appeared after Java 1.1 so you might naturally
assume that it&#8217;s part of <A NAME="Index2275"></A>Java
1.2. However, it is designed to work with <A NAME="Index2276"></A>Java
1.1 as an add-on. This way, you don&#8217;t have to wait for your platform to
support Java 1.2 in order to enjoy a good UI component library. Your users
might actually need to download the Swing library if it isn&#8217;t part of
their Java 1.1 support, and this could cause a few snags. But it works.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Swing
contains all the components that you&#8217;ve been missing throughout the rest
of this chapter: those you expect to see in a modern UI, everything from
buttons that contain pictures to trees and grids. It&#8217;s a big library, but
it&#8217;s designed to have appropriate complexity for the task at hand &#8211;
if something is simple, you don&#8217;t have to write much code but as you try
to do more your code becomes increasingly complex. This means an easy entry
point, but you&#8217;ve got the power if you need it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Swing
has great depth. This section does not attempt to be comprehensive, but instead
introduces the power and simplicity of Swing to get you started using the
library. Please be aware that what you see here is intended to be simple. If
you need to do more, then Swing can probably give you what you want if
you&#8217;re willing to do the research by hunting through the online
documentation from Sun.
</FONT><a name="_Toc408018725"></a><P></DIV>
<A NAME="Heading462"></A><H3 ALIGN=LEFT>
Benefits
of Swing
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you begin to use the Swing library, you&#8217;ll see that it&#8217;s a huge
step forward. Swing components are Beans (and thus use the Java 1.1 event
model), so they can be used in any development environment that supports Beans.
Swing provides a full set of UI components. For speed, all the <A NAME="Index2277"></A>components
are lightweight (no &#8220;peer&#8221; components are used), and Swing is
written entirely in Java for portability.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Much
of what you&#8217;ll like about Swing could be called &#8220;orthogonality of
use;&#8221; that is, once you pick up the general ideas about the library you
can apply them everywhere. Primarily because of the Beans naming conventions,
much of the time I was writing these examples I could guess at the method names
and get it right the first time, without looking anything up. This is certainly
the hallmark of a good library design. In addition, you can generally plug
components into other components and things will work correctly.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index2278"></A><A NAME="Index2279"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Keyboard
navigation is automatic &#8211; you can use a Swing application without the
mouse, but you don&#8217;t have to do any extra programming (the old AWT
required some ugly code to achieve keyboard navigation). Scrolling support is
effortless &#8211; you simply wrap your component in a <A NAME="Index2280"></A><A NAME="Index2281"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JScrollPane</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as you add it to your form. Other features such as tool tips typically require
a single line of code to implement.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Swing
also supports something called &#8220;pluggable look and feel,&#8221; which
means that the appearance of the UI can be dynamically changed to suit the
expectations of users working under different platforms and operating systems.
It&#8217;s even possible to invent your own look and feel.
</FONT><a name="_Toc408018726"></a><P></DIV>
<A NAME="Heading463"></A><H3 ALIGN=LEFT>
Easy
conversion
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you&#8217;ve struggled long and hard to build your UI using Java 1.1, you
don&#8217;t want to throw it away to convert to Swing. Fortunately, the library
is designed to allow easy conversion &#8211; in many cases you can simply put a
&#8216;J&#8217; in front of the class names of each of your old AWT components.
Here&#8217;s an example that should have a familiar flavor to it:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: JButtonDemo.java</font>
<font color="#009900">// Looks like Java 1.1 but with J's added</font>
<font color="#0000ff">package</font> c13.swing;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">import</font> com.sun.java.swing.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> JButtonDemo <font color="#0000ff">extends</font> Applet {
  JButton 
    b1 = <font color="#0000ff">new</font> JButton("JButton 1"),
    b2 = <font color="#0000ff">new</font> JButton("JButton 2");
  JTextField t = <font color="#0000ff">new</font> JTextField(20);
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
    ActionListener al = <font color="#0000ff">new</font> ActionListener() {
      <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e){
        String name = 
          ((JButton)e.getSource()).getText();
        t.setText(name + " Pressed");
      }
    };
    b1.addActionListener(al);
    add(b1);
    b2.addActionListener(al);
    add(b2);
    add(t);
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String args[]) {
    JButtonDemo applet = <font color="#0000ff">new</font> JButtonDemo();
    JFrame frame = <font color="#0000ff">new</font> JFrame("TextAreaNew");
    frame.addWindowListener(<font color="#0000ff">new</font> WindowAdapter() {
      <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
    frame.getContentPane().add(
      applet, BorderLayout.CENTER);
    frame.setSize(300,100);
    applet.init();
    applet.start();
    frame.setVisible(<font color="#0000ff">true</font>);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There&#8217;s
a new 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement, but everything else looks like the Java 1.1 AWT with the addition of
some J&#8217;s. Also, you don&#8217;t just 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">something
to a Swing 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JFrame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but you must get the &#8220;content pane&#8221; first, as seen above.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">But
you can easily get many of the benefits of Swing with a simple conversion.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Because
of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement, you&#8217;ll have to invoke this program by saying:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">java
c13.swing.JbuttonDemo
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
of the programs in this section will require a similar form to run them.
</FONT><a name="_Toc408018727"></a><P></DIV>
<A NAME="Heading464"></A><H3 ALIGN=LEFT>
A
display framework
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
the programs that are both applets and applications can be valuable, if used
everywhere they become distracting and waste paper. Instead, a display
framework will be used for the Swing examples in the rest of this section:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Show.java</font>
<font color="#009900">// Tool for displaying Swing demos</font>
<font color="#0000ff">package</font> c13.swing;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> com.sun.java.swing.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> Show {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> 
  inFrame(JPanel jp, <font color="#0000ff">int</font> width, <font color="#0000ff">int</font> height) {
    String title = jp.getClass().toString();
    <font color="#009900">// Remove the word "class":</font>
    <font color="#0000ff">if</font>(title.indexOf("<font color="#0000ff">class</font>") != -1)
      title = title.substring(6);
    JFrame frame = <font color="#0000ff">new</font> JFrame(title);
    frame.addWindowListener(<font color="#0000ff">new</font> WindowAdapter() {
      <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
    frame.getContentPane().add(
      jp, BorderLayout.CENTER);
    frame.setSize(width, height);
    frame.setVisible(<font color="#0000ff">true</font>);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Classes
that want to display themselves should inherit from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JPanel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and then add any visual components to themselves. Finally, they create a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
containing the line:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Show.inFrame(new
MyClass(), 500, 300);
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">in
which the last two arguments are the display width and height.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that the title for the <A NAME="Index2282"></A><A NAME="Index2283"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JFrame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">

⌨️ 快捷键说明

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