2013-12-14 43 views
1

開始製作使用JButtons的戰艦遊戲,知道它比簡單繪製圖形要困難得多,但是我覺得用這個選項來完成這項壯舉會有點不得了。無論如何,我正在拋出錯誤,並且eclipse並沒有宣佈這些錯誤是什麼。任何人都可以破譯這些,或者至少讓我接近解決我的問題?編輯:更新的代碼被髮布使用JButtons的戰列艦

import java.awt.event.*; 
import java.awt.*; 
import java.util.ArrayList; 
import javax.swing.*; 

public class TestDemo extends JPanel implements ActionListener{ 
    private JPanel lButton, rButton, textPan, turnPan; 
    private JRadioButton turnC, turnP; 
    private JTextArea jta; 
    private static JMenuBar menuBar; 
    private JRadioButton jrb; 
    private static int SIZE = 10; 
    private JButton[][] left, right; 
    private int rand1, rand2; 
    private int turnIndicator; //Allows the swap method to switch between player turn and computer turn 0 = player turn, 1 = computer turn 
    public TestDemo(){ 
     setLayout(new FlowLayout(FlowLayout.CENTER)); 

     /** Buttons + Button Panels */ 
     lButton = new JPanel(); 
     lButton.setLayout(new GridLayout(SIZE,SIZE)); 
     rButton = new JPanel(); 
     rButton.setLayout(new GridLayout(SIZE,SIZE)); 
     left = new JButton[SIZE][SIZE]; 
     right = new JButton[SIZE][SIZE]; 
     turnIndicator = 0; 
     for (char row = 'A'; row <= 'J'; row++) 
      for (int col = 1, i = 0, j = 0; col <= 10 && i < SIZE && j < SIZE; col++, i++, j++) { 
       left[i][j] = new JButton("" + row + col); 
       right[i][j] = new JButton(""+row+col); 
       left[i][j].setMargin(new Insets(0, 0, 0, 0)); 
       left[i][j].setPreferredSize(new Dimension(40, 40)); 
       right[i][j].setMargin(new Insets(0, 0, 0, 0)); 
       right[i][j].setPreferredSize(new Dimension(40, 40)); 
       left[i][j].addActionListener(this); 
       right[i][j].addActionListener(this); 
       lButton.add(left[i][j]); 
       rButton.add(right[i][j]); 
      }//end for 
     /** End of Buttons + Button panels */ 

     /** Text Area */ 
     jta = new JTextArea(5,25); 
     jta.setLineWrap(true); 
     jta.setWrapStyleWord(true); 
     jta.setEditable(false); 
     jta.setVisible(true); 
     textPan = new JPanel(); 
     textPan.setLayout(new FlowLayout(FlowLayout.CENTER)); 
     textPan.add(new JScrollPane(jta)); 
     /** End of Text Area */ 

     /** Turn radio button */ 
     turnC = new JRadioButton("Computer"); 
     turnC.addActionListener(this); 
     turnC.setEnabled(false); 
     turnP = new JRadioButton("Player"); 
     turnP.addActionListener(this); 
     turnP.setEnabled(false); 
     ButtonGroup group = new ButtonGroup(); 
     group.add(turnC); 
     group.add(turnP); 
     turnP.setSelected(true); 
     turnPan = new JPanel(); 
     turnPan.setLayout(new BorderLayout()); 
     turnPan.add(turnC, BorderLayout.LINE_START); 
     turnPan.add(turnP, BorderLayout.LINE_END); 
     /** End turn radio button and label */ 

     /** Menu setup */ 
     menuBar = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     file.setMnemonic(KeyEvent.VK_F); 
     JMenuItem newGame = new JMenuItem("New Game"); newGame.addActionListener(this); 
     JMenuItem exit = new JMenuItem("Exit"); exit.addActionListener(new exitApp()); 
     newGame.setMnemonic(KeyEvent.VK_N); exit.setMnemonic(KeyEvent.VK_E); 
     newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK)); 
     exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK)); 
     menuBar.add(file); 
     file.add(newGame); file.add(exit); 
     /** End Menu setup */ 

     JPanel gameTest = new JPanel(); 
     gameTest.setLayout(new BorderLayout()); 
     gameTest.add(lButton, BorderLayout.LINE_START); 
     gameTest.add(rButton, BorderLayout.LINE_END); 
     gameTest.add(textPan, BorderLayout.PAGE_END); 
     gameTest.add(turnPan, BorderLayout.CENTER); 
     add(gameTest); 
    }//end TestDemo 

