我尝试从另一个类中更新一些标签文本,但我没有成功。这只是整个代码的一部分,但整个代码大约有500行......我可以根据要求发布它。
public class Allo implements ActionListener {
JFrame fenetre = new JFrame();
static JButton[][] bouton;
static int board[][] = new int[16][8];
int compteMiss = 0;
int essais = 0;
int coupsReussis = 0;
int navire1 = 0;
int navire2 = 0;
int naviresCoules = 0;
int destroyer1 = 0;
int destroyer2 = 0;
int destroyer3 = 0;
int destroyersCoules = 0;
int sousMarin1 = 0;
int sousMarin2 = 0;
int sousMarin3 = 0;
int sousMarin4 = 0;
int sousMarinsCoules = 0;
static String nom;
int admin = 0;
String choixclique = "D2";
public Allo(int width, int height) {
SpringLayout springLayout = new SpringLayout();
fenetre.getContentPane().setLayout(springLayout);
bouton = new JButton[16][8];
for (int r = 0; r < 16; r++)
{
for (int c = 0; c < 8; c++)
{
bouton[r][c] = new JButton(" ");
springLayout.putConstraint(SpringLayout.NORTH, bouton[r][c], r*30+100, SpringLayout.NORTH, fenetre.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, bouton[r][c], c*40+300, SpringLayout.WEST, fenetre.getContentPane());
bouton[r][c].setSize(40,40);
bouton[r][c].addActionListener(new ButtonPressed(r,c));
fenetre.getContentPane().add(bouton[r][c]);
}
}
fenetre.getContentPane().setBackground(new Color(173, 216, 230));
fenetre.setSize(900, 700);
fenetre.setResizable(false);
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
fenetre.getContentPane().add(panel);
SpringLayout sl_panel = new SpringLayout();
panel.setLayout(sl_panel);
JPanel panel_1 = new JPanel();
springLayout.putConstraint(SpringLayout.NORTH, panel_1, 0, SpringLayout.NORTH, panel);
springLayout.putConstraint(SpringLayout.WEST, panel_1, 0, SpringLayout.WEST, fenetre.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, panel_1, 662, SpringLayout.NORTH, fenetre.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, panel_1, 202, SpringLayout.WEST, fenetre.getContentPane());
fenetre.getContentPane().add(panel_1);
SpringLayout sl_panel_1 = new SpringLayout();
panel_1.setLayout(sl_panel_1);
JLabel lblRsultats = new JLabel("R\u00E9sultats");
lblRsultats.setForeground(new Color(148, 0, 211));
lblRsultats.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
sl_panel_1.putConstraint(SpringLayout.NORTH, lblRsultats, 10, SpringLayout.NORTH, panel_1);
sl_panel_1.putConstraint(SpringLayout.WEST, lblRsultats, 10, SpringLayout.WEST, panel_1);
panel_1.add(lblRsultats);
JLabel lblSurDestroyers = new JLabel("Sur 3 Destroyers:");
lblSurDestroyers.setFont(new Font("Tahoma", Font.BOLD, 11));
sl_panel_1.putConstraint(SpringLayout.NORTH, lblSurDestroyers, 40, SpringLayout.SOUTH, lblRsultats);
sl_panel_1.putConstraint(SpringLayout.WEST, lblSurDestroyers, 10, SpringLayout.WEST, panel_1);
panel_1.add(lblSurDestroyers);
JLabel labelDestroyers = new JLabel("0");
labelDestroyers.setForeground(new Color(147, 112, 219));
labelDestroyers.setFont(new Font("Tahoma", Font.BOLD, 11));
sl_panel_1.putConstraint(SpringLayout.SOUTH, labelDestroyers, 0, SpringLayout.SOUTH, lblSurDestroyers);
sl_panel_1.putConstraint(SpringLayout.EAST, labelDestroyers, -10, SpringLayout.EAST, panel_1);
panel_1.add(labelDestroyers);
JLabel lblSurNavires = new JLabel("Sur 2 Navires:");
sl_panel_1.putConstraint(SpringLayout.NORTH, lblSurNavires, 16, SpringLayout.SOUTH, lblSurDestroyers);
sl_panel_1.putConstraint(SpringLayout.EAST, lblSurNavires, 0, SpringLayout.EAST, lblRsultats);
lblSurNavires.setFont(new Font("Tahoma", Font.BOLD, 11));
panel_1.add(lblSurNavires);
JLabel labelNavires = new JLabel("0");
labelNavires.setForeground(new Color(147, 112, 219));
labelNavires.setFont(new Font("Tahoma", Font.BOLD, 11));
sl_panel_1.putConstraint(SpringLayout.SOUTH, labelNavires, 0, SpringLayout.SOUTH, lblSurNavires);
sl_panel_1.putConstraint(SpringLayout.EAST, labelNavires, -10, SpringLayout.EAST, panel_1);
panel_1.add(labelNavires);
JLabel lblNewLabel = new JLabel("Sur 4 Sous-Marins:");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
sl_panel_1.putConstraint(SpringLayout.NORTH, lblNewLabel, 13, SpringLayout.SOUTH, lblSurNavires);
sl_panel_1.putConstraint(SpringLayout.WEST, lblNewLabel, 10, SpringLayout.WEST, panel_1);
panel_1.add(lblNewLabel);
JLabel labelSousMarins = new JLabel("0");
labelSousMarins.setFont(new Font("Tahoma", Font.BOLD, 11));
labelSousMarins.setForeground(new Color(147, 112, 219));
sl_panel_1.putConstraint(SpringLayout.SOUTH, labelSousMarins, 0, SpringLayout.SOUTH, lblNewLabel);
sl_panel_1.putConstraint(SpringLayout.EAST, labelSousMarins, -10, SpringLayout.EAST, panel_1);
panel_1.add(labelSousMarins);
JLabel lblNombreDeCoups = new JLabel("Nombre de coups rat\u00E9s:");
lblNombreDeCoups.setForeground(new Color(255, 0, 0));
lblNombreDeCoups.setFont(new Font("Tahoma", Font.BOLD, 11));
sl_panel_1.putConstraint(SpringLayout.NORTH, lblNombreDeCoups, 18, SpringLayout.SOUTH, lblNewLabel);
sl_panel_1.putConstraint(SpringLayout.WEST, lblNombreDeCoups, 10, SpringLayout.WEST, panel_1);
panel_1.add(lblNombreDeCoups);
JLabel labelCoupsRates = new JLabel("0");
labelCoupsRates.setFont(new Font("Tahoma", Font.BOLD, 11));
labelCoupsRates.setForeground(Color.RED);
sl_panel_1.putConstraint(SpringLayout.SOUTH, labelCoupsRates, 0, SpringLayout.SOUTH, lblNombreDeCoups);
sl_panel_1.putConstraint(SpringLayout.EAST, labelCoupsRates, 0, SpringLayout.EAST, labelDestroyers);
panel_1.add(labelCoupsRates);
JLabel lblNombreDeCoups_1 = new JLabel("Nombre de coups r\u00E9ussis:");
sl_panel_1.putConstraint(SpringLayout.NORTH, lblNombreDeCoups_1, 23, SpringLayout.SOUTH, lblNombreDeCoups);
sl_panel_1.putConstraint(SpringLayout.WEST, lblNombreDeCoups_1, 0, SpringLayout.WEST, lblRsultats);
lblNombreDeCoups_1.setForeground(Color.RED);
lblNombreDeCoups_1.setFont(new Font("Tahoma", Font.BOLD, 11));
panel_1.add(lblNombreDeCoups_1);
JLabel labelCoupsReussis = new JLabel("0");
labelCoupsReussis.setFont(new Font("Tahoma", Font.BOLD, 11));
labelCoupsReussis.setForeground(Color.RED);
sl_panel_1.putConstraint(SpringLayout.NORTH, labelCoupsReussis, 0, SpringLayout.NORTH, lblNombreDeCoups_1);
sl_panel_1.putConstraint(SpringLayout.EAST, labelCoupsReussis, 0, SpringLayout.EAST, labelDestroyers);
panel_1.add(labelCoupsReussis);
JLabel lblJeuBatailleNavale = new JLabel("Jeu Bataille Navale 1614");
springLayout.putConstraint(SpringLayout.NORTH, lblJeuBatailleNavale, 10, SpringLayout.NORTH, fenetre.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, lblJeuBatailleNavale, 157, SpringLayout.EAST, panel_1);
springLayout.putConstraint(SpringLayout.EAST, lblJeuBatailleNavale, 374, SpringLayout.EAST, panel_1);
lblJeuBatailleNavale.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
fenetre.getContentPane().add(lblJeuBatailleNavale);
public void actionPerformed(ActionEvent e) {
labelDestroyers.setText(ButtonPressed.getText());
}
fenetre.setVisible(true);
}
public class ButtonPressed implements ActionListener
{
public void setText() {
int coupsReussis = 1;
}
public String getText() {
return coupsReussis;
}
int x; //rang
int y ;//colonne
public ButtonPressed(int rang, int colonne)
{
x = rang;
y = colonne;
}
public void actionPerformed(ActionEvent e)
{
if (board[x][y] == 3) {
bouton[x][y].setText("N3");
for (int r = 0; r < 16; r++)
{
for (int c = 0; c < 8; c++)
{
if ((bouton[x][y].getName() == "a") && (bouton[x][y].getText() == "N3") && (navire1 != 3)) {
navire1 = navire1 + 1;
bouton[x][y].setName("A");
if (navire1 == 3) {
naviresCoules = naviresCoules + 1;
}
} else {
if ((bouton[x][y].getName() == "b") && (bouton[x][y].getText() == "N3") && (navire2 != 3)) {
navire2 = navire2 + 1;
bouton[x][y].setName("B");
if (navire2 == 3) {
naviresCoules = naviresCoules + 1;
}
}
}
}
}
}
essais++;
}
}
提前致谢!