问答题 本题的功能是对列表项的操作,包括删除、添加和反选。窗司中有两个列表框和5个按钮,按钮标签代表着移除列表项的方向,“>”代表只移除选中的列表项,“>>”代表移除所有的列表项,“!”代表反向选择列表项。 import java.awt.*; import java.awt.event.*; class java3 extendsFrame implements ActionListener&ItemListener{ final static int ITEMS=10; List ltList=new List(ITEMS,true); List rtList=new List(0,true); java3(){ super("java3"); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(O); } }); GridBagLayout gbl=new GridBagLayout(); setLayout(gbl); add(1tList,0,0,1,5,1.0,1.0); add(rtList。2,O,1,5,1.O,1.O); ltList.addActionListener(this); ltList.addhemListener(this); rtList.addActionListener(this); rtList.addhemListener(this); Button b; add(b=new Button(">"),1,O,1,1,O,1.0); b.addActionListener(this); add(b=new Button(">>"),1,1,1,1,0,1.O); b.addActionListener(this); add(b=new Button("<"),1,2,1,1,O,1.0); b.addActionListener(this); add(b=newButton("<<"),1,3,1,1,O,1.O); b.addActionListener(this); add(b=new Button("!"),1,4,1,1,0,1.O); b.addActionListener(this); for(int i=0;i<ITEMS;i++){ ltList.add("item"+i); } pack(); show(); } void add(Component comp, int X,int Y,int W,int h,double weightx,double weighty){ GridBagLayout gbl=(GridBagLayout)getLayout (); GridBagConstraints c=new GridBagConstraints (); e.fill=GridBagConstraints.BOTH; c.gridx=x; c.gridy=y; c.gridwidth=W; c.gridheight=h; c.weightx=weightx; c.weighty=weighty; add(comp); gbl.setConstraints(comp,c); } void reverseSelections(List l){ for(int i=0;i<1.length();i++){ if(1.islndexSelected(i)){ 1.deselect(i); }else{ 1.select(i); } } } void deseleetAll(List l){ for(int i=0;i<1.gethemCount();i++){ 1.deseleet(i); } } void replacehem(List l,String item){ for(int i=0;i<1.getltemCount();i++){ if(1.gethem(i).equals(item)){ 1.replacehem(item+"*",i); } } } void move(List ll,List l2,boolean all){ if(a11){ for(int i=0;i<11.getltemCount();i++){ 12.add(11.gethem(i)); } 11.removeAll(); }else{ String[]items=11.getSelectedhems(); int[]itemIndexes=11.getSelectedIndexes(); deselectAll(12); for(int i=0;i<items.length;i++){ 12.add(items[i]); 12.select(12.getItemCount()-1); if(i= =0){ 12.makeVisible(12.getltemCount()-1); } } for(int i=itemlndexes.length-1; i>=0;i--){ 11.remove(itemlndexes[i]); } } } public void actionPerformed(AetionEvent evt){ String ar9=evt.getActionCommand(); if(">".equals(arg)){ move(1tList,rtList,false); }else if(">>".equals(arg)){ move(1tList,rtList,true); }else if("<".equals(arg)){ move(rtList,ltList,false); }else if("<<".equals(arg)){ move(rtList,ltList,true); }else if("!".equals(arg)){ if(ltList.getSelectedhems().length>0){ reverseSelections(ltList); }else if(rtList.getSelectedhems().length> 0){ reverseSelections(rtList); } }else{ Object target=evt.getSouree(); if(target= =rtList‖target= =ltList){ replacehem((List)target,arg); } } } public void itemStatedChanged(ItemEvent ent){ List target=(List)evt.getSource(); if(target= =hList){ deselectAll(rtList); } else if(target= =rtList){ deselectAll(ltList); } } public static void main(String[]args){ new java3(); } }
判断题 教育法规是推行教育道德的有效手段,其对教育道德水平的提高有着积极作用。( )
问答题 本题的功能是获得系统剪贴板中的内容。窗口中有一个菜单“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(); } }