1

这是我的第一个小程序项目,由于某种原因,当我尝试运行小程序时出现此错误。

Warning: Can't read AppletViewer properties file: 
    C:\Users\students\.hotjava\properties Using defaults.
java.security.AccessControlException: access denied (
    "java.lang.RuntimePermission" "exitVM.0")

但除此之外,代码工作得很好。我不确定是什么导致了这个错误。我在这项任务中迟到了 2 天,需要有人帮助我。几个小时以来,我一直把头撞在桌子上。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import javax.swing.JApplet;


/** rock paper scissors class */
public class JRockPaperScissors extends JApplet{
    // Variables declaration
    private JLabel titleLbl;
private JLabel selectionLbl;
private JLabel resultsLbl;
private JTextArea resultTextArea;
private JButton rockBn;
private JButton paperBn;
private JButton scissorBn;
private Container contentPane;
    private int cpu = 0;
    private int wins = 0;
    private int loses = 0;
    private int ties = 0;
    private final int CHOICE_MAX = 3;
    private final int ROCK = 0;
    private final int PAPER = 1;
    private final int SCISSORS = 2;
    private Random rand = new Random();
// End of variables declaration

public JRockPaperScissors(){
    initializeComponent();
}

    /** initializing componets */
private void initializeComponent()
{
    titleLbl = new JLabel();
    selectionLbl = new JLabel();
    resultsLbl = new JLabel();
    resultTextArea = new JTextArea();
    rockBn = new JButton();
    paperBn = new JButton();
    scissorBn = new JButton();
    contentPane = getContentPane();

    //
    // titleLbl
    //
    titleLbl.setText("Rock, Paper, Scissors");
            titleLbl.setFont(new Font("Garrmond", Font.BOLD, 30));
    //
    // selectionLbl
    //
    selectionLbl.setText("Choose one");
            selectionLbl.setFont(new Font("Arial",Font.BOLD,14));
    //
    // resultsLbl
    //
    resultsLbl.setText("*****Results*****");
            selectionLbl.setFont(new Font("Arial",Font.BOLD,14));
    //
    // resultTextArea
    //
    resultTextArea.setOpaque(false);
    resultTextArea.setBackground(new Color(236, 233, 216));

    //
    // rockBn
    //
    rockBn.setText("Rock");
    rockBn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            rockBn_actionPerformed(e);
        }

    });
    //
    // paperBn
    //
    paperBn.setText("Paper");
    paperBn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            paperBn_actionPerformed(e);
        }

    });
    //
    // scissorBn
    //
    scissorBn.setText("Scissors");               
    scissorBn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            scissorBn_actionPerformed(e);
        }

    });
    //
    // contentPane
    //
    contentPane.setLayout(null);
    addComponent(contentPane, titleLbl, 5,9,370,47);
    addComponent(contentPane, selectionLbl, 9,54,150,35);
    addComponent(contentPane, resultsLbl, 9,93,144,38);
    addComponent(contentPane, resultTextArea, 5,132,398,111);
    addComponent(contentPane, rockBn, 162,58,78,31);
    addComponent(contentPane, paperBn, 247,58,81,31);
    addComponent(contentPane, scissorBn, 334,58,87,31);
    contentPane.setSize(new Dimension(435, 290));
    //
    // JRockPaperScissors
    //      
    setTitle("Paper Rock Scissors");
    setSize(435, 290);
    setVisible(true);
}

/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
    c.setBounds(x,y,width,height);
    container.add(c);
}

/** action event methods */
private void rockBn_actionPerformed(ActionEvent e)
{
        //play the game choosing rock
        play(ROCK);
}

private void paperBn_actionPerformed(ActionEvent e)
{
        //play the game choosing paper
        play(PAPER);
}

private void scissorBn_actionPerformed(ActionEvent e)
{
        //play the game choosing scissors
        play(SCISSORS);
}
    //method to play the game
    private void play(int pick){
         String resultStr = "";                   
         //random computer choice
         cpu = rand.nextInt(CHOICE_MAX);
         //nested if statments to determine winner loser or tie
         if(pick == cpu){
             ties++;
             resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
             resultStr += "\nWinner: Tie";
             resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
             resultTextArea.setText(resultStr);
         }
         else if( (pick == ROCK) && (cpu == SCISSORS) ){
             wins++;
             resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
             resultStr += "\nWinner: You";
             resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
             resultTextArea.setText(resultStr);
         }
         else if( (pick == PAPER) && (cpu == ROCK) ){
             wins++;
             resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
             resultStr += "\nWinner: You";
             resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
             resultTextArea.setText(resultStr);                 
         }
         else if( (pick == SCISSORS) && (cpu == PAPER) ){
             wins++;
             resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
             resultStr += "\nWinner: You";
             resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
             resultTextArea.setText(resultStr);
         }
         else{
             loses++;
             resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
             resultStr += "\nWinner: Computer";
             resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
             resultTextArea.setText(resultStr);                 
         }

    }
        //function returns paper rock or scissors as a string
         public String selection(int choice){
             String tempStr;
             switch(choice){
               case 0:
                     tempStr = "rock";
                     break;
               case 1:
                     tempStr = "paper";
                     break;
               case 2:
                     tempStr = "scissors";
                     break;
               default:
                     tempStr = "invalid";
              }
              return tempStr;
         }

}

