下拉列表框
组件
下拉框组件(JComboBox):点击箭头可以显示其他信息
列表框组件(JList):可以跟列表一样显示信息
滚动窗格组件(JScrollPane):与列表框结合使用,可以滚动显示
应用场景
- 下拉框 选择地区 或者一些单个选项
- 列表框 展示信息 一般是动态扩容
注意事项
列表框可以设置显示信息的个数,并与滚动窗格连用。
【代码实例】
package com.gui;
import javax.swing.*;
import java.awt.*;
/**
* created by admin on 2020/6/15
* 下拉框(JCombobox)
* 列表框(JList)
* 滚动窗口(JScrollpane)
* */
public class JComboboxDemo extends JFrame {
//定义需要用到的组件
private JList jList; //列表
private JComboBox jComboBox; //下拉框
private JScrollPane jScrollPane; //滚动窗口
private JLabel jLabel,jLabel2; //标签
private JPanel jPanel,jPanel2; //面板
public static void main(String[] args) {
JComboboxDemo jComboboxDemo = new JComboboxDemo();
}
public JComboboxDemo(){
//面板
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
//标签
JLabel jLabel1 = new JLabel("所在省");
JLabel jLabel2 = new JLabel("所在市");
String [] province ={" ","湖北","湖南","江苏","河南"}; //静态生成列表内容
JComboBox jComboBox = new JComboBox(province);
String[] city = {"武汉","长沙","南京","郑州","上海","广州"};
//列表框
JList jList = new JList(city);
jList.setVisibleRowCount(4); //设置可视化的行数
//滚动窗口
JScrollPane jScrollPane = new JScrollPane(jList);
this.setLayout(new GridLayout(2,1)); //布局
jPanel1.add(jLabel1);
jPanel1.add(jComboBox);
jPanel2.add(jLabel2);
jPanel2.add(jScrollPane);
this.add(jPanel1);
this.add(jPanel2);
this.setTitle("Java");
this.setBounds(500,250,400,350);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
运行结果:
文本框(JTextbox)
使用标签 + 文本框 + 密码框 + 面板 做的登陆页面
JPanel注意事项:
JPanel也是属于容器类组件,上面可以放置其他组件
JPanel上放置的组件默认的布局是流式布局FlowLayout
package com.gui;
//JTextbox文本框
import javax.swing.*;
import java.awt.*;
public class JTextboxDemo extends JFrame {
public JTextboxDemo() {
//创建面板
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
//创建标签
JLabel jLabel1 = new JLabel("用户名:");
JLabel jLabel2 = new JLabel(" 密 码:");
//创建文本框宽度为10
TextField textField = new TextField(10);
//创建密码框宽度为10
JPasswordField jPasswordField = new JPasswordField(10);
//创建按钮
JButton jButton1 = new JButton("登录");
JButton jButton2 = new JButton("注册");
//网格布局!3行1列
this.setLayout(new GridLayout(3,1));
jPanel1.add(jLabel1);
jPanel1.add(textField);
jPanel2.add(jLabel2);
jPanel2.add(jPasswordField);
jPanel3.add(jButton1);
jPanel3.add(jButton2);
this.add(jPanel1);
this.add(jPanel2);
this.add(jPanel3);
this.setTitle("GUI");
this.setBounds(500,250,350,200);
this.setResizable(false); //绝对定位大小不可修改
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JTextboxDemo();
}
}
运行效果:
用GUI实现简单的贪吃蛇小游戏
思路
- 创建构思界面
- 定义数据
- 把内容画上去
- 监听事件
键盘
事件
【代码实例】
游戏主启动类
package com.snake;
import javax.swing.*;
//游戏主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,720);
frame.setResizable(false); //窗口大小固定
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
游戏界面类
package com.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//游戏的面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//定义蛇的数据结构
int length;//蛇的长度
int[]snakesX=new int[600];//蛇的X坐标25*25
int[]snakesY=new int[500];//蛇的Y坐标25*25
String fx;
//食物的坐标
int foodx;
int foody;
Random random=new Random();
//成绩
int score;
//游戏开始的状态,开始,停止
boolean isStar=false;//默认是不开始
boolean isFail=false;//游戏失败状态
//定时器 以毫秒为单位 1000ms=1s
Timer timer= new Timer(100,this);//100毫秒执行一次
//构造器
public GamePanel(){
init();
//获得焦点和键盘事件
this.setFocusable(true);//获得焦点事件
this.addKeyListener(this);//获得键盘的监听事件
timer.start();//游戏一开始,定时器启动
}
//初始化方法
public void init(){
length=3;
snakesX[0]=100;snakesY[0]=100;//头的坐标
snakesX[1]=75;snakesY[1]=100;//第一个身体的坐标
snakesX[2]=50;snakesY[2]=100;//第一个身体的坐标
fx="R";//初始方向向右
//把食物随机分布在界面上
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
score=0;
}
//绘制面板,我们游戏中的所有东西,都用这个画笔来画
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);//清屏,闪烁
//绘制静态面板
this.setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);//头部广告栏放上去
g.fillRect(25,75,850,600);//默认的游戏界面
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度:"+length,750,30);
g.drawString("分数:"+score,750,55);
//画食物
Data.food.paintIcon(this,g,foodx,foody);
//把小蛇画上去
if(fx.equals("R")){
Data.right.paintIcon(this,g,snakesX[0],snakesY[0]);//蛇的初始化向右,需要通过方向来判断
}else if(fx.equals("L")){
Data.left.paintIcon(this,g,snakesX[0],snakesY[0]);//蛇的初始化向右,需要通过方向来判断
}else if(fx.equals("U")){
Data.up.paintIcon(this,g,snakesX[0],snakesY[0]);//蛇的初始化向右,需要通过方向来判断
}else if(fx.equals("D")){
Data.down.paintIcon(this,g,snakesX[0],snakesY[0]);//蛇的初始化向右,需要通过方向来判断
}
for (int i = 1; i <length ; i++) {
Data.body.paintIcon(this,g,snakesX[i],snakesY[i]);//蛇第一个身体
}
//游戏状态
if(isStar==false){
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",300,300);
}
if(isFail){
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("游戏失败,按下空格开始游戏",300,300);
}
}
//键盘监听事件
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获得键盘的按键是哪一个
if(keyCode==KeyEvent.VK_SPACE){
//如果按下的是空格
if(isFail){
//重新开始
isFail=false;
init();
}else{
isStar=!isStar;//取反操作
}
repaint();//重新绘制
}
//小蛇移动
if(keyCode==KeyEvent.VK_UP){
fx="U";
}else if(keyCode==KeyEvent.VK_DOWN){
fx="D";
}else if(keyCode==KeyEvent.VK_LEFT){
fx="L";
}else if(keyCode==KeyEvent.VK_RIGHT){
fx="R";
}
}
//事件监听,----需要通过固定事件来刷新,1s=10次
@Override
public void actionPerformed(ActionEvent e) {
if(isStar && isFail==false){//如果游戏是开始状态,就让小蛇动起来
//吃食物
if(snakesX[0]==foodx && snakesY[0]==foody){
length++;//长度加一
//分数+10
score=score+10;
//再次随机食物
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
}
//移动
for (int i = length-1; i >0 ; i--) {//后一节移到前一节的位置 snakesX[1]=snakesX[0];
snakesX[i]=snakesX[i-1];//向前移动一节
snakesY[i]=snakesY[i-1];
}
//走向
if(fx.equals("R")){
snakesX[0]=snakesX[0]+25;
if(snakesX[0]>850){ snakesX[0]=25;}//边界判断
}else if(fx.equals("L")){
snakesX[0]=snakesX[0]-25;
if(snakesX[0]<25){ snakesX[0]=850;}//边界判断
}else if(fx.equals("U")){
snakesY[0]=snakesY[0]-25;
if(snakesY[0]<75){ snakesY[0]=650;}//边界判断
}else if(fx.equals("D")){
snakesY[0]=snakesY[0]+25;
if(snakesY[0]>650){ snakesY[0]=75;}//边界判断
}
//失败判定,撞到自己就算失败
for (int i = 1; i < length; i++) {
if(snakesX[0]==snakesX[i] && snakesY[0]==snakesY[i]){
isFail=true;
}
}
repaint();//重画页面
}
timer.start();//定时器开始
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
游戏数据类
package com.snake;
import javax.swing.*;
import java.net.URL;
//游戏数据中心
public class Data {
public static URL headerURL = Data.class.getResource("statics/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static URL upURL = Data.class.getResource("statics/up.png");
public static ImageIcon up = new ImageIcon(upURL);
public static URL downURL = Data.class.getResource("statics/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL leftURL = Data.class.getResource("statics/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL rightURL = Data.class.getResource("statics/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
public static URL bodyURL = Data.class.getResource("statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Data.class.getResource("statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
}
运行效果
视频学习参考教程