蜗牛式学习Java--基础篇--综合案例拼图游戏-界面绘制

 

 

  • 创建窗体
  • 代码:
    • package com.ktestdemo;
      
      import javax.swing.*;
      
      // 继承JFrame
      public class PictureFrame extends JFrame {
          // 定义构造方法
          public PictureFrame() {
              // 调用窗体初始化方法
              initFrame();
              // 设置窗体可见
              this.setVisible(true);
          }
      
          public void initFrame() {
      
              // 设置窗体大小
              this.setSize(960,565);
              // 设置标题
              this.setTitle("动漫拼图");
              // 窗口居中
              this.setLocationRelativeTo(null);
              // 设置关闭时退出应用程序
              this.setDefaultCloseOperation(3);
              // 设置窗体位于其他应用之上
              this.setAlwaysOnTop(true);
              // 取消默认布局
              this.setLayout(null);
      
          }
      
      
      }
  •  
  •  效果:

 

  • 窗体控件绘制:
  • 代码:
    •     //定义绘制面板方法
          public void paintView(){
              // 设置标题图片   C:\kk\IdeaProjects\javase_code\ktest-picture-puzzle\images\title.png
              JLabel jLabelTitle = new JLabel(new ImageIcon("ktest-picture-puzzle\\images\\title.png"));
              jLabelTitle.setBounds(354,27,232,57);
              this.add(jLabelTitle);
      
              //设置二维数组,标记16张图片
              int[][] im = {
                      {1,2,3,4},
                      {5,6,7,8},
                      {9,10,11,12},
                      {13,14,15,16}
              };
      
              // 设置面板图片(拼图区)
              JPanel jPanel = new JPanel();
              jPanel.setBounds(150,114,360,360);
              jPanel.setLayout(null);
      
              // 循环获取二维数组里的值,作为图片编号
              for(int i = 0; i<im.length;i++){
                  for (int j=0;j<im[i].length;j++){
                      // 创建JLabel对象,加载图片(图片名称0~16,后缀.png,这里循环获取数组值,在进行字符串拼接)
                      JLabel jLabelXt=new JLabel(new ImageIcon("ktest-picture-puzzle\\images\\"+im[i][j]+".png"));
                      jLabelXt.setBounds(j*90,i*90,90,90);
                      // 把每张小图片都添加拼图区上
                      jPanel.add(jLabelXt);
                  }
              }
              // 把拼图区添加到窗体上
              this.add(jPanel);
      
              // 设置参照图
              JLabel canzhaotu = new JLabel(new ImageIcon("ktest-picture-puzzle\\images\\canzhaotu.png"));
              canzhaotu.setBounds(574,114,122,121);
              this.add(canzhaotu);
      
              // 设置操作方向按钮
              JButton shangBt=new JButton(new ImageIcon("ktest-picture-puzzle\\images\\shang.png"));
              shangBt.setBounds(732,265,57,57);
              this.add(shangBt);
      
              JButton xiaBt=new JButton(new ImageIcon("ktest-picture-puzzle\\images\\xia.png"));
              xiaBt.setBounds(732,347,57,57);
              this.add(xiaBt);
      
              JButton zuoBt=new JButton(new ImageIcon("ktest-picture-puzzle\\images\\zuo.png"));
              zuoBt.setBounds(650,347,57,57);
              this.add(zuoBt);
      
              JButton youBt=new JButton(new ImageIcon("ktest-picture-puzzle\\images\\you.png"));
              youBt.setBounds(813,347,57,57);
              this.add(youBt);
      
              //设置求助,重置按钮
              JButton helpBt=new JButton(new ImageIcon("ktest-picture-puzzle\\images\\qiuzhu.png"));
              helpBt.setBounds(626,444,108,45);
              this.add(helpBt);
      
              JButton czBt=new JButton(new ImageIcon("ktest-picture-puzzle\\images\\chongzhi.png"));
              czBt.setBounds(786,444,108,45);
              this.add(czBt);
      
              // 设置背景图
              JLabel bjt = new JLabel(new ImageIcon("ktest-picture-puzzle\\images\\background.png"));
              bjt.setBounds(0,0,960,530);
              this.add(bjt);
      
          }
  • 效果:

 

posted @ 2023-02-11 23:14  沐沂  阅读(33)  评论(0)    收藏  举报