📄 mainframe.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends Frame implements ActionListener{
Sound sound1=new Sound("81.mid");
Sound sound2=new Sound("5.mid");
int foods=5;
int length=10;
int sudu=200;
int detu=0;
int jafnsu=10;
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("文件");
Menu menuHelp = new Menu("帮助");
Panel helpPanel=new Panel();
TextArea bText=new TextArea(20,50);
Button helpButton=new Button("返回");
MenuItem itemHelp = new MenuItem("帮助");
MenuItem itemStart = new MenuItem("开始");
MenuItem itemSet = new MenuItem("设置");
MenuItem itemExit = new MenuItem("退出");
MsgPanel hueiPanel=new MsgPanel(this);
MsgPanel vPanel=new MsgPanel(this,"你胜利了");
MsgPanel fPanel=new MsgPanel(this,"你输了");
Panel msgPanel=new Panel();
GamePanel gamPanel=new GamePanel(this);
SetPanel setPanel=new SetPanel(this);
Panel cuoliPanel=new Panel();
Panel teziPanel=new Panel();
CardLayout card=new CardLayout();
public MainFrame() {
menuFile.add(itemStart);
menuFile.add(itemSet);
menuFile.add(itemExit);
menuHelp.add(itemHelp);
menuBar.add(menuFile);
menuBar.add(menuHelp);
setMenuBar(menuBar);
setTitle("贪食蛇游戏__郑法配__24号");
setSize(new Dimension(450, 500));
itemStart.addActionListener(this);
itemSet.addActionListener(this);
itemExit.addActionListener(this);
itemHelp.addActionListener(this);
helpButton.addActionListener(this);
helpPanel.add(bText);
helpPanel.add(helpButton);
helpPanel.setLayout(null);
bText.setBounds(50,10,320,300);
helpButton.setBounds(200,320,50,30);
this.setLayout(card);
this.add(hueiPanel,"huei");
this.add(msgPanel,"msg");
this.add(gamPanel,"gam");
this.add(setPanel,"set");
this.add(helpPanel,"help");
this.add(cuoliPanel,"cuoli");
this.add(teziPanel,"tezi");
this.add(vPanel,"v");
this.add(fPanel,"f");
bText.setText("用键盘的上下左右键控制分");
itemExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainFrame.this.windowClosed();
}
}
);
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MainFrame.this.windowClosed();
}
}
);
sound1.loop();
}
public void start(){
card.show(this,"gam");
gamPanel.requestFocus();
gamPanel.start(this);
sound2.loop();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==itemStart){
sound1.stop();
start();
}
else if(e.getSource()==itemSet){
card.show(this,"set");
}
else if(e.getSource()==itemExit){
card.show(this,"set");
}
else if(e.getSource()==itemHelp){
card.show(this,"help");
}
else if(e.getSource()==helpButton){
start();
}
}
protected void windowClosed() {
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -