📄 gamefrm.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GameFrm extends JFrame{
private MainPanel panel;
private final String[] itemNames = {"开始游戏","设置","退出","帮助"};
private JMenuItem[] items;
public GameFrm()
{
buildGUI();
hookEvent();
}
private void buildGUI()
{
SplashWindow splash = new SplashWindow(this,"Image/cover.jpg");
splash.StartSplash();
this.setSize(800,720);
this.setAlwaysOnTop(true);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle("五子棋java小游戏");
JMenuBar bar = new JMenuBar();
JMenu game = new JMenu("游戏");
items = new JMenuItem[itemNames.length];
for(int i=0;i< items.length;++i)
{
items[i] = new JMenuItem(itemNames[i]);
game.add(items[i]);
}
bar.add(game);
this.setJMenuBar(bar);
panel = new MainPanel();
GameInfoPane info = panel.getInfoPane();
this.getContentPane().add(panel,BorderLayout.CENTER);
this.getContentPane().add(info,BorderLayout.EAST);
//居中
Dimension size = this.getToolkit().getScreenSize();
double xBase = ( size.getWidth()-this.getWidth() )/2;
double yBase = ( size.getHeight()-this.getHeight() )/2;
this.setLocation(new Point((int)xBase,(int)yBase));
try{
Thread.sleep(2000);
}catch(InterruptedException e)
{}
splash.stopSplash();
this.setVisible(true);
}
private void hookEvent()
{
for(int i=0;i< items.length;++i)
items[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int temp = 3;
for(int i=0; i<itemNames.length;++i)
if(e.getActionCommand() == itemNames[i])
{
temp = i;
break;
}
switch(temp)
{
//开始游戏
case 0:
startGame();
break;
//ip port设置
case 1:
System.out.print("good");
break;
//退出
case 2:
exit();
break;
//帮助
case 3:
showGameInfo();
break;
default:
break;
}
}
});
}
private void showGameInfo()
{
JOptionPane.showMessageDialog(this,
"作者: 张建军","游戏信息",JOptionPane.PLAIN_MESSAGE);
}
private void exit()
{
if(JOptionPane.YES_OPTION ==
JOptionPane.showConfirmDialog(this, "确定退出吗?",
"退出",JOptionPane.YES_NO_CANCEL_OPTION))
System.exit(0);
}
private void startGame()
{
panel.startGame();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GameFrm();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -