本题的功能是监听对于颜色的复制和粘贴。程序窗l:1中,有一个颜色设置框和两个按钮,名为“复制”和“粘贴”,在颜色设置框中设置颜色后,下面的预览面板将显示选中的颜色,单击“复制”按钮后,将设置的颜色复制到系统的剪贴板上,然后继续选择其他颜色,当单击按钮“粘贴”后预览面板的颜色将设置为刚才复制的颜色。importjava.io.*;import java.awt.*;import java.awt.datatransfer.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;public class java3{public static void main(String[]args){JFrame. frame=new SerialTransferFrame():frame.setDefaultCloseoperation(JFrame.EXITON_CLOSE);frame.show();}}class SerialTransferFrame. extends JFrame{public SerialTransferFrame(){setTitle("java3"):Container contentPane = getContentPane();chooser=new JColorChooser();contentPane.add(chooser,BorderLayout.CEN-TER);JPanel panel=new JPanel();JButton copyButton=new JButton("复制");panel.add(copyButton);copyButton.addActionListener(newActionListener(){public void actionPerformed(ActionEvent event){copy();}});JButton pasteButton=new JButton("粘贴");panel.add(pasteButton);pasteButton.addAetionListener(newActionListener(){public void aetionPerformed(AetionEvent event){paste();}});contentPane.add(panel,BorderLayout.SOUTH);pack();}private void copy(){Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClip-board();int color=chooser.getColor();SerialSeleetion selection=new SerialSelection(color);clipboard.setContents(selection,null);}private void paste(){Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClip-board();Transferable contents=clipboard.getContents(null);if(contents= =null)return;try{DataFlavor flavor=new DataFlavor("application/x-java-serialized-object;class=java.awt.Color");if(contents.isDataFlavorSupported(flavor)){Color color=(Color)contents.getTransferData(flavor);;}}catch(ClassNotFoundException exception){JOptionPane.showMessageDialog(this,excep-tion);}catch(UnsupportedFlavorException exception){JOptionPane.showMessageDialog(this,excep-tion);}catch(IOException exception){JOptionPane.showMessageDialog(this,excep-tion);}}private JColorChooser chooser;}class SerialSelection implements Transferable{SerialSelection(Serializable o){obj=o:}public DataFlavor[]getTransferDataFlavors(){DataFlavor[]flavors=new DataFlavor[2];Class type=obj.getClass();String mimeType="application/x-java-serialized-object;class="+type.getName();try{flavors[0]=new DataFlavor(mimeType);flavors[1]=DataFlavor.stringFlavor;return flavors;}catch(ClassNotFoundExeeption exception){return new DataFlavor[O];}}public boolean isDataFlavorSupported(DataFlavorflavor){ReturnDataFlavor.stringFlavor.equals(flavor)‖"appli-cation".equals(flavor.getPrimaryType())&;&;"x-iava-serialized-object".equals(flavor.getSubType())&;&;flavor.getRepresentationClass().isAssign-ableFrom(obj.getClass());}public void getTransferData(DataFlavor flavor)throws UnsupportedFlavorException{if(! isDataFlavorSupported(flavor))throw new UnsupportedFlavorException(flaVor);if(DataFlavor.stringFlavor.equals(flavor))return obj.toString();return obj;}private Serializable obj;}
本题的功能是监听对于颜色的复制和粘贴。程序窗l:1中,有一个颜色设置框和两个按钮,名为“复制”和“粘贴”,在颜色设置框中设置颜色后,下面的预览面板将显示选中的颜色,单击“复制”按钮后,将设置的颜色复制到系统的剪贴板上,然后继续选择其他颜色,当单击按钮“粘贴”后预览面板的颜色将设置为刚才复制的颜色。
importjava.io.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class java3
{
public static void main(String[]args)
{
JFrame. frame=new SerialTransferFrame():
frame.setDefaultCloseoperation(JFrame.EXIT
ON_CLOSE);
frame.show();
}
}
class SerialTransferFrame. extends JFrame
{
public SerialTransferFrame()
{
setTitle("java3"):
Container contentPane = getContentPane();
chooser=new JColorChooser();
contentPane.add(chooser,BorderLayout.CEN-
TER);
JPanel panel=new JPanel();
JButton copyButton=new JButton("复制");
panel.add(copyButton);
copyButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
copy();
}
});
JButton pasteButton=new JButton("粘贴");
panel.add(pasteButton);
pasteButton.addAetionListener(new
ActionListener()
{
public void aetionPerformed(AetionEvent event)
{
paste();
}
});
contentPane.add(panel,BorderLayout.SOUTH);
pack();
}
private void copy()
{
Clipboard clipboard
=Toolkit.getDefaultToolkit().getSystemClip-
board();
int color=chooser.getColor();
SerialSeleetion selection=new SerialSelection
(color);
clipboard.setContents(selection,null);
}
private void paste()
{
Clipboard clipboard
=Toolkit.getDefaultToolkit().getSystemClip-
board();
Transferable contents=clipboard.getContents
(null);
if(contents= =null)return;
try
{
DataFlavor flavor=new DataFlavor(
"application/x-java-serialized-object;class=
java.awt.Color");
if(contents.isDataFlavorSupported(flavor))
{
Color color
=(Color)contents.getTransferData(flavor);
;
}
}
catch(ClassNotFoundException exception)
{
JOptionPane.showMessageDialog(this,excep-
tion);
}
catch(UnsupportedFlavorException exception)
{
JOptionPane.showMessageDialog(this,excep-
tion);
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(this,excep-
tion);
}
}
private JColorChooser chooser;
}
class SerialSelection implements Transferable
{
SerialSelection(Serializable o)
{
obj=o:
}
public DataFlavor[]getTransferDataFlavors()
{
DataFlavor[]flavors=new DataFlavor[2];
Class type=obj.getClass();
String mimeType
="application/x-java-serialized-object;class
="
+type.getName();
try
{
flavors[0]=new DataFlavor(mimeType);
flavors[1]=DataFlavor.stringFlavor;
return flavors;
}
catch(ClassNotFoundExeeption exception)
{
return new DataFlavor[O];
}
}
public boolean isDataFlavorSupported(DataFlavor
flavor)
{
Return
DataFlavor.stringFlavor.equals(flavor)‖"appli-
cation".equals(flavor.getPrimaryType())
&;&;"x-iava-serialized-object".equals(flavor.
getSubType())
&;&;flavor.getRepresentationClass().isAssign-
ableFrom(obj.getClass());
}
public void getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException
{
if(! isDataFlavorSupported(flavor))
throw new UnsupportedFlavorException(flaVor);
if(DataFlavor.stringFlavor.equals(flavor))
return obj.toString();
return obj;
}
private Serializable obj;
}