这是我的第一个applet项目,出于某种原因,当我尝试运行applet时,我收到此错误。
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 CODE
<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”></object>
</font>
</body>
</html>
答案 0 :(得分:1)
出于安全考虑,applet的优先级有限,无法访问本地磁盘。
要获得访问权限,您应该在发布(或在本地运行)之前签署您的applet。无法描述详细信息,请参阅this tutorial了解如何执行此操作。
答案 1 :(得分:1)
实际上,1997年推出的JDK 1.1需要签署applet。我强烈不建议深入applet签名,这是一个很大的蠕虫。 当前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;
在这种情况下,您的applet将作为Java应用程序运行,没有任何限制
答案 2 :(得分:0)
这对我来说似乎是一个破坏的applet查看器安装。该代码在applet查看器中运行时没有错误(在我评论未编译的行之后,这意味着源不您正在使用的内容)。在支持Java的浏览器中尝试时会发生什么?
我不认为HTML与问题有关,但是因为你提到它:
<object code = “JRockPaperScissors.class” width = “435” height = “290”></object>
我在测试期间有3条建议。
object
applet
.class
扩展名 - 它应该是完全限定的类名,而不是文件名。虽然第二个通常被容忍,但第一个是正确的。 <applet code=JRockPaperScissors width=435 height=290></applet>