public static void main(String[] args) { 
    JFrame frame = new JFrame(); 
    TestDemo td = new TestDemo(); 
    frame.add(td); 
    frame.setVisible(true); 
    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setJMenuBar(menuBar); 
}//end main 

public void actionPerformed(ActionEvent e) 
{ 
    JButton pressed = (JButton) e.getSource(); 
    if(turnIndicator == 0) 
    { 

    turnP.setSelected(true); 
    (pressed).setEnabled(false); 
    jta.append("Shot fired at : " + (pressed).getActionCommand() + '\n'); 
    turnIndicator = 1; 
    } 
    turnC.setSelected(true); 
    swap(); //Throwing an error 

}//end actionPerformed 

public void compAttk() 
{ 
    ArrayList<Integer> key1 = new ArrayList<Integer>(100); 
    ArrayList<Integer> key2 = new ArrayList<Integer>(100); 
    rand1 = (int) ((Math.random() * 10) + 1); 
    rand2 = (int) ((Math.random() * 10) + 1); 
    if(key1.contains(rand1) || key2.contains(rand2))/** Checks if the coords are already pressed*/ 
    { 
     rand1 = (int) ((Math.random() * 10) + 1); 
     rand2 = (int) ((Math.random() * 10) + 1); 
     right[rand1][rand2].setEnabled(false); //I'm sure will also throw an error 
    } 
    right[rand1][rand2].setEnabled(false); //Throwing an error 
    key1.add(rand1); 
    key2.add(rand2); 
    turnIndicator = 0; 
}//end compAttk 

public void swap() //Once user presses a button, turnIndicator will initialize to 1, then this method handles what happens afterwards. 
{ 
    if(turnIndicator == 1) 
    { 
     compAttk(); //Throwing an error 
     turnIndicator = 0; 
    } 
    else 
     JOptionPane.showMessageDialog(null, "Your turn"); 
}//end swap 
static class exitApp implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     System.exit(0); 
    } 
}//end exitApp 
}//end TestDemo class 


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10 
at TestDemo.compAttk(TestDemo.java:91) 
at TestDemo.swap(TestDemo.java:103) 
at TestDemo.actionPerformed(TestDemo.java:70) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at  javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6505) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
at java.awt.Component.processEvent(Component.java:6270) 
at java.awt.Container.processEvent(Container.java:2229) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
at java.awt.Container.dispatchEventImpl(Container.java:2273) 
at java.awt.Window.dispatchEventImpl(Window.java:2719) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:694) 
at java.awt.EventQueue$3.run(EventQueue.java:692) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:708) 
at java.awt.EventQueue$4.run(EventQueue.java:706) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

回答

3

看起來你應該rand1 = (int) (Math.random() * 10);

,現在是寫你總是得到10的隨機數是不是您所期望的方式取代rand1 = (int) (Math.random() + 10);,我想。

這裏的另一件事是,如果你的ArrayList包含10個元素,它的最後一個索引是9,而不是10.這裏是ArrayIndexOutOfBoundsException來自哪裏。

+0

這是遲到了(我的藉口是在我的隨機數發生器中使用+而不是*)。關於數組列表的東西。不,我不想只有10個,我想要在1-10行之間隨機選擇int行和按鈕列。 –

+0

@jeffreyzachary,用'*'替換'+'應該修復異常,除非您的代碼存在更多問題。使用'+'而不是'*'使得你的隨機生成器總是返回10,這對數組來說是越界的。 – svz

+0

對,是的,它擺脫了這些錯誤。我的3個最大的問題是我爲我的'swap();'方法收到的錯誤,我如何調用'right [rand1] [rand2] .setEnabled(false);'和我調用compAttk );'在'swap();'方法中。我完全不確定是否有關java.awt等的其他錯誤會在這些3之後消失,但至少這會給我一些喘息的機會,知道我已經解決了我的方法錯誤。 –