跟着黑马程序员写小项目:拼图游戏
今天实现了窗口的弹出
明白了整个项目的大概框架,和一些基本的语法
代码如下:
//APP
package ui;
public class App {
public static void main(String[] args) {
//程序的启动路口
//如果想要开启一个界面,就创建谁的对象即可
//new LoginJFrame();
//new RegisterJFrame();
new GameJFrame();
}
}
//GameJFrame
package ui;
import javax.swing.*;
public class GameJFrame extends JFrame {
//JFrame 界面,窗体
//子类也表示界面,窗体
//规定:GameJFrame这个界面表示游戏主界面
//构造方法:
public GameJFrame(){
initJFrame();//ctrl+alt+m:创建构造方法
initJMenuBar();
InitImage();
this.setVisible(true);
}
//初始化图片
private void InitImage() {
//创建一个图片ImageIcon的对象
ImageIcon icon=new ImageIcon("D:\\java\\PTgame\\image\\animal\\animal3\\1.jpg");
//创建一个JLable的对象(管理容器)
JLabel jLabel=new JLabel(icon);
//指定图片位置
jLabel.setBounds(0,0,105,105);
//把管理容器添加到界面中
//this.add(jLabel);
//获取隐藏的容器
this.getContentPane().add(jLabel);
}
private void initJMenuBar() {
//初始化菜单
//创建整个菜单对象
JMenuBar jmenubar=new JMenuBar();
//创建选项(功能 关于我们)
JMenu functionJMenu=new JMenu("功能");
JMenu aboutJMenu=new JMenu("关于我们");
//选项下面的条目对象
JMenuItem replayItem=new JMenuItem("重新游戏");
JMenuItem reLoginItem=new JMenuItem("重新登录");
JMenuItem closeItem=new JMenuItem("关闭游戏");
JMenuItem accountItem=new JMenuItem("公众号");
//将条目添加到选项中
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
aboutJMenu.add(accountItem);
//将选项添加到菜单中
jmenubar.add(functionJMenu);
jmenubar.add(aboutJMenu);
//给整个界面设置菜单
this.setJMenuBar(jmenubar);
}
private void initJFrame() {//ctrl+b跟进跳转到方法
this.setSize(603,680);
//界面标题
this.setTitle("拼图游戏单机版 v1.0");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置关闭模式
this.setDefaultCloseOperation(3);
//windowConstants.EXIT_NO_CLOSE,关掉一个,虚拟机整个结束
//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件
this.setLayout(null);
}
}
//LoginJFrame
package ui;
import javax.swing.*;
public class LoginJFrame extends JFrame {
//登入界面
public LoginJFrame(){
this.setSize(488,430);
this.setTitle("拼图 登录");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置关闭模式
this.setDefaultCloseOperation(3);
//windowConstants.EXIT_NO_CLOSE,关掉一个,虚拟机整个结束
this.setVisible(true);
}
}
//RegisterJFrame
package ui;
import javax.swing.*;
public class LoginJFrame extends JFrame {
//登入界面
public LoginJFrame(){
this.setSize(488,430);
this.setTitle("拼图 登录");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置关闭模式
this.setDefaultCloseOperation(3);
//windowConstants.EXIT_NO_CLOSE,关掉一个,虚拟机整个结束
this.setVisible(true);
}
}