麦当劳点餐系统———————新手勿喷

点餐系统

基于java swing的麦当劳点餐系统
没有什么很难的点,只是用了一点GridBagLayout布局

完整代码如下

2019.4.21 更新了小票功能,写入文件

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class DIANCAN extends JFrame implements ActionListener {
    JTextField text1;//汉堡15/
    JLabel label1;
    JTextField text2;//鸡腿12/
    JLabel label2;
    JTextField text3;//可乐5/
    JLabel label3;
    JTextField text7;//鸡翅/4
    JLabel label7;
    JTextField text4;//应付价钱
    JLabel label4;
    JTextField text5;//顾客付的价钱
    JLabel label5;
    JTextField text6;//找零
    JLabel label6;
    //是否有优惠卷//8折
    String path=System.getProperty("user.dir")+"\\src\\pan\\xu\\";//文件路径
    public DIANCAN(){
        super();
        this.setSize(600,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("麦当劳结算系统");
        center();
        GridBagLayout layout=new GridBagLayout();
        this.setLayout(layout);
        GridBagConstraints constraints=new GridBagConstraints();
        text1=init_text(text1);
        text2=init_text(text2);
        text3=init_text(text3);
        text4=init_text(text4);
        text5=init_text(text5);
        text6=init_text(text6);
        text6.setEditable(false);
        text7=init_text(text7);
        label1=new JLabel("汉堡/15");
        label2=new JLabel("鸡腿/12");
        label3=new JLabel("可乐/5");
        label7=new JLabel("鸡翅/4");
        label4=new JLabel("应付价钱");
        label5=new JLabel("实付价钱");
        label6=new JLabel("找零");
        add_to_constratints(label1,constraints,0,0,1,1);
        add_to_constratints(label2,constraints,6,0,1,1);
        add_to_constratints(text1,constraints,1,0,1,1);
        JLabel label11=new JLabel();
        ImageIcon icon11=new ImageIcon(path+"汉堡.png");
        label11.setIcon(icon11);
        add_to_constratints(label11,constraints,2,0,2,2);

        add_to_constratints(text2,constraints,7,0,1,1);
        JLabel label22=new JLabel();
        ImageIcon icon22=new ImageIcon(path+"鸡腿.png");
        label22.setIcon(icon22);
        add_to_constratints(label22,constraints,8,0,2,2);

        add_to_constratints(label3,constraints,0,2,1,1);
        add_to_constratints(label7,constraints,6,2,1,1);
        add_to_constratints(text3,constraints,1,2,1,1);
        ImageIcon icon33=new ImageIcon(path+"可乐.png");
        JLabel label33=new JLabel();
        label33.setIcon(icon33);
        add_to_constratints(label33,constraints,2,2,2,2);

        add_to_constratints(text7,constraints,7,2,1,1);
        ImageIcon icon77=new ImageIcon(path+"鸡翅.png");
        JLabel label77=new JLabel();
        label77.setIcon(icon77);
        add_to_constratints(label77,constraints,8,2,2,2);

        add_to_constratints(label4,constraints,0,4,1,1);
        add_to_constratints(label5,constraints,6,4,1,1);
        add_to_constratints(text4,constraints,1,4,1,1);
        add_to_constratints(text5,constraints,7,4,1,1);
        add_to_constratints(label6,constraints,1,6,1,1);
        add_to_constratints(text6,constraints,2,6,2,1);
        Timer timer=new Timer(1000,this);
        timer.start();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e){
        int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0;
            if("".equals(text1.getText()))
                num1=0;
            else
                num1=Integer.parseInt(text1.getText());
            if("".equals(text2.getText()))
                num2=0;
            else
                num2=Integer.parseInt(text2.getText());
            if("".equals(text3.getText()))
                num3=0;
            else
                num3=Integer.parseInt(text3.getText());
            if("".equals(text7.getText()))
                num4=0;
            else
                num4=Integer.parseInt(text7.getText());
            if("".equals(text5.getText()))
                num5=0;
            else
                num5=Integer.parseInt(text5.getText());
            text4.setText((15*num1+12*num2+5*num3+4*num4)+"");
            if(num5==0)
                ;
            else {
                num6 = num5 - (15 * num1 + 12 * num2 + 5 * num3 + 4 * num4);
                if(num6>0)
                    text6.setText(num6+"");
            }
            if(num5!=0){
                File file=new File(path+"doc.txt");
                try{
                    OutputStream stream=new FileOutputStream(file);
                    String str="汉堡 ----------15/个"+num1+"\n鸡腿 ----------12/个"+num2+"\n可乐 ----------5/杯"+num3+"\n鸡翅 ----------4/个"+num4+
                            "\n应付钱 ----------"+(15*num1+12*num2+5*num3+4*num4)+"\n实付钱 ----------"+num5+
                            "\n找零 ----------"+num6;
                    byte[]data=str.getBytes();
                    stream.write(data);

                }
                catch(Exception e1){
                    e1.printStackTrace();
                }
            }
    }

    void add_to_constratints(Component component,GridBagConstraints constraints,int x,int y,int w,int h){
        constraints.gridx=x;
        constraints.gridy=y;
        constraints.gridwidth=w;
        constraints.gridheight=h;
        this.add(component,constraints);
    }

    void center(){//将窗口放于屏幕中间
        Toolkit kit=Toolkit.getDefaultToolkit();
        Dimension size=kit.getScreenSize();
        this.setLocation(size.width/2-300,(size.height/2-300));
    }

    JTextField init_text(JTextField text){
        text=new JTextField(10);
        return text;
    }

    public static void main(String[]args){
        DIANCAN e=new DIANCAN();
    }
}


运行截图
1

2

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了编写麦当劳系统,您可以使用EasyX图形库和C ++编程。下面是一个简单的示例程序,展示了如何使用EasyX绘制窗口并获取用户的输入,并使用C ++处理并显示数据。 ```cpp #include <graphics.h> #include <conio.h> #include <iostream> #include <string> using namespace std; int main() { // 初始化窗口 initwindow(800, 600); // 绘制背景 setbkcolor(WHITE); cleardevice(); // 绘制标题 settextcolor(BLACK); settextstyle(40, 0, _T("微软雅黑")); outtextxy(250, 50, _T("欢迎来到麦当劳系统")); // 绘制菜单 settextcolor(BLACK); settextstyle(25, 0, _T("微软雅黑")); outtextxy(100, 150, _T("1.汉堡")); outtextxy(100, 200, _T("2.薯条")); outtextxy(100, 250, _T("3.可乐")); // 获取用户输入 int x = 0; int y = 0; while (true) { if (_kbhit()) { int ch = _getch(); if (ch == 49) // 用户按下1 { x = 1; break; } else if (ch == 50) // 用户按下2 { x = 2; break; } else if (ch == 51) // 用户按下3 { x = 3; break; } } } // 显示用户选择 string selected; if (x == 1) { selected = "汉堡"; } else if (x == 2) { selected = "薯条"; } else if (x == 3) { selected = "可乐"; } settextcolor(BLACK); settextstyle(25, 0, _T("微软雅黑")); outtextxy(100, 350, _T("您选择了:")); outtextxy(300, 350, _T(selected.c_str())); // 等待用户响应 while (!_kbhit()) { delay_fps(60); } // 关闭窗口 closegraph(); return 0; } ``` 在这个例子中,我们使用`initwindow`函数初始化窗口,使用`outtextxy`函数绘制文本,使用`_kbhit`和`_getch`函数获取用户的输入,使用`closegraph`函数关闭窗口。在用户选择完品后,我们将所选内容显示在屏幕上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值