问题描述
//Loading,displayingandscalinganimageinanappletimportjava.awt.BorderLayout;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.jnlp.FileContents;importjavax.jnlp.FileOpenService;importjavax.jnlp.ServiceManager;importjavax.swing.ImageIcon;importjavax.swing.JApplet;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassLoadImageAndScaleextendsJApplet{privateImageIconimage;//referencesimagetodisplayprivateJPanelscaleJPanel;//JPanelcontainingthescale-selectorprivateJLabelpercentJLabel;//labelforJTextFieldprivateJTextFieldscaleInputJTextField;//obtainsuser'sinputprivateJButtonscaleChangeJButton;//initiatesscalingofimageprivatedoublescaleValue=1.0;//scalepercentageforimage//loadimagewhenappletisloadedpublicvoidinit(){scaleJPanel=newJPanel();percentJLabel=newJLabel("scalepercent:");scaleInputJTextField=newJTextField("100");scaleChangeJButton=newJButton("SetScale");//addcomponentsandplacescaleJPanelinapplet'sNORTHregionscaleJPanel.add(percentJLabel);scaleJPanel.add(scaleInputJTextField);scaleJPanel.add(scaleChangeJButton);add(scaleJPanel,BorderLayout.NORTH);//registereventhandlerforscaleChangeJButtonscaleChangeJButton.addActionListener(newActionListener(){//whentheJButtonispressed,setscaleValueandrepaintpublicvoidactionPerformed(ActionEvente){scaleValue=Double.parseDouble(scaleInputJTextField.getText())/100.0;repaint();//causesimagetoberedisplayedatnewscale}//endmethodactionPerformed}//endanonymousinnerclass);//endcalltoaddActionListener//useJNLPservicestoopenanimagefilethattheuserselectstry{//getareferencetotheFileOpenServiceFileOpenServicefileOpenService=(FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService");//getfile'scontentsfromtheFileOpenServiceFileContentscontents=fileOpenService.openFileDialog("C:",null);//bytearraytostoreimage'sdatabyte[]imageData=newbyte[(int)contents.getLength()];contents.getInputStream().read(imageData);//readimagebytesimage=newImageIcon(imageData);//createtheimage//ifimagesuccessfullyloaded,createandaddDrawJPaneladd(newDrawJPanel(),BorderLayout.CENTER);}//endtrycatch(Exceptione){e.printStackTrace();}//endcatch}//endmethodinit//DrawJPanelusedtodisplayloadedimageprivateclassDrawJPanelextendsJPanel{//displayimagepublicvoidpaintComponent(Graphicsg){super.paintComponent(g);//thefollowingvaluesareusedtocentertheimagedoublespareWidth=getWidth()-scaleValue*image.getIconHeight();doublespareHeight=getHeight()-scaleValue*image.getIconHeight();//drawimagewithscaledwidthandheightg.drawImage(image.getImage(),(int)(spareWidth)/2,(int)(spareHeight)/2,(int)(image.getIconWidth()*scaleValue),(int)(image.getIconHeight()*scaleValue),this);}//endmethodpaint}//endclassDrawJPanel}//endclassLoadImageAndScale----------------------------javax.jnlp.UnavailableServiceException:uninitializedatjavax.jnlp.ServiceManager.lookup(UnknownSource)atLoadImageAndScale.init(LoadImageAndScale.java:61)atsun.applet.AppletPanel.run(UnknownSource)atjava.lang.Thread.run(UnknownSource)-------------------------程序没有达到预期的效果。只是出现了一个对话框,但没有出现让我选择图片的open对话框。