2016-03-04 52 views
0

我正在尝试创建一个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根本不加载。

+0

你允许或禁止小程序从该域运行? – Fallenreaper

+0

Applets是允许的,但除此之外,它不会在HTML文件中运行,并且我测试了演示applet以确保没有浏览器问题。 – Ranma344

+0

确保将[Java控制台](http://www.java.com/en/download/help/javaconsole.xml)配置为显示。如果在默认级别没有输出,请提高级别并再次尝试。 –

回答

0

“嵌入”标记的结尾有误。它应该是:

</embed> 

代替:

</applet> 
+0

在发布之后不久我就发现了这一点,但是当我尝试运行它时,我仍然没有收到任何信息。但AppletViewer工作得很好。 – Ranma344

+0

我不是applet的专家,但你有像“绘画”这样的空方法,它们在不调用它们的情况下覆盖超级方法。 – foxu94

+0

*:..你有像paint这样的空方法,它可以在不调用它们的情况下覆盖超级方法。“*这很棒,applet不可能使用重写和'无所事事'的paint方法!事实上,如果你制作如果答案是肯定的,那就值得投票赞成(虽然你对标签需要保持平衡和一致性是正确的,但我不认为这是导致applet失败的原因。) –