简答题:本题的功能是获得系统剪贴板中的内容。窗口中有一个菜单“Edit”和一个文本域,“Edit”中有菜单项“Cut”、“Cop

  • 题目分类:Java
  • 题目类型:简答题
  • 查看权限:VIP
题目内容:
本题的功能是获得系统剪贴板中的内容。窗口中有一个菜单“Edit”和一个文本域,“Edit”中有菜单项“Cut”、“Copy”和“Paste”,在文本域中输入内容,可以通过菜单进行剪切、复制和粘贴操作,如果系统剪贴板为空,又做粘贴操作的话,则设置文本域中背景颜色为红色,并显示错误信息。
import Java.awt.*;
importjava.io.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
class java3 extends Frame implements ActionListener,
ClipboardOwner{
TextArea textArea=new TextArea();
java3(){
super("java3");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MenuBar mb=new MenuBar();
Menu m=new Menu("Edit");
setLayout(new BorderLayout());
add("Center",textArea);
m.add("Cut");
m.add("Copy");
m.add("Paste");
mb.add(m);
setMenuBar(this)
for(int i=0;i<m.gethemCount();i++){
m.itern(i).addActionListener(this)
}
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent evt){
if("Paste".equals(evt.getActionCommand())){
boolean error=true;
Transferable t=
getToolkit().getSystemClipboard().getContents
(this);
try{
if(t! =null&&t.isDataFlavorSupported(Dat-
aFlavor.stringFlavor)){
textArea.setBackground(Color.white);
textArea.setForeground(Color.black);
textArea.replaceRange(
(String)t.getTransferData(DataFlavor.stringFla-
vor),
textArea.getSelectionStart(),
textArea.getSelectionEnd());
error=false;
}
}catch(UnsupportedFlavorException e){
}catch(IOException e){
}
if(error){
textArea.setBackground(Color.red);
textArea.setForeground(Color.white);
textArea.repaint();
textArea.setText("ERROR:\nEither the clip-
board"+"is empty or the contents is not fl string.");
}
}else if("Copy".equals(evt.getActionCommand
())){
setContents();
}else if("Cut".equals(evt.getActionCommand
())){
setContents();
textArea.replaceRange("",textArea.getSelec-
tionStart(),textArea.getSelectionEnd());
}
}
void setContents(){
S=textArea.getSelectedText()
St ringSelection contents=new StringSelection
(s);
getToolkit().getSystemClipboard().setContents
(contents,this);
}
public void lostOwnership(Clipboard clipboard,
Transferable contents){
System.out.println("lost ownership");
}
public static void main(String args[]){
new java3();
}
}
参考答案:
答案解析:

本题将数组中arr[]={5,6,3,7,9,1}的各个元素按下标的逆序输出。 public class javal{p

本题将数组中arr[]={5,6,3,7,9,1}的各个元素按下标的逆序输出。 public class javal{public static void

查看答案

47. The words "chocolate snobs" in Paragraph 3 probably ref

47. The words "chocolate snobs" in Paragraph 3 probably refer to people who A.a

查看答案

45. Why is Chris Lee able to develop his idea of the CRF?

45. Why is Chris Lee able to develop his idea of the CRF?A.He knows the import

查看答案

42. Who joined the "Fringe" after it appeared?

42. Who joined the "Fringe" after it appeared?A.they owned a public house there

查看答案

22.

22. A.cars B.people C.street numbers D.traffic lights

查看答案