class="MsoNormal">弹球游戏分步解析(一)——界面绘制
接下来我们将要做的游戏是一个弹球游戏,在这个游戏中,我们将会运用到的知识点有:1、绘制窗体;2、绘制按钮;3、绘制菜单栏;4、事件监听器的使用;5、多线程;6文件操作之另存为方法;
1、2、3点在这节会跟大家仔细解释;第4点和第5点由于涉及的知识点较多,分别讲;
由于弹球游戏代码量较大,每次显示的代码都不是完整代码,最后我会把完整代码打包供大家学习;
好了,我们开始进入正题吧;首先上源代码;
//javax.swing.JFileChooser使用此来选择文件
package jumpingBall;
?
import java.awt.Toolkit;
import java.awt.BorderLayout;
import java.awt.Menu;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
?
?
public class JFjumpingBall extends JFrame{
?
???????? /**
???????? ?* @param args
???????? ?* @throws IOException
???????? ?*/
???????? public static void main(String[] args) throws IOException {
?????????????????? // TODO Auto-generated method stub
?
?????????????????? JFjumpingBall ballgame = new JFjumpingBall();
?????????????????? ballgame.JFshow();
???????? }
????????
???????? public void JFshow(){
?????????????????? //建立一个窗体:
???????? ?? double width = Toolkit.getDefaultToolkit().getScreenSize().width;//定义width获取显示器在该分辨率下的宽
???????? ?? double height = Toolkit.getDefaultToolkit().getScreenSize().height;//定义height获取显示器在该分辨率下的高
???????? ?? this.setSize((int)width,(int)height);//设置窗体大小
???????? ?? this.setTitle("小鱼躲气泡");//设置标题
???????? ?? this.setLocationRelativeTo(null);//设置窗口居中
???????? ?? this.setDefaultCloseOperation(3);//设置关闭窗口程序停止运行
???????? ?? this.setLayout(new BorderLayout());//设置窗体布局(边框式布局)
?????????????????? JPanel northPanel = new JPanel();//北部面板
?????????????????? JPanel jpanel = new JPanel();//面板
?????????????????? JPanel southjpanel = new JPanel();//南部面板
?????????????????? northPanel.setLayout(new BorderLayout());//设置北部面板布局方式
?????????????????? JPanel northLeftPanel = new JPanel();//北部面板左边
?????????????????? JPanel northRightPanel = new JPanel();//……右边
?????????????????? JPanel northCenterPanel = new JPanel();//……中间
?????????????????? northLeftPanel.setSize(100,100); //
?????????????????? northRightPanel.setSize(100, 100);//
?????????????????? JButton jbplay = new JButton("Play");//放置游戏按钮(开始游戏)
?????????????????? JButton jbPause = new JButton("Pause");//放置游戏按钮(游戏暂停)
?????????????????? JButton jbAdd? = new JButton("Add");//放置游戏按钮(增加)
?????????????????? JButton deletone = new JButton("Delet one ball");//放置游戏按钮(减少一个小球)
?????????????????? northCenterPanel.add(jbplay);//将按钮放置在北部面板的中间面板
?????????????????? northCenterPanel.add(jbPause);//同上
?????????????????? northCenterPanel.add(jbAdd);//同上
?????????????????? northCenterPanel.add(deletone);//同上
?????????????????? northPanel.add(northCenterPanel,BorderLayout.CENTER);//将北部面板的各部分面板加在北部面板里
?????????????????? northPanel.add(northLeftPanel,BorderLayout.WEST);//
?????????????????? northPanel.add(northRightPanel,BorderLayout.EAST);//
?????????????????? this.add(northPanel,BorderLayout.NORTH);//布置窗体
?????????????????? this.add(jpanel,BorderLayout.CENTER);//
?????????????????? this.add(southjpanel,BorderLayout.SOUTH);//
?????????????????? this.setJMenuBar(JMenuline(jpanel));//给窗体添加菜单栏(菜单栏方法:JMenuline;方法声明在下部)
?????????????????? this.setVisible(true);//设置窗体可见
?????????????????? //List<BallThread> allBall = new ArrayList<BallThread>();//
??????????????????
ActionListener l = new ActionListener(jpanel);//动作监听器
?????????????????? jbplay.addActionListener(l);//jbplay添加动作监听器
?????????????????? jbPause.addActionListener(l);//jbPause添加动作监听器
?????????????????? jbAdd.addActionListener(l);//jbAdd添加动作监听器
?????????????????? deletone.addActionListener(l);//delete添加动作监听器
//添加监听器部分属于第四点的知识点,只做的界面的可以吧这些删掉
???????? }
???????? public JMenuBar JMenuline(JPanel jpanel){
???????? ?? JMenuBar jme =new JMenuBar();//定义菜单栏
???????? ?? JMenu menuF = new JMenu("文件(F)");//菜单栏按钮
???????? ?? JMenuItem jmiFO = new JMenuItem("保存(save)");//定义菜单栏按钮的选项(保存)
???????? ?? JMenuItem jmiIO = new JMenuItem("读取(login)");//定义菜单栏按钮的选项(读取)
???????? ?? foListener listener = new foListener();//定义保存监听器
???????? ?? ioListener listener2 = new ioListener(jpanel);//定义读取监听器
???????? ?? jmiFO.addActionListener(listener);//将保存监听器加在jmiFO
???????? ?? jmiIO.addActionListener(listener2);//将读取监听器加在jmiIO
???????? ?? menuF.add(jmiFO);//将(保存)菜单栏按钮加在menuF里
???????? ?? JSeparator jSeparator = new JSeparator();//定义水平分割线
???????? ?? menuF.add(jSeparator);//将水平分割线加在menuF里
???????? ?? menuF.add(jmiIO);//将(读取)菜单栏按钮加在menuF里
???????? ?? jme.add(menuF);//讲此菜单栏按钮加在jme里
???????? ?? return jme;
???????? }
?
}