我正在嘗試創建一個Java小程序,是的,我知道,棄用,等等等等,我有我的理由。我似乎無法通過將文件放在我的網站上或通過使用HTML文件在本地加載文件來加載文件。任何關於這件事的幫助都會得到最多的讚賞,因爲這讓我感到無奈。Java Applet不會在Web或HTML文件中加載,我做錯了什麼?
<html>
<head>
<title>
Applet
</title>
</head>
<body>
<h2>Applet</h2>
<embed
archive="eloRating.jar"
code="eloRatingSystem"
width=550 height=300>
This means you done goofed
</applet>
</body>
</html>
。
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.util.ArrayList;
@SuppressWarnings("serial")
public class eloRatingSystem extends JApplet /*implements Runnable*/{
/*****************************************************************************/
private static final int BUTTON_HEIGHT = 50;
private static final int BUTTON_WIDTH = 150;
/*****************************************************************************/
private Button newPlayer, editPlayer, editTeams, commitMatch, downloadBook;
private JButton editPopUp;
/*****************************************************************************/
private JComponent[] inputs;
private JComponent[] tables;
/*****************************************************************************/
private int testRow;
private int playerCounter;
/*****************************************************************************/
private ArrayList<Player> pList;
/*****************************************************************************/
private String[] titles;
/*****************************************************************************/
private ListenForAction lForAction;
/*****************************************************************************/
private modifyExcel book;
/*****************************************************************************/
private JPanel panel;
/*****************************************************************************/
private JTable playerTable;
/*****************************************************************************/
private TableRowSorter<?> sorter;
/*****************************************************************************/
Dimension d;
/*****************************************************************************/
/*****************************Initialization**********************************/
public void init() {
inputs = new JComponent[]{
new JLabel("Enter The Player's Name"),
};
testRow = 10000;
playerCounter = 0;
lForAction = new ListenForAction();
book = new modifyExcel();
book.openExcel();
titles = new String[6];
panel = new JPanel();
pList = new ArrayList<Player>();
d = new Dimension(500,140);
titles = book.setTitles();
/*DEBUG
JOptionPane.showMessageDialog(null, titles[0] + titles[1] + titles[2] + titles[3] + titles[4] + titles[5], "Work", JOptionPane.PLAIN_MESSAGE);
*/
editPopUp = new JButton("Edit Player");
newPlayer = new Button("Add Player");
editPlayer = new Button("Edit Player");
editTeams = new Button("Edit Teams");
commitMatch = new Button("Commit Match");
downloadBook = new Button("Download Excel File");
setLayout(null);
//editPopUp.setBounds(0,0,BUTTON_WIDTH,BUTTON_HEIGHT);
newPlayer.setBounds(75, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
editPlayer.setBounds(235, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
editTeams.setBounds(395, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
commitMatch.setBounds(555, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
panel.add(editPopUp);
newPlayer.addActionListener(lForAction);
editPlayer.addActionListener(lForAction);
editPopUp.addActionListener(lForAction);
initPlayers(book.getRows());
//System.out.println(pList[4].getName());
tables = new JComponent[]{
new JScrollPane(playerTable = new JTable(new MyTableModel(pList))),
panel
};
playerTable.setAutoCreateRowSorter(true);
createSorter();
add(newPlayer);
add(editPlayer);
add(editTeams);
add(commitMatch);
this.setBackground(Color.BLACK);
this.setVisible(true);
}
public void start() {
}
public void destroy() {
}
public void stop() {
}
public void paint(Graphics g){
}
public void run() {
this.setVisible(true);
}
/*****************************Initialize Players*************************************
* This function calls for a read in of all players and data listed
* inside of an excel spreadsheet. The players are added to an Array
* List for easy access and dynamic storage capabilities. *
/************************************************************************************/
public void initPlayers(int i){
for(int x = 0; x < (i-1); x++){
//System.out.println("Made it Here");
pList.add(book.setPlayer((x+1)));
/*DEBUG
JOptionPane.showMessageDialog(null, pList[x].getName());
*/
}
playerCounter = (book.getRows()-1);
}
/***************************************************************************************
* This function defines an Action Listener for defining button activity
* The buttons all refer to this listener to create their functionality
***************************************************************************************/
public class ListenForAction implements ActionListener{
String S;
int active = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newPlayer){
S = JOptionPane.showInputDialog(null, inputs, "Add New Player", JOptionPane.PLAIN_MESSAGE);
if (S != null){
addPlayer(S);
}
/*DEBUG
JOptionPane.showMessageDialog(null, pList[playerCounter - 1].getName());
pList[0].setElo(2300);
pList[0].calculateElo(6, "LOSE");
JOptionPane.showMessageDialog(null, pList[0].getElo());
*/
} else if (e.getSource() == editPlayer){
JOptionPane.showOptionDialog(null, tables, "Edit Player Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
} else if (e.getSource() == editPopUp){
if (active == 0) {
editPopUp.setText("Stop Editing");
testRow = playerTable.getSelectedRow();
active = 1;
} else {
editPopUp.setText("Edit Player");
testRow = 1000000;
active = 0;
}
}
}
}
/************************************************************************************
* Custom Table Model to allow for a list of editable size and data
* The players are stored read into this via the Array List and the
* Data is displayed in a list that can be sorted by column.
*************************************************************************************/
private class MyTableModel extends AbstractTableModel {
ArrayList<Player> list = null;
MyTableModel(ArrayList<Player> list) {
this.list = list;
}
public int getColumnCount() {
return titles.length;
}
public int getRowCount() {
return list.size();
}
public String getColumnName(int col) {
return titles[col];
}
public void setValueAt(Object value, int row, int col){
switch(col) {
case 0:
pList.get(row).setName((String)value);
break;
case 1:
pList.get(row).setElo((Integer)value);
break;
case 2:
pList.get(row).setAttendance((Integer)value);
break;
case 3:
pList.get(row).setMVP((Integer)value);
break;
case 4:
pList.get(row).setWins((Integer)value);
break;
case 5:
pList.get(row).setLose((Integer)value);
break;
}
System.out.println(pList.get(row).getName() + pList.get(row).getAttendance());
}
public boolean isCellEditable(int row, int column)
{
//System.out.println(Flag);
if(testRow == playerTable.getSelectedRow()){
return true;
} else {
return false;
}
}
public Object getValueAt(int row, int col) {
Player object = list.get(row);
switch (col) {
case 0:
return object.getName();
case 1:
return object.getElo();
case 2:
return object.getAttendance();
case 3:
return object.getMVP();
case 4:
return object.getWin();
case 5:
return object.getLose();
default:
return "unknown";
}
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
private void addPlayer(String S){
pList.add(new Player(S));
tables = new JComponent[]{
new JScrollPane(playerTable = new JTable(new MyTableModel(pList))),
panel
};
playerCounter++;
createSorter();
}
public void createSorter(){
playerTable.setPreferredScrollableViewportSize(d);
playerTable.setAutoResizeMode(getHeight());
playerTable.setAutoCreateRowSorter(true);
sorter = (TableRowSorter<?>)playerTable.getRowSorter();
sorter.setRowFilter(new RowFilter<TableModel, Integer>(){
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry){
boolean included = true;
Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
if(cellValue == null || cellValue.toString().trim().isEmpty()){
included = false;
}
return included;
}
});
}
}
。
public class Player {
private String Name;
private int Elo = 1600, Attendance = 0, MVP = 0, Win = 0, Lose = 0;
public Player(String n, int e, int a, int m, int w, int l){
Name = n;
Elo = e;
Attendance = a;
MVP = m;
Win = w;
Lose = l;
}
public Player(String n){
Name = n;
}
public Player() {
}
/***********************************************/
public void setName(String n){
Name = n;
}
public void setElo(int e){
Elo = e;
}
public void setAttendance(int a){
Attendance = a;
}
public void setMVP(int m){
MVP = m;
}
public void setWins(int w){
Win = w;
}
public void setLose(int l){
Lose = l;
}
/************************************************/
public void addAttendance(int e){
Attendance += e;
}
public void addElo(int e){
Elo += e;
}
public void addMVP(int e){
MVP += e;
}
public void addWins(int e){
Win += e;
}
public void addLose(int e){
Lose += e;
}
/************************************************/
public void calculateElo(int oE, String win){
double tRatingP; //holds the players transformed Elo Rating for calculation
double tRatingO; //holds the opponents transformed Elo Rating for calculation
double expectedP; //Players expected score
double pointP = 0;
int eloValue = 32; //default elo value
if (Elo > 2400){
eloValue = 24;
}
switch(win){
case "WIN": pointP = 1;
break;
case "DRAW": pointP = 0.5;
break;
case "LOSE": pointP = 0;
break;
}
tRatingP = 10^(Elo/400);
tRatingO = 10^(oE/400);
expectedP = tRatingP/(tRatingP+tRatingO);
this.setElo((int)Math.round(Elo + (eloValue*(pointP-expectedP))));
}
/************************************************/
public String getName(){
return Name;
}
public int getElo(){
return Elo;
}
public int getAttendance(){
return Attendance;
}
public int getMVP(){
return MVP;
}
public int getWin(){
return Win;
}
public int getLose(){
return Lose;
}
}
。
import java.io.File;
import java.util.Date;
import jxl.*;
import jxl.write.*;
public class modifyExcel {
private Player pHolder;
private String[] tHolder = new String[6];
private Workbook eloBook;
Sheet sheet;
public void openExcel(){
try {
eloBook = Workbook.getWorkbook(new File("./eloSpreadsheet.xls"));
} catch(Exception e){
throw new Error(e);
}
sheet = eloBook.getSheet(0);
}
public void appendExcel(Player p){
}
public String[] setTitles(){
for(int x = 0; x<=5; x++){
tHolder[x] = sheet.getCell(x, 0).getContents();
}
return(tHolder);
}
public Player setPlayer(int i){
//System.out.println("Made it Here " + i);
pHolder = new Player();
pHolder.setName(sheet.getCell(0, i).getContents());
pHolder.setElo(Integer.parseInt(sheet.getCell(1, i).getContents()));
pHolder.setAttendance(Integer.parseInt(sheet.getCell(2, i).getContents()));
pHolder.setMVP(Integer.parseInt(sheet.getCell(3, i).getContents()));
pHolder.setWins(Integer.parseInt(sheet.getCell(4, i).getContents()));
pHolder.setLose(Integer.parseInt(sheet.getCell(5, i).getContents()));
return(pHolder);
}
public int getRows(){
return(sheet.getRows());
}
}
欲瞭解更多信息,我甚至不能得到的Java控制檯出現,我認爲這意味着該applet根本不加載。
你允許或禁止小程序從該域運行? – Fallenreaper
Applets是允許的,但除此之外,它不會在HTML文件中運行,並且我測試了演示applet以確保沒有瀏覽器問題。 – Ranma344
確保將[Java控制檯](http://www.java.com/en/download/help/javaconsole.xml)配置爲顯示。如果在默認級別沒有輸出,請提高級別並再次嘗試。 –