java实现可视化界面肯德基(KFC)点餐系统代码实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java实现可视化界面肯德基(KFC)点餐系统代码实例

  2021-04-03 我要评论

一、题目

使用java实现可视化KFC点餐系统。

二、题目分析

根据java中的用户图形界面包中的各个类设计界面。利用JFrame提供最大的容器,然后设计各个面板,各个面板中添加所需要的组件,本程序中需要对按钮组件添加监听者,当按下按钮之后做出相应的相应。

对于程序运行显示的第一个界面由一个继承于JFrame的类run类在构造函数中设计并通过函数setVisible(true)显示在界面上,界面上有一个按钮“点餐饮”,当此按钮按下时触发响应函数,进入点餐界面,然后通过点击点餐界面各食物的按钮“+”“-”触发响应改变用户类client的对象的相对应变量的数量并且刷新界面,此界面还有食物种类的四个按钮“汉堡”“小吃”“饮品”“套餐”,同样点击按钮触发响应刷新出对应的界面,之后设计了两种付款模式,一种是现金付款,使用文本框组件类JTextField产生组件,在此组件中输入你的支付金额,另外是扫二维码支付,只需一个标签组件JLabel中加入图片即可。

三、程序代码

由于初次使用java设计可视化界面的程序,没有设计好导致源代码繁琐,以下列出部分代码

主函数类:

import java.awt.*;
import java.io.Serializable;
import javax.swing.JFrame;
public class run implements Serializable{
	static String title = "KFC";
	static Beginning begin = new Beginning(title);
	static Order order=new Order(title);
	static JFrame[] frames= {begin,order};
	static saoma sao=new saoma("扫码支付");					//二维码窗口
	static Xiadan xiadan=new Xiadan("订单信息");				//订单信息窗口
	public static void main(String args[]) throws InterruptedException {

		for (int index = 0; index < frames.length; index++) {
			frames[index].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frames[index].setPreferredSize(new Dimension(1000,900));
      screencentre(frames[index]);

			frames[index].pack();
		}
		frames[0].setVisible(true);

	}
  //使框架显示在屏幕中央
	public static void screencentre(JFrame frame) {

		Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包

		Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸

		int screenWidth = screenSize.width; // 获取屏幕的宽

		int screenHeight = screenSize.height; // 获取屏幕的高

		
		frame.setLocation(screenWidth/4, screenHeight/15);// 设置窗口居中显示
	}
}

程序运行第一个界面:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Beginning extends JFrame{
	JPanel panel = new JPanel();								
	JLabel title = new JLabel("欢迎光临KFC",JLabel.CENTER);	//标签组件
	JLabel im = new JLabel(new ImageIcon("images/1.jpg"));	//标签组件
	JButton order=new JButton(new ImageIcon("images/4.png"));	//按钮组件
	public Beginning(String name)
	{
		super(name);
		setLayout(new GridLayout(4,1));						//设置布局
		title.setFont(new Font("KFC",Font.BOLD,50) );			//设置字体
		add(title);												//添加组件
		add(im);
		order.addActionListener(new BeginLis());				//添加监听者
		order.setBorderPainted(false);							//去边界
		JPanel jpanel2=new JPanel();
		jpanel2.setLayout(new GridLayout(1,3));
		jpanel2.add(new JPanel());
		jpanel2.add(order);
		jpanel2.add(new JPanel());
		add(jpanel2);
		//panel.add(title);
		//getContentPane().add(panel);
	}
	private class BeginLis implements ActionListener {
	  public void actionPerformed(ActionEvent e) {
	  	setVisible(false);
    	run.frames[1].setVisible(true);
		}
	}
}

二维码支付窗口:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class saoma extends JFrame{
	JPanel jpanel=new JPanel();
	JButton buttonwancheng=new JButton("支付完成");
	public saoma(String name) {
		super(name);
		JLabel jlabel=new JLabel(new ImageIcon("images/saoma.png"));
		jpanel.add(jlabel);
		this.setSize(700, 700);
		this.setLocation(300, 200);
		this.add(jpanel);
		this.add(buttonwancheng,BorderLayout.SOUTH);

		buttonwancheng.addActionListener(e->{
			run.sao.setVisible(false);
			run.xiadan.setVisible(false);
			run.frames[1].setVisible(false);
			System.exit(-1);
		});
	}
}

现金支付窗口:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Xianjin extends JFrame{
	JPanel jpanel=new JPanel();

	JLabel jlabel=new JLabel("掏出整钱");
	JTextField in=new JTextField(20);
	JButton buttonzhifu=new JButton("支付");
	public Xianjin(String name) {
		super(name);
		this.setLayout(new GridLayout(2,1));
		this.setSize(700, 450);
		this.setLocation(300, 200);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		JTextArea show=new JTextArea(24,20);
		JScrollPane scrollpane=new JScrollPane(show);
		show.setEditable(false);
		show.append("您需要支付"+run.order.client1.num+"元"+'\n'+"请在下方输入框输入金钱进行支付以及找零:\n");
		this.add(scrollpane);
		jpanel.add(jlabel);
		jpanel.add(in);
		jpanel.add(buttonzhifu);
		this.add(jpanel);
		buttonzhifu.addActionListener(e->{
			String content=in.getText();
			float jinqian=Integer.parseInt(content);
			float zhaoling=jinqian-run.order.client1.num;
			if(jinqian<run.order.client1.num)
			{
				show.append("您给的钱不够支付,请重新支付\n");
			}
			else
				show.append("您支付了"+jinqian+"元,找您"+zhaoling+"元\n"+"本次订单完成\n");
			in.setText("");
		});
	}
}

下单窗口:

 import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  public class Xiadan extends JFrame{
  	JButton buttonxianjinzhifu=new JButton(new ImageIcon("images/xianjinzhifu.png"));
  	JButton buttonzhifubaosaoma=new JButton(new ImageIcon("images/zhifubaosaoma.png"));
  	JTextArea show=new JTextArea(24,20);
  	public Xiadan(String name)
  	{
  		super(name);
  
  		}
  	public void s() {
  		this.setLayout(new GridLayout(2,1));
  		this.setSize(700, 450);
  		this.setLocation(300, 200);
  
  		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  	
  		JScrollPane scrollpane=new JScrollPane(show);
  		show.setEditable(false);
}

四、测试以及运行结果

程序运行界面:

点击“点餐饮”:

 

点击各食物的“+”:

 

点击“立即购买”:

 

点击“现金支付”并在文本框输入30后点击“支付”:

 

点击“扫码支付”:

 

 五、总结

初次使用java设计可视化程序,对用户图形界面方面的只是很是陌生,本应该结合设计模式使得程序代码结构更加清晰,由于第一次接触图形界面以及设计模式应用实践很少,这次程序设计没有结合好设计模式,这次程序代码也相对很是糙,在写的过程中遇到各种问题,图形界面设计总是不在理想状态,慢慢的上网查资料以及查阅书本大多数得到了解决,只不过程序还存在一些小问题,一点点积累经验吧。

有了这次实践的经验,下次一定会做出稍漂亮的界面以及设计更加结构清晰的代码。

以上所述是小编给大家介绍的java肯德基点餐系统详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们