Java jnlp异常

时间:2016-01-25 06:03:25

标签: java applet jnlp

我是 Java Applets 区域的初学者,现在跟着一本书,我一步一步地写了所有写的指令,但是在编译时我遇到了错误。我正在尝试使用 JNLP ServiceManager ,当applet启动时,它必须要求权限但在我的情况下它会给我错误:

 javax.jnlp.UnavailableServiceException: uninitialized
    at javax.jnlp.ServiceManager.lookup(Unknown Source)
    at LoadImageAndScale.init(LoadImageAndScale.java:48)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:745)

这是我的代码:

import javax.jnlp.FileContents;
import javax.jnlp.FileOpenService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * Created by taner on 2016-01-25.
 */
public class LoadImageAndScale extends JApplet {
    private ImageIcon image;
    private JPanel scaleJPanel;
    private JLabel percentJLabel;
    private JTextField scaleInputJTextField;
    private JButton scaleChangeJButoon;
    private double scaleValue = 1.0;

    public void init() {
        scaleJPanel = new JPanel();
        percentJLabel = new JLabel("scale percent: ");
        scaleInputJTextField = new JTextField("100");
        scaleChangeJButoon = new JButton("Set Scale");

        scaleJPanel.add(percentJLabel);
        scaleJPanel.add(scaleInputJTextField);
        scaleJPanel.add(scaleChangeJButoon);
        add(scaleJPanel, BorderLayout.NORTH);

        scaleChangeJButoon.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        scaleValue = Double.parseDouble(
                                scaleInputJTextField.getText()) / 100.0;
                        repaint();
                    }
                }
        );

        try {
            FileOpenService fileOpenService = (FileOpenService) ServiceManager.lookup("javax.jnlp.FileOpenService");
            FileContents contents = fileOpenService.openFileDialog(null, null);

            byte[] imageData = new byte[(int) contents.getLength()];
            contents.getInputStream().read(imageData);
            image = new ImageIcon(imageData);

            add(new DrawJPanel(), BorderLayout.CENTER);

        } catch (UnavailableServiceException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class DrawJPanel extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            double spareWidth = getWidth() - scaleValue * image.getIconWidth();
            double spareHeight = getHeight() - scaleValue * image.getIconHeight();

            g.drawImage(image.getImage(),
                    (int) (spareWidth) / 2, (int) (spareHeight) / 2,
                    (int) (image.getIconWidth() * scaleValue),
                    (int) (image.getIconHeight() * scaleValue), this);
        }
    }
}

0 个答案:

没有答案