我正在为我的学校最终项目编写一个程序。我用框架编写整个项目,但发现它需要是一个applet。所以我尝试转换所有内容,但它不起作用。
源代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.applet.Applet;
public class AlteredBattleship extends Applet implements ActionListener{
int ro;//holds row input
int co;//holds column input
int[][] shots=new int[10][10];//holds all shots taken
boolean displayLose=false;//boolean triggers if game is lost
boolean displayWin=false;//boolean triggers if game is won
//the next six variables hold how many parts of ships haven't been hit
int two=2;
int three=3;
int four=4;
int threeTwo=3;
int five=5;
//boolean triggers if a boat is sunk
boolean twoSunk=false;
boolean threeSunk=false;
boolean threeTwoSunk=false;
boolean fourSunk=false;
boolean fiveSunk=false;
//holds difficulty
boolean b=true;
boolean n=false;
boolean e=false;
ArrayList<Integer> misses= new ArrayList<Integer>();//holds all missed shots
ArrayList<Integer> hits= new ArrayList<Integer>();//holds all hit shots
int shotCount=75;//how many shots player has to win
int[][] board=new int[10][10];//holds all ship positions
JApplet main =new JApplet();//holds all graphics
TextField input=new TextField(2);//takes input from player
TextField count=new TextField(2);//displays shots left
public void actionPerformed(ActionEvent e) {
};
//places the four long ship
public void placeFour(){
int fourStartx=(int)(Math.random()*5);
int fourStarty=(int)(Math.random()*9);
for(int i=0; i<4; i++){
board[fourStartx+i][fourStarty]=4;
}
}
//places the three long ship
public void placeThree(){
int threeStartx=(int)(Math.random()*9);
int threeStarty=(int)(Math.random()*6);
if(board[threeStartx][threeStarty]==0&& board[threeStartx][threeStarty+1]==0&&board[threeStartx][threeStarty+2]==0){
board[threeStartx][threeStarty]=3;
board[threeStartx][threeStarty+1]=3;
board[threeStartx][threeStarty+2]=3;
}
else
this.placeThree();
}
//places the two long ship
public void placeTwo(){
int twoStartx=(int)(Math.random()*7);
int twoStarty=(int)(Math.random()*9);
if(board[twoStartx][twoStarty]==0&& board[twoStartx+1][twoStarty]==0){
board[twoStartx][twoStarty]=2;
board[twoStartx+1][twoStarty]=2;
}
else
this.placeTwo();
}
//places the second three long ship
public void placeThreeTwo(){
int threeStartx=(int)(Math.random()*6);
int threeStarty=(int)(Math.random()*9);
if(board[threeStartx][threeStarty]==0&& board[threeStartx+1][threeStarty]==0&&board[threeStartx+2][threeStarty]==0){
board[threeStartx][threeStarty]=6;
board[threeStartx+1][threeStarty]=6;
board[threeStartx+2][threeStarty]=6;
}
else
this.placeThreeTwo();
}
//places the five long ship
public void placeFive(){
int twoStartx=(int)(Math.random()*9);
int twoStarty=(int)(Math.random()*4);
if(board[twoStartx][twoStarty]==0&& board[twoStartx][twoStarty+1]==0&& board[twoStartx][twoStarty+2]==0&& board[twoStartx][twoStarty+3]==0&& board[twoStartx][twoStarty+4]==0){
board[twoStartx][twoStarty]=5;
board[twoStartx][twoStarty+1]=5;
board[twoStartx][twoStarty+2]=5;
board[twoStartx][twoStarty+3]=5;
board[twoStartx][twoStarty+4]=5;
}
else
this.placeFive();
}
//this method creates the frame and builds the interactable parts of the GUI
public void init() {
main = new JApplet();
DrawPanel drawPanel = new DrawPanel();
main.add(drawPanel);
JPanel holder=new JPanel();
JPanel line=new JPanel();
JPanel space=new JPanel();
space.setMaximumSize(new Dimension(50,150));
space.setBackground(Color.lightGray);
ButtonGroup level=new ButtonGroup();//holds all difficulty radioButtons
JRadioButton beginner = new JRadioButton();//beginner difficulty button
beginner.setSelected(true);
beginner.setBackground(Color.lightGray);
beginner.addActionListener(new ActionListener() { //makes the button set difficulty to beginner
public void actionPerformed(ActionEvent i) {
b=true;
n=false;
e=false;
}
});
space.add(beginner);//adds button to panel
JRadioButton normal = new JRadioButton();//normal difficulty button
normal.setBackground(Color.lightGray);
space.add(normal);//adds button to panel
normal.addActionListener(new ActionListener() { //makes the button set difficulty to normal
public void actionPerformed(ActionEvent i) {
b=false;
n=true;
e=false;
}
});
JRadioButton expert = new JRadioButton();
expert.setBackground(Color.lightGray);
expert.addActionListener(new ActionListener() {//makes the button set difficulty to expert
public void actionPerformed(ActionEvent i) {
b=false;
n=false;
e=true;
}
});
space.add(expert);//adds button to panel
level.add(beginner);
level.add(normal);
level.add(expert);
count.setEditable(false);
count.setText(Integer.toString(shotCount));
line.setMaximumSize(new Dimension(105,100));
line.setBackground(Color.lightGray);
line.add(count);
holder.setLayout(new BoxLayout(holder, BoxLayout.Y_AXIS));//creates panel that holds all other panels
holder.setMaximumSize(new Dimension(20,50));
JPanel inputHold=new JPanel();//creates panel that holds input textField
inputHold.setMaximumSize(new Dimension(40,100));
inputHold.add(input);//adds textField to panel
//makes input take shot and put it through program
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String row=input.getText().substring(0,1);//makes the first letter of input the row
String column=input.getText().substring(1,input.getText().length());//makes number portion column
ro=row.compareTo("A");//makes row a number representation
co=Integer.parseInt(column)-1;//makes the String into int
if(row.compareTo("A")<10 && Integer.parseInt(column)<=10&&shots[ro][co]==0){//makes sure shot is in bounds and not already called
if(board[ro][co]==0){//checks for miss
misses.add(ro);
misses.add(co);
}
else{//makes sure it's a hit
hits.add(ro);
hits.add(co);
if(board[ro][co]==2){//checks if hit is two ship
two--;
if (two==0)//checks to see if two is sunk
twoSunk=true;
}
else if(board[ro][co]==3){//checks if hit is three ship
three--;
if (three==0)//checks to see if three is sunk
threeSunk=true;
}
else if(board[ro][co]==6){//checks if hit is other three ship
threeTwo--;
if(threeTwo==0)//checks if other three ship is sunk
threeTwoSunk=true;
}
else if(board[ro][co]==4){//check if hit is four ship
four--;
if (four==0)//check if four ship is sunk
fourSunk=true;
}
else if(board[ro][co]==5){//check if hit is five ship
five--;
if(five==0)//check if five ship is sunk
fiveSunk=true;
}
}
shotCount--;//takes away a shot
shots[ro][co]=1;//remembers spot as an already taken shot
}
if(twoSunk&&threeSunk&&fourSunk&&threeTwoSunk&&fiveSunk){//checks for win status
displayWin=true;
}
input.setText("");//resets textField
//frame.repaint();//resets board with new shot
count.setText(Integer.toString(shotCount));//resets shotCount to new number
if(shotCount==0){//checks for lose status
displayLose=true;
input.setEditable(false);
}
}
});
JPanel spacer=new JPanel();
JButton newB=new JButton("New Game");//creates new game button
newB.addActionListener(new ActionListener() { //makes button work
public void actionPerformed(ActionEvent i) {
//resets all variables to new game status
displayLose=false;
displayWin=false;
twoSunk=false;
threeSunk=false;
threeTwoSunk=false;
fourSunk=false;
fiveSunk=false;
two=2;
three=3;
threeTwo=3;
four=4;
five=5;
misses= new ArrayList<Integer>();
hits= new ArrayList<Integer>();
if(b)
shotCount=75;
else if(n)
shotCount=65;
else if(e)
shotCount=50;
count.setText(Integer.toString(shotCount));
board=new int[10][10];
shots=new int[10][10];
placeFour();
placeThree();
placeThreeTwo();
placeFive();
placeTwo();
main.repaint();
}
});
//adds all panels to frame
spacer.add(newB,BorderLayout.NORTH);
spacer.setBackground(Color.lightGray);
inputHold.setBackground(Color.lightGray);
spacer.setBackground(Color.lightGray);
spacer.setMaximumSize(new Dimension(100,32));
holder.add(space,BorderLayout.NORTH);
holder.add(line,BorderLayout.NORTH);
holder.add(inputHold, BorderLayout.NORTH);
holder.add(spacer,BorderLayout.SOUTH);
holder.setBackground(Color.lightGray);
main.add(holder,BorderLayout.EAST);
main.setVisible(true);
//places ships
this.placeFour();
this.placeThree();
this.placeTwo();
this.placeThreeTwo();
this.placeFive();
}
我的HTML是:
<EMBED
type="application/x-java-applet;version=1.2"
code="AlteredBattleship.class"
WIDTH=680 HEIGHT=400>
</EMBED>
我很抱歉,如果答案非常明显,我对Java相当陌生并且非常陌生。
谢谢你的时间
答案 0 :(得分:1)
你犯了一个错误。
public class AlteredBattleship extends Applet implements ActionListener
应该是:
public class AlteredBattleship extends JApplet implements ActionListener
如果Applet仍然不起作用,请通知。