import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 贪吃蛇界面 extends JFrame
{
JPanel panel1,panel11,panel12 ,panelset,helppanel;
JButton btStart,btParse,btExit,btHelp;
JLabel picture1,picture2,picture3;
JLabel label1,label2,label3,label4,label5;
JTextField textField1,textField2,textField3;
JRadioButton RadioButton1,RadioButton2,RadioButton3,RadioButton4,RadioButton5,RadioButton6,RadioButton7;
ButtonGroup radioGroup;
JComboBox backBox;
String speedLevelNumber[] = {"1","2","3","4","5"};
String colorNames[] = { "蓝色","黑色", "青色", "深灰色", "灰色",
"绿色", "浅灰色", "紫红色",
"橙色", "粉红色", "红色", "白色", "黄色" };
Color colors[] = { Color.blue, Color.black,
Color.cyan, Color.darkGray, Color.gray, Color.green,
Color.lightGray, Color.magenta, Color.orange, Color.pink,
Color.red, Color.white, Color.yellow };
//风格
String strings[] = { "Metal", "Motif", "Windows" };
UIManager.LookAndFeelInfo looks[];
JRadioButtonMenuItem radio[];
//判断暂停键是否第一次按下
boolean parse = true;
//设置按钮
String up,DOWN,LEFT,RIGTH;
贪吃蛇界面 ()
{
final 贪吃蛇游戏界面 panel2=new 贪吃蛇游戏界面(this);
//字体
Font font1=new Font("",Font.ITALIC+Font.BOLD,15);
Font font2=new Font("",Font.ITALIC+Font.BOLD,20);
Font font3=new Font("",Font.ITALIC+Font.BOLD,25);
Font font4=new Font("",Font.ITALIC+Font.BOLD,15);
//菜单栏
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
//主菜单
JMenu fileMenu=new JMenu("选项");
fileMenu.setFont(font2);fileMenu.setForeground(Color.blue);
//按键设置菜单
fileMenu.add(new AbstractAction("按键设置"){
public void actionPerformed(ActionEvent event){
panelset.setVisible(true);
}
}
); //结束监听菜单
fileMenu.addSeparator();
//网格设置
JMenu netmenu = new JMenu("网格设置");
netmenu.add(new AbstractAction("显示网格"){
public void actionPerformed(ActionEvent event){
try{panel2.setBackground(colors[backBox.getSelectedIndex()+1]); }
catch(ArrayIndexOutOfBoundsException e)
{panel2.setBackground(Color.blue);}
}
}
); //结束监听菜单
netmenu.add(new AbstractAction("隐藏网格"){
public void actionPerformed(ActionEvent event){
panel2.setBackground(colors[backBox.getSelectedIndex()]);
}
}
); //结束监听菜单
fileMenu.add(netmenu);
fileMenu.addSeparator();
//风格选项
JMenu menu = new JMenu("风格");
radio = new JRadioButtonMenuItem[ strings.length ];
ButtonGroup group = new ButtonGroup();
ItemHandler handler = new ItemHandler();
for ( int count = 0; count < radio.length; count++ ) {
radio[ count ] = new JRadioButtonMenuItem( strings[ count ] );
radio[ count ].addItemListener( handler );
group.add( radio[ count ] );
menu.add( radio[ count ] );
}
looks = UIManager.getInstalledLookAndFeels();
radio[ 2 ].setSelected( true );
fileMenu.add(menu);
menubar.add(fileMenu);
//设置容器
Container container = getContentPane();
container.setLayout( new BorderLayout(5,0));
//panel1——按钮之类部分
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel11 = new JPanel();
panel11.setLayout(new GridLayout(2,8));
panel12 = new JPanel();
panel12.setLayout(new FlowLayout());
label1 = new JLabel("等级",SwingConstants.CENTER);
label1.setFont(font1);
panel11.add(label1);
RadioButton1 = new JRadioButton("菜鸟",false);
RadioButton1.setBackground(Color.cyan);
RadioButton1.setFont(font1);
RadioButton2 = new JRadioButton("入门",true);
RadioButton2.setBackground(Color.cyan);
RadioButton2.setFont(font1);
RadioButton3 = new JRadioButton("高手",false);
RadioButton3.setBackground(Color.cyan);
RadioButton3.setFont(font1);
RadioButton4 = new JRadioButton("专家",false);
RadioButton4.setBackground(Color.cyan);
RadioButton4.setFont(font1);
RadioButton5 = new JRadioButton("精英",false);
RadioButton5.setBackground(Color.cyan);
RadioButton5.setFont(font1);
RadioButton6 = new JRadioButton("大师",false);
RadioButton6.setBackground(Color.cyan);
RadioButton6.setFont(font1);
RadioButton7 = new JRadioButton("超人",false);
RadioButton7.setBackground(Color.cyan);
RadioButton7.setFont(font1);
radioGroup = new ButtonGroup();
radioGroup.add(RadioButton1);
radioGroup.add(RadioButton2);
radioGroup.add(RadioButton3);
radioGroup.add(RadioButton4);
radioGroup.add(RadioButton5);
radioGroup.add(RadioButton6);
radioGroup.add(RadioButton7);
RadioButton1.addActionListener(new listener(400));
RadioButton2.addActionListener(new listener(300));
RadioButton3.addActionListener(new listener(200));
RadioButton4.addActionListener(new listener(100));
RadioButton5.addActionListener(new listener(75));
RadioButton6.addActionListener(new listener(50));
RadioButton7.addActionListener(new listener(25));
panel11.add(RadioButton1);
panel11.add(RadioButton2);
panel11.add(RadioButton3);
panel11.add(RadioButton4);
panel11.add(RadioButton5);
panel11.add(RadioButton6);
panel11.add(RadioButton7);
label2 = new JLabel("得分",SwingConstants.CENTER);
label2.setFont(font1);
panel11.add(label2);
textField1 = new JTextField("0",4);
textField1.setHorizontalAlignment(textField2.CENTER);
textField1.setFont(font1);
textField1.setBackground(Color.white);
textField1.setEditable(false);
panel11.add(textField1);
label3 = new JLabel("蛇身",SwingConstants.CENTER);
label3.setFont(font1);
panel11.add(label3);
textField2 = new JTextField("0",4);
textField2.setHorizontalAlignment(textField2.CENTER);
textField2.setFont(font1);
textField2.setBackground(Color.white);
textField2.setEditable(false);
panel11.add(textField2);
label4 = new JLabel("食物",SwingConstants.CENTER);
label4.setFont(font1);
panel11.add(label4);
textField3 = new JTextField("0",4);
textField3.setHorizontalAlignment(textField2.CENTER);
textField3.setFont(font1);
textField3.setBackground(Color.white);
textField3.setEditable(false);
panel11.add(textField3);
//下拉框换背景色
label5 = new JLabel("背景",SwingConstants.CENTER);
label5.setFont(font1);
panel11.add(label5);
backBox = new JComboBox( colorNames);
backBox.setFont(font2) ;
backBox.setBackground(Color.white);
backBox.setMaximumRowCount( 5 );
//开始监听
backBox.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
panel2.setBackground(colors[backBox.getSelectedIndex()]);
panel2.othersColor=colors[backBox.getSelectedIndex()];
for (int i = 0 ;i <panel2.row ;i++ )
{
for (int j = 0;j<panel2.col ;j++ )
{panel2.grids[i][j].setBackground(colors[backBox.getSelectedIndex()]);
}
}
}
}
); // 结束监听
panel11.add(backBox);
//button组件
//开始按钮
Icon btStartPicture = new ImageIcon( "开始.gif" );
btStart = new JButton("开始",btStartPicture);
btStart.setToolTipText("开始");
btStart.setFont(font3) ;
btStart.setHorizontalTextPosition( SwingConstants.CENTER );
btStart.setVerticalTextPosition( SwingConstants.BOTTOM );
btStart.setBackground(Color.green);
btStart.setForeground(Color.red);
//监听开始按钮
btStart.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent e)
{
panel2.newGame(speedtime);
}
}
);
//暂停按钮
Icon btParsePicture = new ImageIcon( "暂停.gif" );
btParse = new JButton("暂停",btParsePicture);
btParse.setToolTipText("暂停");
tcs(java).rar_tcs_tcs java_贪吃蛇_贪吃蛇java
版权申诉
179 浏览量
2022-09-22
16:19:42
上传
评论
收藏 157KB RAR 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+
最新资源
- 与spoon配套使用的jdk
- 基于单片机的便携式粮食水分测试仪的研究
- 测绘基坑支护工程变形监测报告1.pdf
- 基于PHP+swoole实现的微信机器人,依赖vbot和微信网页版的功能,帮助管理微信群/聊天/踢人等+源码+开发文档+运行教程
- com.xunmeng.pinduoduo_Release_cd290ca9_ARM64.apk
- 2788727d-25a0-41b2-b6b4-265d193edb95.doc
- 基于AVR单片机的伺服电机系统研究
- Lab-Electronic Craft Practicum-2-Simulation of a Single Tube Com
- 贪吃蛇基于TypeScript
- CS-CP1-2C3WF固件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