javax.jnlp.UnavailableServiceException: uninitialized問題
//Loading, displaying and scaling an image in an applet
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.jnlp.FileContents;
import javax.jnlp.FileOpenService;
import javax.jnlp.ServiceManager;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LoadImageAndScale extends JApplet
{
private ImageIcon image;//references image to display
private JPanel scaleJPanel;//JPanel containing the scale-selector
private JLabel percentJLabel;//label for JTextField
private JTextField scaleInputJTextField;//obtains user's input
private JButton scaleChangeJButton;//initiates scaling of image
private double scaleValue = 1.0; //scale percentage for image
//load image when applet is loaded
public void init()
{
scaleJPanel = new JPanel();
percentJLabel = new JLabel("scale percent:");
scaleInputJTextField = new JTextField("100");
scaleChangeJButton= new JButton("Set Scale");
// add components and place scaleJPanel in applet's NORTH region
scaleJPanel.add(percentJLabel);
scaleJPanel.add(scaleInputJTextField);
scaleJPanel.add(scaleChangeJButton);
add(scaleJPanel,BorderLayout.NORTH);
//register event handler for scaleChangeJButton
scaleChangeJButton.addActionListener(
new ActionListener()
{
//when the JButton is pressed,set scaleValue and repaint
public void actionPerformed(ActionEvent e)
{
scaleValue = Double.parseDouble(
scaleInputJTextField.getText())/100.0;
repaint();//causes image to be redisplayed at new scale
}// end method actionPerformed
}//end anonymous inner class
);//end call to addActionListener
//use JNLP services to open an image file that the user selects
try
{
//get a reference to the FileOpenService
FileOpenService fileOpenService =
(FileOpenService) ServiceManager.lookup(
"javax.jnlp.FileOpenService");
//get file's contents from the FileOpenService
FileContents contents =
fileOpenService.openFileDialog(null, null);
//byte array to store image's data
byte[] imageData = new byte[(int) contents.getLength()];
contents.getInputStream().read(imageData);//read image bytes
image = new ImageIcon(imageData);//create the image
//if image successfully loaded,create and add DrawJPanel
add(new DrawJPanel(),BorderLayout.CENTER);
}//end try
catch(Exception e)
{
e.printStackTrace();
}// end catch
}//end method init
//DrawJPanel used to display loaded image
private class DrawJPanel extends JPanel
{
//display image
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//the following values are used to center the image
double spareWidth=
getWidth()-scaleValue*image.getIconHeight();
double spareHeight=
getHeight()-scaleValue*image.getIconHeight();
//draw image with scaled width and height
g.drawImage(image.getImage(),
(int) (spareWidth)/2,(int)(spareHeight)/2,
(int) (image.getIconWidth()*scaleValue),
(int) (image.getIconHeight()*scaleValue),this);
}//end method paint
}//end class DrawJPanel
}//end class LoadImageAndScale
----------------------------------
以上是源码。在eclipse里编的,然后直接运行run。但没有出现“open”对话框。是和java start有关吗?如何达成效果啊,可以用eclipse做到吗?
----------------------------
javax.jnlp.UnavailableServiceException: uninitialized
at javax.jnlp.ServiceManager.lookup(Unknown Source)
at LoadImageAndScale.init(LoadImageAndScale.java:61)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是错误信息。
---------
没有出现“open”对话框,这是为什么啊?