`
Cecily
  • 浏览: 57626 次
  • 性别: Icon_minigender_2
  • 来自: 大连
社区版块
存档分类
最新评论

队列的一个小练习

阅读更多

      今天随手写了一个关于队列的小练习,总觉得不是特别贴切,但一时还没有更好的想法,发出来请大家提意见吧。这是一个模仿队列数据存储方式的停车场小程序,车辆可以入站和出站,参考效果如附件图。

车辆进站时的效果:

     

 

车辆出站时的效果:



  

实现的java源码如下:

package ch3队列;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;

public class FinalCar extends JFrame {
	int x=10;		//图片的横坐标
	int y=10;	 	//图片的纵坐标
	int width=100;  //图片的宽度
	int height=120; //图片的高度
	Object queue[];
	int    size=5;
	int    front=0;	//头指针,牺牲一个节点,front及rear均指向此头节点,此节点不存放数据,只用于在代码第97行间接判断队列是否放满
	int    rear=0;  //尾指针
	
	JLabel lblPic=new JLabel(new ImageIcon("car.GIF"));		//定义标签数组,存储汽车图片
	public FinalCar() {
		
		super();
		queue=new Object[5];   //最多放4辆汽车
		this.setIconImage(new ImageIcon("/car_icon.GIF").getImage());
		setTitle("队列演示");
		getContentPane().setLayout(null);
		new FlowLayout();
		final JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 10, 461, 143);
		panel.setLayout(null);//调整为通过坐标控制的布局形式
		final JButton btnIn = new JButton();
		
		//进站
		btnIn.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if(isFull())
				{
					JOptionPane.showMessageDialog(null, "停车场已满,请等待!");
				}
				else
				{
					rear=(rear+1)%size;
					x=460-rear*(width+5);
					queue[rear]=new JLabel(new ImageIcon("car.GIF"));
					((Component) queue[rear]).setBounds(x, y, width, height);
					panel.add((Component) queue[rear]);
					repaint();         //如果是null式布局,则需要repaint
					//setVisible(true);//如果是流式布局,setVisible即可显示图片更新
				}
			}
		});
		
		getContentPane().add(panel);
		btnIn.setText("进站");
		btnIn.setBounds(76, 170, 106, 28);
		getContentPane().add(btnIn);

		final JButton btnOut = new JButton();
		//出站
		btnOut.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				JLabel lblPic=null;
				if(isEmpty())
				{
					JOptionPane.showMessageDialog(null, "停车场已空,没有车辆!");
					rear=0;
				}
				else
				{
					front=(front+1)%size;
					lblPic=(JLabel) queue[front];
					panel.remove(lblPic);		//移除汽车
					repaint();
				}
				
			}
		});
		btnOut.setText("出站");
		btnOut.setBounds(208, 170, 106, 28);
		getContentPane().add(btnOut);
		this.setSize(500, 250);
		this.setVisible(true);
	}
	//判断停车场是否已经满了
	public boolean isFull()
	{
		boolean full=true;
		if((this.rear+1)%size==front)	//停车场满了
		{
			full=true;
		}
		else
		{
			full=false;
		}
		return full;
	} 
	//判断停车场是否是空的
	public boolean isEmpty()
	{
		boolean empty=true;
		if(this.rear==this.front)		//停车场已经空了
		{
			empty=true;
			rear=0;
			front=0;
		}
		else
		{
			empty=false;
		}
		return empty;
	} 

	public static void main(String[] args) {
		FinalCar frame=new FinalCar();
		
	}
}

 

其实对最终的演示效果不太满意,但在界面的表现上来说实在不想花更多的时间,只希望能基本表现出队列的一些使用方法,请大家多提意见。

 

  • 大小: 20.7 KB
  • 大小: 17.7 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics