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

📄 tij0149.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<A NAME="Index2154"></A><A NAME="Index2155"></A>printing
support. Some of the publicity seemed to claim that you&#8217;d be able to
print from within an applet. However, the Java security system contains a
feature that could lock out an applet from initiating its own print job,
requiring that the initiation be done via a Web browser or applet viewer. At
the time of this writing, this seemed to remain an unresolved issue. When I ran
this program from within a Web browser, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintDemo</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
window came up just fine, but it wouldn&#8217;t print from the browser.
</FONT><a name="_Toc408018716"></a><P></DIV>
<A NAME="Heading453"></A><H3 ALIGN=LEFT>
The
clipboard
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.1<A NAME="Index2156"></A>
supports limited operations with the <A NAME="Index2157"></A><A NAME="Index2158"></A><A NAME="Index2159"></A>system
clipboard (in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.awt.datatransfer
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">package).
You can copy 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects to the clipboard as text, and you can paste text from the clipboard into 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. Of course, the clipboard is designed to hold any type of data, but how
this data is represented on the clipboard is up to the program doing the
cutting and pasting. Although it currently supports only string data, the Java
clipboard API provides for extensibility through the concept of a
&#8220;flavor.&#8221; When data comes off the clipboard, it has an associated
set of <A NAME="Index2160"></A><A NAME="Index2161"></A>flavors
that it can be converted to (for example, a graph might be represented as a
string of numbers or as an image) and you can see if that particular clipboard
data supports the flavor you&#8217;re interested in.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following program is a simple demonstration of cut, copy, and paste with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data in a <A NAME="Index2162"></A><A NAME="Index2163"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextArea</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
One thing you&#8217;ll notice is that the keyboard sequences you normally use
for cutting, copying, and pasting also work. But if you look at any 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextField</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextArea</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in any other program you&#8217;ll find that they also automatically support the
clipboard key sequences. This example simply adds programmatic control of the
clipboard, and you could use these techniques if you want to capture clipboard
text into some non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextComponent</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: CutAndPaste.java</font>
<font color="#009900">// Using the clipboard from Java 1.1</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> java.awt.datatransfer.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> CutAndPaste <font color="#0000ff">extends</font> Frame {
  MenuBar mb = <font color="#0000ff">new</font> MenuBar();
  Menu edit = <font color="#0000ff">new</font> Menu("Edit");
  MenuItem
    cut = <font color="#0000ff">new</font> MenuItem("Cut"),
    copy = <font color="#0000ff">new</font> MenuItem("Copy"),
    paste = <font color="#0000ff">new</font> MenuItem("Paste");
  TextArea text = <font color="#0000ff">new</font> TextArea(20,20);
  Clipboard clipbd = 
    getToolkit().getSystemClipboard();
  <font color="#0000ff">public</font> CutAndPaste() {
    cut.addActionListener(<font color="#0000ff">new</font> CutL());
    copy.addActionListener(<font color="#0000ff">new</font> CopyL());
    paste.addActionListener(<font color="#0000ff">new</font> PasteL());
    edit.add(cut);
    edit.add(copy);
    edit.add(paste);
    mb.add(edit);
    setMenuBar(mb);
    add(text, BorderLayout.CENTER);
  }
  <font color="#0000ff">class</font> CopyL <font color="#0000ff">implements</font> ActionListener {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      String selection = text.getSelectedText();
      StringSelection clipString = 
        <font color="#0000ff">new</font> StringSelection(selection);
      clipbd.setContents(clipString, clipString);
    }
  }
  <font color="#0000ff">class</font> CutL <font color="#0000ff">implements</font> ActionListener {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      String selection = text.getSelectedText();
      StringSelection clipString = 
        <font color="#0000ff">new</font> StringSelection(selection);
      clipbd.setContents(clipString, clipString);
      text.replaceRange("", 
        text.getSelectionStart(), 
        text.getSelectionEnd());
    }
  }
  <font color="#0000ff">class</font> PasteL <font color="#0000ff">implements</font> ActionListener {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      Transferable clipData =
        clipbd.getContents(CutAndPaste.<font color="#0000ff">this</font>);
      <font color="#0000ff">try</font> {
        String clipString = 
          (String)clipData.
            getTransferData(
              DataFlavor.stringFlavor);
        text.replaceRange(clipString, 
          text.getSelectionStart(), 
          text.getSelectionEnd());
      } <font color="#0000ff">catch</font>(Exception ex) {
        System.out.println("not String flavor");
      }
    }
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    CutAndPaste cp = <font color="#0000ff">new</font> CutAndPaste();
    cp.addWindowListener(
      <font color="#0000ff">new</font> WindowAdapter() {
        <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    cp.setSize(300,200);
    cp.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">The
creation and addition of the menu and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextArea</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
should by now seem a pedestrian activity. What&#8217;s different is the
creation of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Clipboard</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
field 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clipbd</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is done through the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Toolkit</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
the action takes place in the listeners. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CopyL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CutL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
listeners are the same except for the last line of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CutL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which erases the line that&#8217;s been copied. The special two lines are the
creation of a <A NAME="Index2164"></A><A NAME="Index2165"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringSelection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object from the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the call to <A NAME="Index2166"></A><A NAME="Index2167"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setContents(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with this 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringSelection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
That&#8217;s all there is to putting a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
on the clipboard.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PasteL,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data is pulled off the clipboard using <A NAME="Index2168"></A><A NAME="Index2169"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getContents(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
What comes back is a fairly anonymous <A NAME="Index2170"></A><A NAME="Index2171"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Transferable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object, and you don&#8217;t really know what it contains. One way to find out
is to call <A NAME="Index2172"></A><A NAME="Index2173"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getTransferDataFlavors(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which returns an array of <A NAME="Index2174"></A><A NAME="Index2175"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataFlavor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects indicating which flavors are supported by this particular object. You
can also ask it directly with <A NAME="Index2176"></A><A NAME="Index2177"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>isDataFlavorSupported(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
passing in the flavor you&#8217;re interested in. Here, however, the bold
approach is taken: <A NAME="Index2178"></A><A NAME="Index2179"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getTransferData(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is called assuming that the contents supports the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
flavor, and if it doesn&#8217;t the problem is sorted out in the exception
handler.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the future you can expect more data flavors to be supported.
</FONT><a name="_Toc408018717"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0148.html">Prev</a> | <a href="tij0150.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

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