计算机类
公务员类
工程类
语言类
金融会计类
计算机类
医学类
研究生类
专业技术资格
职业技能资格
学历类
党建思政类
计算机等级考试(NCRE)
全国计算机应用水平考试(NIT)
计算机软件水平考试
计算机等级考试(NCRE)
全国高校计算机等级考试CCT
行业认证
信息素养
计算机等级考试二级
计算机等级考试一级
网络工程师(计算机等级考试四级)
计算机等级考试二级
数据库工程师(计算机等级考试四级)
计算机等级考试三级
信息安全工程师(计算机等级考试四级)
嵌入式系统开发工程师(计算机等级考试四级)
软件测试工程师(计算机等级考试四级)
Java语言程序设计
Python语言程序设计
WPS Office高级应用与设计
C语言程序设计
C++语言程序设计
Java语言程序设计
Visual Basic语言程序设计
Web程序设计
Access数据库程序设计
MySQL数据库程序设计
Visual FoxPro数据库程序设计
办公软件高级应用
问答题本程序的功能是获得系统剪贴板中的内容。窗口中有一个“Edit”菜单和一个文本域,“Edit”菜单中有“Cut”、“Copy”和“Paste”菜单项,在文本域中输入内容,可以通过菜单进行剪切、复制和粘贴操作,如果系统剪贴板为空,又做粘贴操作的话,则设置文本域中背景色为红色,并显示错误信息,如图所示。importjava.awt.*;importjava.io.*;importjava.awt.datatransfer.*;importjava.awt.event.*;classexam_75extendsFrameimplementsActionListener,ClipboardOwner{TextAreatextArea=newTextArea();exam_75(){super("exam_75");addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});MenuBarmb=newMenuBar();Menum=newMenu("Edit");setLayout(newBorderLayout());add("Center",textArea);m.add("Cut");m.add("Copy");m.add("Paste");mb.add(m);setMenuBar(this);for(inti=0;i<m.getItemCount();i++){m.item(i).addActionListener(this);}setSize(300,300);show();}publicvoidactionPerformed(ActionEventevt){if("Paste".equals(evt.getActionCommand())){booleanerror=true;Transferablet=getToolkit().getSystemClipboard().getContents(this);try{if(t!=nulltextArea.setForeground(Color.black);textArea.replaceRange((String)t.getTransferData(DataFlavor.stringFlavor),textArea.getSelectionStart(),textArea.getSelectionEnd());error=false;}}catch(UnsupportedFlavorExceptione){}catch(IOExceptione){}if(error){textArea.setBackground(Color.red);textArea.setForeground(Color.white);textArea.repaint();textArea.setText("ERROR:/nEithertheclipboard"+"isemptyorthecontentsisnotastring.");}}elseif("Copy".equals(evt.getActionCommand())){setContents();}elseif("Cut".equals(evt.getActionCommand())){setContents();textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());}}voidsetContents(){s=textArea.getSelectedText();StringSelectioncontents=newStringSelection(s);getToolkit().getSystemClipboard().setContents(contents,this);}publicvoidlostOwnership(Clipboardclipboard,Transferablecontents){System.out.println("lostownership");}publicstaticvoidmain(Stringargs[]){newexam_75();}}
进入题库练习
问答题如下程序在编译时出错,请改正程序中的错误,使之顺利通过编译和运行。 注意:不改动程序结构,不得增行或删行。 class SubClass extends BaseClass class BaseClass String str; public BaseClass() SyStem.out.println("good"); public BasecClass(String s) str=s; public class ConstructTest1 public static void main(String args[]) SubClass s=new SubClass("hi"); BaseClass b=new BaseClass("Java");
进入题库练习
问答题下面的程序是10000以内的“相亲数”。所谓相亲数是指这样的一对数:甲数的约数之和等于乙数,而乙数的约数等于甲数,(例如220和284是一对相亲数)请在程序的每条横线处填写一条语句,使程序的功能完整。 注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 public class QinShu public static void main(String args[ ]) for(int n=1;n<9999;n++) int s=divsum(n); if( ) System.out.println(n+","+s); public static int divsum(int n)//该方法的功能是求一个数的所有约数 int s=0; for(int i=1;____________________i++) if(____________________)s+=i; return s;
进入题库练习
问答题本程序的功能是用表格的形式列出当前目录所有的文件以及属性,包括是否是文件夹、文件名称、是否可读、是否可写、文件大小以及最后修改时间,如图所示。importjava.awt.*;importjavax.swing.*;importjava.util.Date;importjava.io.File;importjavax.swing.table.*;publicclassexam_42extendsJFrame{publicexam_42(){super("exam_42");setSize(500,400);setDefaultCloseOperation(EXIT_ON_CLOSE);FileModelfm=newFileModel();JTablejt=newJTable(fm);jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);jt.setColumnSelectionAllowed(true);jt.setDefaultRenderer(Number.class,newBigRenderer(1000));JScrollPanejsp=newJScrollPane(jt);getContentPane().add(jsp,BorderLayout.CENTER);}publicstaticvoidmain(Stringargs[]){exam_42ft=newexam_42();ft.setVisible(true);}}classBigRendererextendsDefaultTableCellRenderer{doublethreshold;publicBigRenderer(doublet){threshold=t;setHorizontalAlignment(JLabel.RIGHT);setHorizontalTextPosition(SwingConstants.RIGHT);}publicComponentgetTableCellRendererComponent(JTabletable,Objectvalue,booleanisSelected,booleanhasFocus,introw,intcol){returnsuper.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);}}classFileModelextendsAbstractTableModel{Stringtitles[]=newString[]{"目录?","文件名","可读?","可写?","大小","最后修改时间"};Classtypes=newClass[]{Boolean.class,String.class,Boolean.class,Boolean.class,Number.class,Date.class};Objectdata[][];publicFileModel(){this(".");}publicFileModel(Stringdir){Filepwd=newFile(dir);setFileStats(pwd);}publicintgetRowCount(){returndata.length;}publicintgetColumnCount(){returntitles.length;}publicStringgetColumnName(intc){returntitles[c];}publicClassgetColumnClass(intc){returntypes[c];}publicvoidgetValueAt(intr,intc){returndata[r][c];}publicvoidsetFileStats(Filedir){Stringfiles[]=dir.list;data=newObject[files.length][titles.length];for(inti=0;i<files.length;i++){Filetmp=newFile(files[i]);data[i][0]=newBoolean(tmp.isDirectory());data[i][1]=tmp.getName();data[i][2]=newBoolean(tmp.canRead());data[i][3]=newBoolean(tmp.canWrite());data[i][4]=newLong(tmp.length());data[i][5]=newDate(tmp.lastModified());}fireTableDataChanged();}}
进入题库练习
问答题下面是一个Applet程序,程序的功能是用鼠标画图。本题是通过单击鼠标后画出一个Java字。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意;不改动程序的结构,不得增行或减行。 源程序文件代码清单如下; import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code=SimpleHitMouse.java width=800 height=400> </applet> */ public class SimpleHitMouse implements Applet private java.util.Vector points=new java.util.Vector(); public void init() addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e) points.add(new Point(e.getX(),e.getY()) paint(); ); public void update(Graphics g) repaint(g); public void paint(Graphics g) for(int i=0;i<points.size();i++) Point dot=(Point)points.elementAt(i); g.drawString("x",dot.x,dot.y); public static void main(String args[ ]) Frame fram=new Frame("CelayTree"); SimpleHitMouse dot=new SimpleHitMouse(); dot.init (); dot.start (); fram.add ( "Center", dot ); fram.setSize( 400,300 ); fram.addWindowListener( new WindowAdapter() public void windowClosing( WindowEvent e ) System.exit(0); ); fram.show (); ex12_3.html: <html> <head> <title>A Simple Program</title> </head> <body> <applet code=" SimpleHitMouse.class" width=800 height=400> </applet> </body> </html>
进入题库练习
问答题本程序的功能是判断年份1990、2000和2008是否是闰年,并打印输出判断结果。 public class exam_2{ public static void main(String[] args){ yes_no(1990); yes_no(2000); yes_no(2008); } public______yes_no(int year){ if(______) System.out.print(year+"年是闰年。"); ______ System.out.print(year+"年不是闰年。"); } }
进入题库练习
问答题本程序的功能是监听窗口在屏幕中的位置。程序中有三个窗口,主窗口根据两个子窗口在屏幕中的位置及大小,并在主窗口中以矩形的方式绘制出来,当拖动两个子窗口时,主窗口中跟踪显示子窗口的位置,即窗口中的矩形随着子窗口的移动对应移动,如图所示。importjava.awt.*;importjava.awt.event.*;importjava.util.*;classexam_76extendsFrameextendsComponentListener{DimensionscreenSize;Vectorwatched=newVector();exam_76(){super("exam_76");addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});screenSize=Toolkit.getDefaultToolkit().getScreenSize();setSize(200,200);show();}publicvoidwatch(Componentsc){watched.addElement(c);c.addComponentListener(newComponentListener());}publicvoidpaint(Graphicsg){Insetsinsets=getInsets();intw=getSize().width-insets.left-insets.right;inth=getSize().height-insets.top-insets.bottom;for(inti=0;i<watched.size();i++){Componentc=(Component)watched.elementAt(i);Rectangler=c.getBounds();g.drawRect(r.x*w/screenSize.width+insets.left,r.y*h/screenSize.height+insets.top,r.width*w/screenSize.width,r.height*h/screenSize.height);}}publicvoidcomponentResized(ComponentEventevt){repaint();}publicvoidcomponentMoved(ComponentEventevt){repaint();}publicvoidcomponentShown(ComponentEventevt){repaint();}publicvoidcomponentHidden(ComponentEventevt){repaint();}publicstaticvoidmain(String[]args){exam_76m=newexam_76();Framef;m.watch(f=newFrame("移动我的位置"));f.setSize(100,100);f.show();m.watch(f=newFrame("移动我的位置"));f.setSize(50,50);f.show();}}
进入题库练习
问答题myC.printAll();
进入题库练习
问答题请完成下列Java程序:建立一个Applet程序,包括创建一个画布构件、一个面板构件,面板构件上包含3个按钮,用来设置画布和面板的背景颜色,这3个按钮(Red、Green、Blue)分别控制画布和面板背景色改变为3原色,即红、绿、蓝。要求画布宽度为300,高度为200。 注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 源程序文件代码清单如下: import java.awt.*; import java.applet.*; /* <applet code="ex10_2.class" width=800 height=400 > </applet> */ public class ex10_2 extends Applet private Panel pane110_2; private Canvas canvas10_2; private Button btn1, btn2, btn3; public void init() canvas10_2=new Canvas(); canvas10_2. ______; canvas10_2.setBackground(Color.black); add(canvas10_2); pane110_2 = new Panel(); pane110_2.setBackground( Color.black ); btn1 = new Button( "Red" ); btn2 = new Button( "Green" ); btn3 = new Button( "Blue" ); pane110_2.add( btn1 ); pane110_2.add( btn2 ); pane110_2.add( btn3 ); ______; public boolean action( Event e, Object o ) if( e.target == btn1 ) pane110_2.setBackground( Color.red ); canvas10_2.setBackground( Color.red ); else if( e.target == btn2 ) pane110_2.setBackground( Color.green ); canvas10_2.setBackground(Color.green); else if( e.target == btn3 ) canvas10_2.setBackground( Color.blue ); pane110_2.setBackground( Color.blue ); return true;
进入题库练习
问答题“角谷猜想”指出:将一个自然数按以下的一个简单规则进行运算:若数为偶数,则除以2;若为奇数,则乘3加1。将得到的数按该规则重复运算,最终可得1。请在下面程序得每条横线处填写一个语句,使程序的功能完整(如:输入34,则输出结果为34 17 52 26 13 40 20 10 5 16 8 4 2 1)。 注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 import java.io.*; class JiaoGu{ public static void main(String args[ ]) { System.out.print("/n请输入一个数"); try { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); Stirng s=br.readLine(); _____________________ while(a!=1) { System.out.print(" " +a); if(a%2==1) _______________________ else a=a/2; } System.out.println(" " +a); } ___________________{} } }
进入题库练习
问答题本题中定义了一个长度为20的整数数组,然后将1~20分别赋给数组元素,计算该数组中所有下标为奇数的元素的和。   public class javal{   public static void main(String args[]){   int sum;    ;   int arrayList[]=new int[20];   for(int i=0;i<=19;i++)   arrayList[i]=i+1;   int pos=0;   while(pos<20){   if( )   sum=sum+arrayList[pos];    ;   }   System.out.println("sum="+sum);   }   }
进入题库练习
问答题本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。 import java.awt.event.*; import java.awt.*; import javax.swing.JOptionPane; class Dwindow extends Frame implements ActionListener TextField inputNumber; TextArea show; Dwindow(String s) super(s); inputNumber=new TextField(22); inputNumber.addActionListener(this); show=new TextAtea(); add(inputNumber,BorderLayout.NORTH); add(show,BorderLayout.CENTER); setBounds(60,60,300,300);setVisible(true); validate(); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void aetionPerformed(ActionEvent e) boolean boo=false; if(e.getSource()==inputNumber) String s=______; char a[]=s.toCharArray(); for(int i=0;i<a.length;i++) if(!(Character.isDigit(a[i]))) boo=true; if(boo==true) JOptionPane.showMessageDialog(this,"您输入了非法字符","警告对话框", ______); inputNumber.setText(null); else if(boo==false) int number=Integer.parseInt(s); show.append("/n"+number+"平方:"+(number*number)); public class java2 public static void main(String args[]) new Dwindow("java2");
进入题库练习
问答题本程序的功能是监听窗口的状态,包括窗口为当前窗口、窗口不是当前窗口、窗口被图标化和窗口撤销图标化,并将这些信息显示在文本域中,如图所示。importjava.awt.*;importjava.awt.event.*;classMyFrameextendsFrame______{TextAreatext;MyFrame(Strings){super(s);setBounds(100,100,200,300);setVisible(true);text=newTextArea();add(text,BorderLayout.CENTER);______;validate();}publicvoidwindowActivated(WindowEvente){text.append("/n窗口被激活");validate();}publicvoidwindowDeactivated(WindowEvente){text.append("/n窗口不是激活状态");setBounds(0,0,400,400);validate();}publicvoidwindowClosing(WindowEvente){text.append("/n窗口正在关闭");dispose();}publicvoidwindowClosed(WindowEvente){System.exit(0);}publicvoidwindowIconified(WindowEvente){text.append("/n窗口被图标化");}publicvoidwindowDeiconified(WindowEvente){text.append("/n窗口撤消图标化");setBounds(0,0,400,400);validate();}publicvoidwindowOpened(WindowEvente){}}classexam_85{publicstaticvoidmain(Stringargs[]){newMyFrame("exam_85");}}
进入题库练习
问答题本程序的功能是计算圆和三角形的面积,通过“选择”菜单可以分别进行圆和三角形面积的计算,单击“圆面积计算”菜单项,窗口中就会显示两个文本框和一个“确定”按钮,在第一个文本框中输入圆的半径,单击“确定”按钮后就可以在第二个文本框中显示圆的面积。单击“三角形面积计算”菜单项,窗口中就会显示四个文本框和一个“确定”按钮,在前三个文本框中分别输入三角形三个边的长度,单击“确定”按钮后,如果三个边的长度不能组成三角形,则在结果文本框中给出提示信息,否则显示三角形的面积。如果输入的值不是数值,则给出提示信息,如图所示。importjava.awt.*;importjava.awt.event.*;classcircleextendsPanelimDlementsActionListener{doubler,area;TextFieldradius=null,result=null;Buttonb=null;voidcircle(){radius=newTextField(10);result=newTextField(10);b=newButton("确定");add(newLabel("输入半径"));add(radius);add(newLabel("面积是:"));add(result);add(b);b.addActionListener(this);result.setEnabled(false);}publicvoidactionPerformed(ActionEvente){try{r=Double.parseDouble(radius.getText());area=Math.PI*r*r;result.setText(""+area);catch(Exceptionee){radius.setText("请输入数学字符");}}}classtriangleextendsPanelimplementsActionListener{doublea=O,b=O,c=O,area;TextFieldbordera=newTextField(6),borderb=newTextField(6),borderc=newTextField(6),result=newTextField(24);Buttonbutton=newButton("确定");triangle(){add(newLabel("输入三边的长度:"));add(border_a);add(border_b);add(border_c);add(newLabel("面积是:"));add(result);add(button);button.addActionListener(this);result.setEnabled(false);}publicvoidactionPerformed(ActionEvente){try{a=Double.parseDouble(bordera.getText());b=Double.parseDouble(border_b.getText());c=Double.parseDouble(border_c.getText());if(a+b>carea=Math,sqrt(p*(p-a)*(p-b)*(p-c));result,setText(""+area);}else{result,setText("您输入的数字不能形成三角形");}}catch(Exceptionee){result,setText("请输入数字字符");}}}classWinextendsJFrameimplementsActionListener{MenuBarbar=null;Menumenu=null;MenuItemitem1,item2;circlecircle;triangletrangle;Win(){bar=newMenuBar();menu=newMenu("选择");setSize(300,200);item1=newMenuItem("圆面积计算");item2=newMenuItem("三角形面积计算");menu.add(item1);menu.add(item2);bar.add(menu);setMenuBar(bar);circle=newcircle();trangle=newtriangle();item1.addActionListener(this);item2.addActionListener(this);setVisible(true);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==item1){removeAll();add(circle,"Center");validate();}elseif(e.getSource()==item2){removeAll();add(trangle,"Center";validate();}}}publicclassexam_1{publicstaticvoidmain(Stringargs[]){Winwin=newWin();win.setTitle("exam_1");win.setBounds(100,100,700,300);win.setVisible(true);win.addWindowListener(newWindowListener(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}}
进入题库练习
问答题本题程序的功能是用复选框来控制鼠标右键的弹出菜单是否弹出。窗口中有一个复选框“弹出菜单”,勾选该复选框后,鼠标置于窗口上,右击会弹出一个菜单,单击菜单中的命令后,后台会输出所单击的菜单项。如果取消勾选该复选框,右击就不能弹出菜单。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; class CanvasWithPopup extends Canvas ______; CanvasWithPopup(PopupMenu popup) enableEvents(AWTEvent.MOUSE EVENT MASK); this.popup = popup; void addPopup() add(popup); void removePopup() remove(popup); protected void processMouseEvent(MouseEvent evt) if (popup.getParent() ! = null && evt.isPopupTrigger()) popup.show(evt.getComponent(),evt.getX(),evt.getY()); super.processMouseEvent(evt); public class advance extends Frame implements ItemListener,ActionListener Checkbox cb = new Checkbox("弹出菜单",false); CanvasWithPopup canvas; advance() super("advance"); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); add(cb,BorderLayout.NORTH); cb.addItemListener(this); PopupMenu popup = new PopupMenu("Button Control"); popup.add("item1"); popup.add("item2"); popup.addActionListener(this); canvas = new CanvasWithPopup(popup); add(canvas,BorderLayout.CENTER); setSize(100,200); show(); public void itemStateChanged(ItemEvent evt) ______ case ItemEvent.SELECTED: canvas.addPopup(); break; case ItemEvent.DESELECTED: canvas.removePopup(); break; public void actionPerformed(ActionEvent evt) ______; static public void main(String[] args) new advance ();
进入题库练习
问答题本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; public class java3 public static void main(String[]args) TextTestFrame frame=new TextTestFrame(); frame.setDefauhCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class TextTestFrame extends JFrame public TextTestFrame() setTitle("java3"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); Container contentPane=getContentPane(); DocumentListener listener=new DocurnentListener(); JPanel panel=new JPanel(); hourField=new JTextField("12",3); panel.add(hourField); hourField.getDocument().addDocumentListener(this); minuteField=new JTextField("00",3); panel.add(minuteField); minuteField.getDocument().addDocumentListener(listener); contentPane.add(panel,BorderLayout.SOUTH); clock=new ClockPanel(); contentPane.add(clock,BorderLayout.CENTER); public void setClock() try int hours =Integer.parseInt(hourField,getText().trim()); int minutes =Integer.parseInt(minuteField.getText().trim()); clock.setTime(hours,minutes); catch(NumberFormatException e) public static final int DEFAULT_WIDTH=300; public static final int DEFAULT_HEIGHT=300; private JTextField hourField; private JTextField minuteField; private ClockPanel clock; private class clockFieldListener extends DocumentListener public void insertUpdate(DocumentEvent e)setClock(); public void removeUpdate(DocumentEvent e)setClock(); public void changedUpdate(DocumentEvent e) class ClockPanel extends JPanel public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2=(Graphics2D)g; Ellipse2D circle =new Ellipse2D.Double(0,0,2*RADIUS,2*RADIUS); g2.draw(circle); double hourAngle =Math.toRadians(90-360*minutes/(12*60)); drawHand(g2.hourAngle,HOUR_HAND_LENGTH); double minuteAngle =Math.toRadians(90-360*minutes/60); drawHand(g2,minuteAngle,MINUTE_HAND_LENGTH); public void drawHand(Graphics2D g2, double angle,double handLength) Point2D end=new Poim2D.Double( RADIUS+handLength*Math.cos(angle), RADIUS-handLength*Math.sin(angle)); Point2D center=new Point2D.Double(RADIUS,RADIUS); g2.draw(new Line2D.Double(center,end)); public void setTime(int h,int m) minutes—h*60+m: repaint(); private double minutes=0; private double RADIUS=100; private double MINUTE_HAND_LENGTH=0.8*RADIUS; private double HOUR_HAND_LENGTH=0.6*RADIUS;
进入题库练习
问答题本程序生成类exam_74的一个对象temp,并调用该对象的方法opera。方法opera中计算整型变量a和双精度浮点型变量b的和,并打印输出结果,其中a为34,b为1。 public class exam_74{ public static void main(String[] args) { exam_74 temp=______; ______; } ______opera(){ int a=34; double b=1; System.out.println(a+b); } }
进入题库练习
问答题本程序的功能是计算二维数组arr中所有元素的和,并打印输出计算结果。 public class exam_97{ public static void main(String[] args) { int arr[][]={{34,21,45,67,20},{23,10,3,45},{22,3,79}}; int i=0,j=0; int sum=0; outer:while(true){ if(i>=arr.length) ______; while(true){ if(j>=arr[i].length){ ______; j=0; continue outer; } sum=sum+arr[i][j]; ______; } } System.out.println("sum="+sum); } }
进入题库练习
问答题本题是-个Applet,功能是用鼠标画不同颜色的图形。页面中有5个按钮“画红色图形”、“画绿色图形”、“画蓝色图形”、“橡皮”和“清除”,单击前三个按钮中的-个,按住鼠标左键或右键在面板中拖动,就能画出对应颜色的线条,单击“橡皮”按钮,按住鼠标左键或右键在面板中拖动就能将面板中的图形擦除掉,单击“清除”按钮,就能将面板中所有的图形清除掉。
进入题库练习
问答题本程序的功能是用复选框来控制鼠标右键的弹出菜单是否弹出。窗口中有一个“弹出菜单”复选框,如果选中该复选框,鼠标置于窗口上,单击鼠标右键会弹出一个菜单,单击菜单项中的项后,后台会输出所单击的菜单项,如果取消该复选框的选择,单击鼠标右键就不能弹出菜单,如图所示。importjava.awt.*;importjava.awt.event.*;classCanvasWithPopupextendsCanvas{Popupmenupopup;CanvasWithPopup(PopupMenupopup){enableEvents(AWTEvent.MOUSE_EVENT_MASK);this.popup=popup;}voidaddPopup(){add(popup);}voidremovePopup(){remove(popup);}protectedvoidprocessMouseEvent(MouseEventevt){if(popup.getParent()!=null}super.processMouseEvent(evt);}}publicclassexam_53extendsFrameimplementsItemListener,ActionListener{Checkboxcb=newCheckbox("弹出菜单",false);CanvasWithPopupcanvas;exam_53(){super("exam_53");addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});add(cb,BorderLayout.NORTH);cb.addItemListener(this);PopupMenupopup=newPopupMenu("ButtonControl");popup.add("item1");popup.add("item2");popup.addActionListener(this);canvas=newCanvasWithPopup(popup);add(canvas,BorderLayout.CENTER);setSize(100,200);show();}publicvoiditemStateChanged(ItemEventevt){switch(evt.getState()){caseItemEvent.SELECTED:canvas.addPopup();break;caseItemEvent.DESELECTED:canvas.removePopup();break;}}publicvoidactionPerformed(ActionEventevt){System.out.println(getActionCommand()+"isselected");}staticpublicvoidmain(String[]args){newexam_53();}}
进入题库练习