Java 如何使GUI在web浏览器中工作?
我正在为我的学校期末项目写一个程序。我用框架写了整个项目,结果发现它需要一个小程序。所以我试着把所有的东西都转换,但都不起作用 源代码:Java 如何使GUI在web浏览器中工作?,java,html,user-interface,applet,jframe,Java,Html,User Interface,Applet,Jframe,我正在为我的学校期末项目写一个程序。我用框架写了整个项目,结果发现它需要一个小程序。所以我试着把所有的东西都转换,但都不起作用 源代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.applet.Applet; public class AlteredBattleship extends Applet implements Act
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();
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
导入java.applet.applet;
公共类AlteredBattleship扩展小程序实现ActionListener{
int ro;//保存行输入
int co;//保存列输入
int[][]shots=new int[10][10];//保存所有拍摄的快照
boolean displayLose=false;//如果游戏失败,将触发布尔值
boolean displayWin=false;//如果游戏赢了,将触发布尔值
//接下来的六个变量包含了船只有多少部分没有被击中
int 2=2;
int三=3;
int-four=4;
int-three-two=3;
int 5=5;
//如果船沉没,布尔值触发
布尔值=false;
布尔值=假;
布尔值=假;
布尔值=假;
布尔值fiveSunk=false;
//有困难
布尔b=真;
布尔n=假;
布尔e=false;
ArrayList misses=新建ArrayList();//保存所有未命中的快照
ArrayList hits=新建ArrayList();//保存所有命中快照
int shotCount=75;//玩家必须赢得多少次投篮
int[][]board=newint[10][10];//占据所有船位
JApplet main=new JApplet();//保存所有图形
TextField input=newtextfield(2);//从播放器获取输入
TextField count=new TextField(2);//显示左镜头
已执行的公共无效操作(操作事件e){
};
//放置四条长船
公共场所(四){
int fourStartx=(int)(Math.random()*5);
int fourStarty=(int)(Math.random()*9);
对于(inti=0;i你犯了一个错误
public class AlteredBattleship extends Applet implements ActionListener
应该是:
public class AlteredBattleship extends JApplet implements ActionListener
如果小程序仍然无法工作,请通知。我很可能会出错。我不知道什么是DrawPanel类。它确实可以工作。谢谢。很抱歉,我没有包括DrawPanel,我不相信它与此相关。我很乐意提供帮助。不要使用小程序,它们在Java 1.9中已被弃用,并且将从Java 1.10中删除。