代码

<html>
<head><title>Rock Paper Scissors</title></head>
<body bgcolor="black">
<font color="yellow"><center><h1>Welcome!! Ready to play?</h1></center>
<p><b><object code = “JRockPaperScissors.class” width = “435” height = “290”&gt;</object>
</font>
</body>
</html>
4

3 回答 3

1

为安全起见,小程序访问本地磁盘的优先级有限。

要获得访问权限,您应该在发布(或在本地运行)之前签署您的小程序。无法描述细节,请参阅本教程了解如何操作。

于 2012-04-09T16:53:45.137 回答
1

实际上,1997 年发布的 JDK 1.1 需要签署小程序。我强烈不建议深入研究小程序签名,这是一大堆蠕虫。当前的 JVM 具有基于权限的安全策略。如果这是一个快速破解,我建议您在 JAVA_HOME/jre/lib/secirity/java.policy 文件中添加一行代码:

permission java.lang.RuntimePermission "exitVM";

确保将其添加到默认域。这是我的整个文件的示例,配置了退出 JVM 的权限:

grant codeBase "file:/home/igor/programs/jdk1.6.0_24/jre/lib/ext/*" {
  permission java.security.AllPermission;
};

grant codeBase "file:/usr/java/packages/lib/ext/*" {
  permission java.security.AllPermission;
};

grant {
  permission java.lang.RuntimePermission "stopThread";
  permission java.net.SocketPermission "localhost:1024-", "listen";
  permission java.util.PropertyPermission "java.version", "read";
  permission java.util.PropertyPermission "java.vendor", "read";
  permission java.util.PropertyPermission "java.vendor.url", "read";
  permission java.util.PropertyPermission "java.class.version", "read";
  permission java.util.PropertyPermission "os.name", "read";
  permission java.util.PropertyPermission "os.version", "read";
  permission java.util.PropertyPermission "os.arch", "read";
  permission java.util.PropertyPermission "file.separator", "read";
  permission java.util.PropertyPermission "path.separator", "read";
  permission java.util.PropertyPermission "line.separator", "read";
  permission java.util.PropertyPermission "java.specification.version", "read";
  permission java.util.PropertyPermission "java.specification.vendor", "read";
  permission java.util.PropertyPermission "java.specification.name", "read";
  permission java.util.PropertyPermission "java.vm.specification.version", "read";
  permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
  permission java.util.PropertyPermission "java.vm.specification.name", "read";
  permission java.util.PropertyPermission "java.vm.version", "read";
  permission java.util.PropertyPermission "java.vm.vendor", "read";
  permission java.util.PropertyPermission "java.vm.name", "read";
  permission java.lang.RuntimePermission "exitVM";
};

如果需要,可以通过添加以下行来添加所有权限:

permission java.security.AllPermission;

在这种情况下,您的小程序将作为 Java 应用程序运行,没有任何限制

于 2012-04-09T17:05:15.757 回答
0

对我来说,这似乎是一个损坏的小程序查看器安装。该代码在此处的小程序查看器中运行而没有错误(在我注释了未编译的行之后,这意味着源不是您正在使用的)。当您在支持 Java 的浏览器中尝试它时会发生什么?

HTML

我认为 HTML 与问题无关,但既然你提到了它:

<object code = “JRockPaperScissors.class” width = “435” height = “290”&gt;</object>

在测试期间我有 3 条建议。

  1. 换成object_applet
  2. 删除.classcode 属性的扩展名 - 它是完全限定的类名,而不是文件名。虽然通常可以容忍第二个,但第一个是正确的。
  3. 将“智能引号”更改为标准 " 样式引号。看起来 HTML 是在文字处理器中准备的。或者更好的是,完全删除引号。

例如

 <applet code=JRockPaperScissors width=435 height=290></applet>
于 2012-04-09T17:30:00.427 回答