board.java
来自「一个自己做的battleShip 的java 网络游戏。。。供大家交流」· Java 代码 · 共 61 行
JAVA
61 行
package Client;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import Utility.*;
/* This class allows the user to display the board relecting the state of
our ships and that of the opponent. The constructor takes an argument
that inactivates the Button (for our ownBoard) */
public class Board extends JPanel implements BattleShipConstants
{
public Board(String title, ActionListener a, boolean active)
{
setBorder(new TitledBorder(title));
setLayout(new GridLayout(10,10,0,0));
for (int i=1; i<=100; i++)
{
JButton b = new JButton(""+i);
add(b);
b.addActionListener(a);
b.setEnabled(active);
}
}
void setCross(int pos)
{
((JButton)getComponent(pos-1)).setEnabled(false);
((JButton)getComponent(pos-1)).setText("x");
}
void setHit(int pos)
{
((JButton)getComponent(pos-1)).setEnabled(false);
((JButton)getComponent(pos-1)).setText("HIT");
}
void display(Ship s)
{
display(s,shipColors[s.size()-1]);
}
// This method allows a ship position to be reflected using a specified color
void display(Ship s, Color c)
{
boolean okay = true;
int pos = s.topLeftPosition();
int size = s.size();
int k = 1;
while ( k <= size)
{
((JButton)getComponent(pos-1)).setText(""+size);
getComponent(pos-1).setBackground(c);
int inc = s.orientation() == 0 ? 1:10;
pos += inc;
k++;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?