初识JFrame

JFrame 类就是做这个的——它是一个容器,允许您把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。

JFrame 实际上不仅仅让您把组件放入其中并呈现给用户。比起它表面上的简单性,它实际上是 Swing 包中最复杂的组件。为了最大程度地简化组件,在独立于操作系统的 Swing 组件与实际运行这些组件的操作系统之间,JFrame 起着桥梁的作用。JFrame 在本机操作系统中是以窗口的形式注册的,这么做之后,就可以得到许多熟悉的操作系统窗口的特性:最小化/最大化、改变大小、移动。但是对于本教程的目标来说,把 JFrame 当作放置组件的调色板就足够了。可以在 JFrame 上调用的一些修改属性的方法是:

  • get/setTitle()获取/设置帧的标题。
  • get/setState()获取/设置帧的最小化、最大化等状态。
  • is/setVisible()获取/设置帧的可视状态,换句话说,是否在屏幕上显示。
  • get/setLocation()获取/设置帧在屏幕上应当出现的位置。
  • get/setSize()获取/设置帧的大小。
  • add()将组件添加到帧中。

import java.awt.event.*;
import javax.swing.*;
public class tryWindowAdapter extends JFrame {
public tryWindowAdapter() {
  super("try to close") ;
  this.setSize(400,400) ;
  this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) ;
  this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
     System.out.println("haha") ;
     System.exit(0) ;
    }
   }
   );
  this.setVisible(true) ;
  }

public static void main(String args[]) {
  new tryWindowAdapter() ;  
}
}

//关闭时提示

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;

public class tryWindowAdapter extends JFrame {
public tryWindowAdapter() {
  super("try to close") ;
  this.setSize(400,400) ;
  this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) ;
  this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
     if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog((Frame)e.getSource(),"really quit?")) {
      System.exit(0) ;
     }else
      return ;
    }
   }
   );
  this.setVisible(true) ;
  }

public static void main(String args[]) {
  new tryWindowAdapter() ;  
}
}

解析下列代码:package test; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.util.Random; public class Game extends JFrame implements KeyListener, ActionListener, MouseListener { private BufferedImage snake= GameUtil.load(Constant.Snake); private BufferedImage snakeHead = GameUtil.load(Constant.SnakeHead); private BufferedImage bombIMG = GameUtil.load(Constant.bombIMG); private BufferedImage backGround = GameUtil.load(Constant.backGruond); int slong = 2;//蛇当前长度 //蛇坐标 int[] Snakex = new int[100]; int[] Snakey = new int[100]; int bombCount=3; int[] bombx=new int [bombCount]; int[] bomby=new int [bombCount]; int fx = 1;//蛇的方向 0-左 1-右 2-上 3-下 Timer timer = new Timer(100, this);//设置定时器,每100毫秒一次 //食物位置 int foodx; int foody; Random random = new Random();//随机数,随机位置生成食物 int started = 0;//游戏信息 0-未开始 1-开始 2-结束 //--------------------------------------------------------------------------------------------------------------------- //窗体 public void myJFrame() { this.setTitle("贪吃蛇"); //标题 this.setSize(800, 610); //窗口大小 this.setResizable(false); //窗口是否可以改变大小=否 this.setDefaultCloseOperation(Game.EXIT_ON_CLOSE); //窗口关闭方式为关闭窗口同时结束程序 int width = Toolkit.getDefaultToolkit().getScreenSize().width; //获取屏幕宽度 int height = Toolkit.getDefaultToolkit().getScreenSize().height; //获取屏幕高度 // System.out.println("宽度:"+width);//测试 // System.out.println("高度:"+height);//测试 this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 this.setFocusable(true); this.addKeyListener(this); this.setVisible(true); //窗口是否显示=是 // 蛇的初识位置 Snakex[0] = 60; Snakey[0] = 100; Snakex[1] = 40; Snakey[1] = 100; // 随机食物的初识位置 foodx = random.nextInt(39); foody = random.nextInt(22); foodx = foo
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值