调用目标异常

时间:2013-04-25 09:36:34

标签: java swing applet japplet

这是我的程序代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class JAdapterOkno extends WindowAdapter {

    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

class CalcButton extends JButton implements ActionListener {

    Kontroler k;

    CalcButton(Kontroler k, String nazwa, int s) {
        super(nazwa);
        this.k = k;
        setFocusable(false);
        this.setFont(new Font("Tahoma", Font.BOLD, s));
        addActionListener(this);
    }

    public void actionPerformed(ActionEvent er) {
        k.dopisz(this);
    }
}

class InfoButton extends JButton implements ActionListener {

    Kontroler k;

    InfoButton(Kontroler k, int s) {
        super("Info");
        this.k = k;
        setFocusable(false);
        addActionListener(this);
        this.setFont(new Font("Tahoma", Font.BOLD, s));
    }

    public void actionPerformed(ActionEvent err) {
        k.info();
    }
}

class ClearButton extends JButton implements ActionListener {

    Kontroler k;

    ClearButton(Kontroler k, int s) {
        super("C");
        this.k = k;
        setFocusable(false);
        addActionListener(this);
        this.setFont(new Font("Tahoma", Font.BOLD, s));
    }

    public void actionPerformed(ActionEvent errr) {
        k.clear();
    }
}

class Wyswietlacz extends JTextPane implements KeyListener {

    Kontroler z;

    Wyswietlacz(Kontroler zz, int k) {
        super();
        z = zz;
        setFocusable(true);
        setEditable(false);
        this.setFont(new Font("Tahoma", Font.PLAIN, k));
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent evt) {
        char c = evt.getKeyChar();
        if (c == '0' || c == '1') {
            if (z.LorP) {
                if (z.Lwyr.length() < 16) {
                    z.Lwyr.append(c);
                }
            } else {
                if (z.Pwyr.length() < 16) {
                    z.Pwyr.append(c);
                }
            }
        } else {
            if (z.sign == '2' && c != '=' && z.Lwyr.length() != 0) {
                z.LorP = false;
                z.sign = c;
            }
            if (z.sign != '2' && c == '=') {
                z.eval(true);
            }
        }
        z.eval(false);
        StringBuilder fin = new StringBuilder(z.Lwyr);
        if (z.sign != '2') {
            fin.append(z.sign);
            fin.append(z.Pwyr);
        }
        setText(fin.toString());
    }

    public void keyReleased(KeyEvent evt) {
        ;
    }

    public void keyTyped(KeyEvent evt) {
        ;
    }
}

public class Kontroler extends JApplet {

    JMenuBar menuBar;
    /**
     * < Zmienna menubar
     */
    JMenu menu;
    /**
     * < zmienna menu
     */
    JMenuItem menuItem;
    /**
     * < zmienna element menu
     */
    StringBuilder Lwyr;
    /**
     * < zmienna lewe wyrazenie obliczenia
     */
    StringBuilder Pwyr;
    /**
     * < zmienna prawe wyrazenie obliczenia
     */
    final int modulo = (int) Math.pow(2.0, 15.0);
    /**
     * < stala
     */
    char sign;
    /**
     * < dzialanie
     */
    Boolean LorP;
    int x;
    int y;
    CalcButton inf;
    /**
     * < przycisk gui
     */
    CalcButton zero;
    /**
     * < przycisk gui
     */
    CalcButton one;
    /**
     * < przycisk gui
     */
    CalcButton add;
    /**
     * < przycisk gui
     */
    CalcButton sub;
    /**
     * < przycisk gui
     */
    CalcButton div;
    /**
     * < przycisk gui
     */
    CalcButton mlt;
    /**
     * < przycisk gui
     */
    CalcButton modu;
    /**
     * < przycisk gui
     */
    ClearButton blank;
    /**
     * < przycisk gui
     */
    Wyswietlacz wys;
    /**
     * < wyswietlacz
     */
    JPanel przyciski;
    /**
     * < panel guzikow
     */
    JPanel calosc;

    /**
     * < panel
     */
    /**
     * metoda inicjalizujaca applet
     */
    public void st() {
        menuBar = new JMenuBar();
        menu = new JMenu("informacje");
        menuItem = new JMenuItem("Info");
        menuItem.addActionListener(
            new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                info();
            }
        });
        menuBar.add(menu);
        menu.add(menuItem);
        setJMenuBar(menuBar);
        float xx = x / 240;
        float yy = y / 320;
        int avg = (int) ((xx + yy) / 2);
        System.out.println(modulo);
        setPreferredSize(new Dimension(x, y));
        inf = new CalcButton(this, "=", 15 * avg);
        zero = new CalcButton(this, "0", 15 * avg);
        one = new CalcButton(this, "1", 15 * avg);
        add = new CalcButton(this, "+", 15 * avg);
        sub = new CalcButton(this, "-", 15 * avg);
        div = new CalcButton(this, "/", 15 * avg);
        mlt = new CalcButton(this, "*", 15 * avg);
        modu = new CalcButton(this, "%", 15 * avg);
        blank = new ClearButton(this, 20 * avg);
        wys = new Wyswietlacz(this, 15 * avg);
        wys.setPreferredSize(new Dimension(x, (int) (y / 4)));
        przyciski = new JPanel();
        przyciski.setPreferredSize(new Dimension(x, x));
        przyciski.setLayout(new GridLayout(3, 3));
        przyciski.add(zero);
        przyciski.add(one);
        przyciski.add(add);
        przyciski.add(sub);
        przyciski.add(mlt);
        przyciski.add(div);
        przyciski.add(modu);
        przyciski.add(inf);
        przyciski.add(blank);
        przyciski.setFocusable(false);
        calosc = new JPanel();
        calosc.setLayout(new BoxLayout(calosc, BoxLayout.Y_AXIS));
        calosc.add(wys);
        calosc.add(przyciski);
        calosc.setPreferredSize(new Dimension(x, y));
        calosc.setFocusable(false);
        add(calosc);
        setFocusable(false);
    }

    public void init() {
        st();
    }

    public void destroy() {
        ;
    }

    public void stop() {
        ;
    }

    Kontroler() {
        this.Lwyr = new StringBuilder();
        this.Pwyr = new StringBuilder();
        this.sign = '2';
        this.LorP = true;
        this.x = 240;
        this.y = 320;

    }

    Kontroler(int x, int y) {
        if (x < 240 || y < 320) {
            JOptionPane.showMessageDialog(null,
                "Program nie dziala przy tak malej wielkosci okna");
            System.exit(0);
        }
        this.Lwyr = new StringBuilder();
        this.Pwyr = new StringBuilder();
        this.sign = '2';
        this.LorP = true;
        this.x = x;
        this.y = y;
    }

    void info() {
        JOptionPane.showMessageDialog(null, "Calc v1.1");
    }

    void dopisz(CalcButton c) {
        String actual = c.getText();
        char ac = actual.charAt(0);
        if (ac == '0' || ac == '1') {
            if (LorP) {
                if (Lwyr.length() < 16) {
                    Lwyr.append(ac);
                }
            } else {
                if (Pwyr.length() < 16) {
                    Pwyr.append(ac);
                }
            }
        } else {
            if (sign == '2' && ac != '=' && Lwyr.length() != 0) {
                LorP = false;
                sign = ac;
            }
            if (sign != '2' && ac == '=') {
                eval(true);
            }
        }
        eval(false);
        StringBuilder fin = new StringBuilder(Lwyr);
        if (sign != '2') {
            fin.append(sign);
            fin.append(Pwyr);
        }
        c.k.wys.setText(fin.toString());
    }

    void clear() {
        this.Lwyr = new StringBuilder();
        this.Pwyr = new StringBuilder();
        this.wys.setText("");
    }

    void eval(Boolean t) {
        int l;
        int p;
        if (Lwyr.length() == 0) {
            return;
        }
        if (t || (Lwyr.length() == 16 && Pwyr.length() == 16 && sign != '2')) {
            if (Pwyr.length() == 0) {
                Pwyr = Lwyr;
            }
            l = Integer.parseInt(Integer.valueOf(this.Lwyr.toString(), 2).toString());
            p = Integer.parseInt(Integer.valueOf(this.Pwyr.toString(), 2).toString());
            if (l == 0 && p == 0) {
                Lwyr = new StringBuilder();
                Pwyr = new StringBuilder();
                sign = '2';
                this.LorP = true;
            }
            switch (this.sign) {
                case '+':
                    this.Lwyr = new StringBuilder(Integer.toString((l + p) % modulo, 2));
                    break;
                case '-':
                    this.Lwyr = new StringBuilder(Integer.toString((l - p) % modulo, 2));
                    break;
                case '*':
                    this.Lwyr = new StringBuilder(Integer.toString((l * p) % modulo, 2));
                    break;
                case '/':
                    this.Lwyr = new StringBuilder(Integer.toString((l / p) % modulo, 2));
                    break;
                case '%':
                    this.Lwyr = new StringBuilder(Integer.toString((l % p) % modulo, 2));
                    break;
            }
            Pwyr = new StringBuilder();
            sign = '2';
            this.LorP = true;
        }

    }

    /**
     * Metoda main
     */
    public static void main(String[] args) {
        JFrame f = new JFrame();
        Kontroler k;
        if (args.length == 0) {
            k = new Kontroler();
            f.add(k);
            k.init();
        } else {
            k = new Kontroler(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
            f.add(k);
            k.init();
        }
        f.pack();
        f.setVisible(true);
        f.setResizable(false);
    }
}

这是完全例外

basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
    ... 5 more
Caused by: java.lang.RuntimeException: java.lang.IllegalAccessException:
    Class com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1 can not
        access a member of class Kontroler with modifiers ""
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.IllegalAccessException:
    Class com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1 can not
        access a member of class Kontroler with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    ... 20 more

当我“正常”奔跑时,它有效;但是当我尝试在Web浏览器中将其作为applet运行时,我会重新调用调用目标异常?我的错在哪里?我实际上看不到异常开始的地方,因为我在Java控制台中没有任何细节

1 个答案:

答案 0 :(得分:3)

异常的根本原因IllegalAccessException会提示您快速查看What Applets Can and Cannot Do,但您的小程序 违反沙箱。

使用appletviewer运行,如here所示,显示以下错误,通过制作Kontroler构造函数public解决。

load: Kontroler is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not
    access a member of class Kontroler with modifiers ""
...

此外,