📄 board.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -