📄 wuziqi.java
字号:
package 五子棋2;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class WuZiQi { public WuZiQi() { tk = Toolkit.getDefaultToolkit(); o = tk.getImage("images/o.gif"); x = tk.getImage("images/x.gif"); init(); } protected void init()//初始化 { board = new ChessBoard(o, x,Color.white); } void go(JFrame win)//建立窗口 { JPanel show=new JPanel();//新建Panel,作为4个按钮的容器; Cfirst=new Button("电脑先"); Pfirst=new Button("玩家先"); NewGame=new Button("新游戏"); Unmove=new Button("悔棋"); a=new ActionHandler(this); Cfirst.addActionListener(a); Pfirst.addActionListener(a); NewGame.addActionListener(a); Unmove.addActionListener(a); show.add(Cfirst); show.add(Pfirst); show.add(Unmove); show.add(NewGame); ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem rb = new JRadioButtonMenuItem("容易"); group.add(rb); rb.setSelected(true); // rb.setMnemonic(KeyEvent.VK_A); rb.addActionListener(a); show.add(rb); rb = new JRadioButtonMenuItem("中等"); group.add(rb); //rb.setMnemonic(KeyEvent.VK_U); rb.addActionListener(a); show.add(rb); rb = new JRadioButtonMenuItem("困难"); group.add(rb); // rb.setMnemonic(KeyEvent.VK_C); rb.addActionListener(a); show.add(rb); win.add(show); win.add(board); win.setLayout(new BorderLayout());//设置两个panel 的位置 win.add(show, BorderLayout.NORTH); win.add(board, BorderLayout.CENTER); win.addWindowListener(new WindowHandler(win)) ; win.setSize(448, 509); win.validate(); win.setVisible(true); board.addMouseListener(new ClickHandler(this)); } public void Move(int x, int y){//开始游戏 board.StartGame(x,y); } public static void main(String[] args) { WuZiQi game = new WuZiQi(); game.go(new JFrame("五子棋")); } protected Image x, o; protected Toolkit tk = null; public ChessBoard board=null; Button Cfirst; Button Pfirst; Button NewGame; Button Unmove; protected ActionListener a;}class ActionHandler implements ActionListener { ActionHandler(WuZiQi p) { op = p; } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if ( cmd.equals("电脑先") ){ //按下Computer first按钮,则重新开始游戏,设置电脑先下,且电脑走一步; op.board.NewGame();//新游戏; op.board.CFirst();//设置电脑先下; op.board.CMove();//电脑走一步; } else if ( cmd.equals("玩家先") ){ //按下Player first按钮,则重新开始游戏,设置电脑先下 op.board.NewGame(); op.board.PFirst(); } else if ( cmd.equals("新游戏") ) { op.board.NewGame(); //新游戏; } if ( cmd.equals("悔棋") )//悔棋; { op.board.UnMove(); } } private WuZiQi op; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -