2017-07-12 2 views
-1

Ich versuche, ein Schlachtschiff-Java-Spiel zu machen, und zum Start habe ich zwei jbutton-Grids erstellt und Arrays verwendet, um die jbuttons zu überprüfen. Beim Klicken auf ein jbutton, da ich alle jbuttons leer gelassen habe, sollte sich die Farbe von jbutton ändern. Eine Menge ist noch hinzuzufügen, um es zu einem vollen Spiel zu machen, aber die grundlegende Änderung der Farbe von jbutton auf Klick funktioniert nicht. Dies ist die erste Datei, die meine mpanel-Datei aufruft, wo alle Arbeiten erledigt sind.Jbutton führt keine Aktion auf Klick aus

import java.awt.*; 

import javax.swing.*; 

public class a { 

public static void main(String[] args) { 
    JFrame frame = new JFrame("Battleship"); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    mPanel mPanel = new mPanel(); 
    frame.getContentPane().add(mPanel); 

    frame.pack(); 

    frame.setVisible(true); 
    frame.setResizable(true); 


} 

} 

// hier ist die zweite Datei

import java.awt.event.*; 
import java.awt.*; 

import javax.swing.event.MouseInputAdapter; 
import javax.swing.*; 



public class mPanel extends JPanel { 
private static final int UNCLICKED = 6; 
private static final int MISS = 9; 
private static final Color COLOR_UNCLICKED = Color.white; 
private static final Color COLOR_MISS = Color.blue; 
private JLabel title; 
private JPanel titlePanel; 
private JButton[][] gridButton,gridButton1; 
private JPanel gridPanel,gridPanel1; 
private int[][] board,board1; 
GridListener gridListener = new GridListener(); 
int count=0; 
//count is kept to keep a check if itsplayer1 or 2's turn 

public mPanel() 
{ 

title = new JLabel("BATTLESHIP!"); 
titlePanel = new JPanel(); 
titlePanel.add(title); 
gridButton = new JButton[10][10]; 
gridButton1 = new JButton[10][10]; 
gridPanel = new JPanel(); 
gridPanel1= new JPanel(); 
gridPanel.setLayout(new GridLayout(10, 10)); 
gridPanel1.setLayout(new GridLayout(10, 10)); 
for (int r = 0; r < gridButton.length; r++) 
    for (int c = 0; c < gridButton[r].length; c++) 
    { 
    gridButton[r][c] = new JButton(); 
    gridButton[r][c].setBackground(COLOR_UNCLICKED); 
    gridButton[r][c].setEnabled(true); 
    gridButton[r][c].addActionListener(gridListener); 
    gridPanel.add(gridButton[r][c]); 
    } 
gridPanel1.setLayout(new GridLayout(10, 10)); 
for (int r = 0; r < gridButton1.length; r++) 
    for (int c = 0; c < gridButton1[r].length; c++) 
    { 
    gridButton1[r][c] = new JButton(); 
    gridButton1[r][c].setBackground(COLOR_UNCLICKED); 
    gridButton1[r][c].setEnabled(true); 
    gridButton1[r][c].addActionListener(gridListener); 
    gridPanel1.add(gridButton1[r][c]); 
    } 
this.setLayout(new BorderLayout()); 
this.add(titlePanel, "North"); 
this.add(gridPanel, BorderLayout.LINE_START); 
this.add(gridPanel1, BorderLayout.LINE_END); 
this.setPreferredSize(new Dimension(1100, 400)); 
board = new int[10][10];//imagine board is kept below grid buttons and whatever happens on grid buttons i store that change in this array. 
for (int r = 0; r < board.length; r++) 
for (int c = 0; c < board.length; c++) 
{ 
board[r][c] = UNCLICKED; 
gridButton[r][c].setEnabled(true); 
} 
board1 = new int[10][10]; 
for (int r = 0; r < board1.length; r++) 
for (int c = 0; c < board1.length; c++) 
{ 
board1[r][c] = UNCLICKED; 
gridButton1[r][c].setEnabled(true); 
} 


} 
class GridListener implements ActionListener 
{ 
public void actionPerformed1(ActionEvent evt) 
{ 
if(count%2==0) 
{ 

for (int r = 0; r < gridButton.length; r++) 
for(int c = 0; c < gridButton[r].length; c++) 
{ 

if (evt.getSource() != gridButton[r][c]) 
continue; 
handleGridButton(r,c); 
return; 
} 
} 

else 
{ 
System.out.println(count); 


for (int r = 0; r < gridButton1.length; r++) 
for(int c = 0; c < gridButton1[r].length; c++) 
{ 
if (evt.getSource() != gridButton1[r][c]) 
continue; 
handleGridButton(r,c); 
return; 
} 


} 

} 

@Override 
public void actionPerformed(ActionEvent e) { 
// TODO Auto-generated method stub 

} 
} 
public void handleGridButton(int r, int c) 
{ 
count++; 
if(count%2==0) 
{ 
if (board[r][c] == UNCLICKED) 
{ 
//For start i have kept the whole board empty so on click it should change its color 
     board[r][c] = MISS; 
     gridButton[r][c].setBackground(COLOR_MISS); 

} 
} 
else 
{ 
    if (board1[r][c] == UNCLICKED) 
    { 

    board1[r][c] = MISS; 
    gridButton1[r][c].setBackground(COLOR_MISS); 


    } 
    } 
    } 
    } 
+1

'actionPerformed1' kann/wird nie aufgerufen - verschiebe die Funktionalität auf die' actionPerformed' Methode anstatt – MadProgrammer

+1

danke es funktioniert –

+1

Bleibe bei den Java Namenskonventionen - kannst du nicht sehen wie 'mPanel mPanel = new mPanel();' bekommt verwirrend? –

Antwort

0

Gelöst

actionPerformed1 kann/wird nie genannt - statt die Funktionalität der Methode actionPerformed bewegt. War einfach nicht in der Lage, die Actionperformed zu nennen, auch das ist es.

Verwandte Themen