【实例简介】
【实例截图】
【核心代码】
package game;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// 五子棋的主框架
public class StartChessJFrame extends JFrame {
private ChessBoard chessBoard;//对战面板
private JPanel toolbar;//工具条面板
private Button startButton;//设置开始按钮
private Button backButton;//设置悔棋按钮
private Button exitButton;//设置退出按钮
private JPanel leftbar; //设置左面板,包括头像以及名称
private JPanel playerbar1;//玩家一头像
private JPanel playerbar2;//玩家二头像
public StartChessJFrame(){
setTitle("五子棋");//设置标题
chessBoard=new ChessBoard();//初始化面板对象,创建和添加菜单
setSize(780,654);
setResizable(false);// 设置窗体不可以放大
MyItemListener lis=new MyItemListener();//初始化按钮事件监听器内部类
Container contentPane=getContentPane();
contentPane.add(chessBoard);
chessBoard.setOpaque(true);
ClassLoader loader = StartChessJFrame.class.getClassLoader(); //加载图片
// URL url_1 = loader.getResource("player1.jpg");
// ImageIcon player1 = new ImageIcon(url_1);//玩家1头像
// URL url_2 = loader.getResource("player2.jpg");
// ImageIcon player2 = new ImageIcon(url_2);//玩家2头像
leftbar = new JPanel();
playerbar1= new JPanel();
playerbar2=new JPanel();
playerbar1.setLayout(new GridLayout(1,1));//1行一列
playerbar2.setLayout(new GridLayout(1,1));//1行一列
JPanel playerPanel1 = new JPanel(new GridLayout(1,1,5,20));//玩家头像总区域
JPanel playerPanel2 = new JPanel(new GridLayout(1,1,5,20));
// JLabel player1Label = new JLabel(player1);
// JLabel player2Label = new JLabel(player2);
// playerPanel1.add(player1Label);//玩家1
// playerPanel2.add(player2Label);//玩家2
playerbar1.add(playerPanel1);
playerbar2.add(playerPanel2);
JLabel player1name=new JLabel(" 黑方");
JLabel player2name=new JLabel(" 白方");
leftbar.setLayout(new GridLayout(4, 1,5,10));//4行一列
leftbar.add(playerbar1);
leftbar.add(player1name);
leftbar.add(playerbar2);
leftbar.add(player2name);
leftbar.setBorder(BorderFactory.createEmptyBorder(50,10,18,10));
toolbar=new JPanel();//工具面板栏实例化
toolbar.setBorder(BorderFactory.createEmptyBorder(0,25,0,30));
startButton=new Button("重新开始");
backButton=new Button("悔棋");
exitButton=new Button("退出");//三个按钮初始化
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//将工具面板按钮用FlowLayout布局
chessBoard.setLayout(new BorderLayout());
toolbar.add(backButton);
toolbar.add(startButton);
toolbar.add(exitButton);//个按钮添加到工具面板上
startButton.addActionListener(lis);
backButton.addActionListener(lis);
exitButton.addActionListener(lis);//将三个按钮事件注册监听事件
add(leftbar,BorderLayout.WEST);
add(toolbar,BorderLayout.SOUTH);//将工具面板布局到界面南方也就是下面
add(chessBoard,BorderLayout.CENTER);//将面板对象添加到窗体上
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置界面关闭事件
pack();//自适应大小
}
private class MyItemListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();//获取事件源
if(obj==startButton){
System.out.println("重新开始...");//重新开始
chessBoard.restartGame();
}else if(obj==exitButton){
System.exit(0);//结束应用程序
}else if(obj==backButton){
System.out.println("悔棋...");//悔棋
chessBoard.goback();
}
}
}
static void playMusic(){//背景音乐播放
// URL cb;
// ClassLoader loader = StartChessJFrame.class.getClassLoader();
// cb= loader.getResource("背景音乐.wav"); // 获取背景音乐资源
// AudioClip aau;
// aau = Applet.newAudioClip(cb);
// //aau.play();
// aau.loop();//循环播放
// System.out.println("可以播放");
}
public static void main(String[] args) {
StartChessJFrame f=new StartChessJFrame();//创建主框架
playMusic(); //播放背景音乐
f.setVisible(true);//显示主框架
}
}