关于JAVA界面——商品购买的简单实现。(图片背景)

以下为笔者的第一篇文章,不喜勿喷。 

以下的代码记录了笔者一些查错的过程,读者有兴趣的可以看看,在我看来这个程序别的功能其实没啥,主要就是一个商品购买的背景实现了,后期因为一些个人原因没再帮这个程序进行一个完善,可能后期有时间会倒回来做这个项目也说不准。

目前只是实现了商品的购买,商品的添加与商城的界面设计都需要再记事本实现,这也是这个程序所存在的不足。 

以下附上笔者的一张运行图片。

 

对了一下简单说下笔者当时遇到的一些问题,为后者提供一些参考:

1.注意记事本的编码格式与编译器的编码格式,往往这就是产生乱码的情况

2.文件以字节流读取出来以后转为字符串类型的数据,在其末尾不能进行一个添加,这个我还没有找到相关问题的来源,只是利用了一个在其文件末尾加入,垃圾数据的方式进行一个解决。具体原因,后期完善。

 

      本程序只需在D盘下创建一个文件以kese为文件名称的文件夹再在后期加入三个记事本文件(文件类型与使用方法如下),将代码部分考入编译器,执行即可。

关于商品的添加:

在kese1,kese3,kese4,kese.jpg都需要进行相应的更改,对于kese1,在原有的商品基础上加入商品,在上一行回车换行以后,输入要加入的商品名称,以三个tab键为分割输入商品价格,而后的两个文本文档为价格和商品名称,都以空格为分割添加即可。

对于kese.jpg:

这个是作为购买的界面的图片,命名方式为kese,其图片以jpg为图片后缀。直接丢在D盘的kese文件夹下即可。

对于kese1:

只需创建一个以txt为后缀的文本文件,而后内容自行设定,其主要作用为商城的显示,这个可自行设置。

对于kese3:

同样创建一个以txt为后缀的文本文件,内部主要存储商品内容,其存储格式为商品名加一个空格再加商品名的形式。对于知道怎么实现的大佬可对其读取形式进行一个更改,文件主要以字节流的形式进行一个读取。商品名称以商城名称的顺序存储,笔者当时是本想着用字符数组的形式进行一个存放,但考虑到后期如果商品名称多了,那就不妥了。

对于kese4:

存储格式与kese3相同,存储的主要内容为商品的价格其存储顺序依旧是商城中的顺序。

对于商城购买使用操作说明:

只需在第二个输入框以空格为分割且在不以回车换行结束的情况下,输入商品编号,以旁边的结算按钮为一次结算。

每次结算后都可手动利用鼠标操作对文本框内的内容进行一次清空。

 

在做这个的时候因为种种原因,还有很多笔者认为的功能都还没有进行一个实现,比如说商品类别的添加与管理者的登录,以后一些相应数据得出后的一些清除,对了,笔者还认为,界面可以在做的简洁一些,这些遗憾可能后期会有一个实现。

import java.io.*;
import java.util.*;
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.awt.*;
/*
 * 商店  文件复写  记事本  返回String
 * 第一次先实现简单的读出操作
 * 后期的另外考虑
 * goods 为物品
 */
class Shape{   
	String goods;
	public Shape() {
		this.read();
	}
	private void read() {
		String file = "d:"+File.separator+"kese"+File.separator+"kese1.txt";
		File f = new File(file);
		InputStream input = null;
		try {
			input = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		byte b[] = new byte[1024];
		try {
			input.read(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		goods = new String(b);
	}
	
	public String fan() {
		return goods;
	}
}

/*
 * 商店文本框
 */
class Textone{
	JTextArea text1;
	public Textone(JTextArea text1) {
		this.text1 = text1;
		this.chuli();
	}
	private void chuli() {
		Shape sh = new Shape();
		text1.append(sh.fan());
	}
	public JTextArea fan() {
		return text1;
	}
}
/*
 * 第二个文本框的设置
 * 实现键盘监听
 * 
 */
class Texttwo {
	JTextArea text2;
	JTextArea text3;
	JTextArea text4;
	JButton an;
	int wuping[];
	public Texttwo(JTextArea text2,JButton an,JTextArea text3,JTextArea text4) {
		this.text2 = text2;
		this.an = an;
		this.text3 = text3;
		this.text4 = text4;
		this.chuli();
	}
	private void chuli() {
		//String s = text2.getText();
		an.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(arg0.getSource() == an) {
					
					String shu = text2.getText();
					String shu1[] = shu.split(" ");
					wuping = new int[shu1.length];
					for(int i=0;i<shu1.length;i++) {
						wuping[i] = Integer.parseInt(shu1[i]);
					}
					//text2.append(text2.getText()+'\n');
					Textthree t3 = new Textthree(text3,wuping);
					Textfour t4 = new Textfour(text4,wuping);
				}
			}
		});
	}
	public JTextArea fan2() {
		return text2;
	}
	public JTextArea fan3() {
		return text3;
	}
	public JTextArea fan4() {
		return text4;
	}
	public int[] fanshuju() {
		return wuping;
	}
}

/*
 * 第三个文本框的设置
 * 实现键盘监听
 * 
 */
class Textthree {
	JTextArea text3;
	int wuping[];
	String goods[];
	public Textthree(JTextArea text3,int wu[]) {
		this.text3 = text3;
		this.wuping = wu;
		this.read();
		this.chuli();
	}
	private void chuli() {
		text3.append("此时您的购物车内有以下物品:\n"
				+ "是否再次购买,如需购买请在输入处追加!谢谢!\n");
		//String s = text2.getText();
		for(int i=0; i<wuping.length;i++) {
			for(int j=0;j<=goods.length;j++) {
				if(wuping[i]-1 == j) {
					text3.append(goods[j]+"\n");
				}
			}
		}
	}
	
	private void read() {
		String file = "d:"+File.separator+"kese"+File.separator+"kese3.txt";
		File f = new File(file);
		InputStream input = null;
		try {
			input = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		byte b[] = new byte[500];
		try {
			input.read(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pp = new String(b);
		//pp = pp+"fff";
		//System.out.println(pp);                    //注意一下那个
		//text3.append(pp+"\n");
		
		goods = pp.split(" ");
//		for(int p0=0;p0<goods.length;p0++) {
//			System.out.println(goods[p0]+"11");
//		}
		//text3.append(goods[0]+"\n");
		//goods = new String(b).split(".");
	}
	
	public JTextArea fan() {
		return text3;
	}
	
}

/*
 * 第四个文本框的设置
 * 实现键盘监听
 * 
 */
class Textfour {
	JTextArea text4;
	int wuping[];
	int jiage[];//= {5,6,7,9,8,5,5};
	int sum;
	public Textfour(JTextArea text4,int wu[]) {
		this.text4 = text4;
		this.wuping = wu;
		this.read();
		this.chuli();
	}
	private void chuli() {
		//String s = text2.getText();
		text4.append("此时您所需要支付以下金额,二维码在旁边自行扫码支付\n不能逃票哦?\n"
				+ "欢迎您的下次光临!!!\n");
		sum=0;
		for(int i=0 ;i<wuping.length;i++) {
			for(int j=0;j<jiage.length;j++) {
				if(wuping[i]-1 == j) {
					sum=sum+jiage[j];
				}
			}
		}
		text4.append(sum+"元\n");
	}
	
	private void read() {
		String file = "d:"+File.separator+"kese"+File.separator+"kese4.txt";
		File f = new File(file);
		InputStream input = null;
		try {
			input = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		byte b[] = new byte[500];
		try {
			input.read(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pp = new String(b);
		//System.out.print(pp);
		//pp = pp+"fff";
		//System.out.print(pp);
		//text4.append(pp+"\n");
		String jiage0[] = pp.split(" ");
		jiage = new int[jiage0.length];
//		for(int p0=0;p0<jiage.length;p0++) {
//			System.out.println(jiage0[p0]+"11");
//		}
		for(int i=0;i<jiage0.length-1;i++) {
			jiage[i] = Integer.parseInt(jiage0[i]);
		}
		
		//text3.append(goods[0]+"\n");
		//goods = new String(b).split(".");
	}
	
	
	public JTextArea fan() {
		return text4;
	}
	
}

/*
 * 总文本框处理,总枢纽
 * 功能:实现第二文本的输入的事件监听同时调用第三四文本框实时显示
 * 		通过实现一个输入以回车为结束的,实时调用显示钱数的功能与结算功能
 * 		后期考虑时间的显示
 */

class Tex{
	JTextArea text1;
	JTextArea text2;
	JTextArea text3;
	JTextArea text4;
	JButton an;
	public Tex() {
		this.set();
	}
	private void set() {
		text1 = new JTextArea(10,10);
		text1.setBounds(100,20, 300, 300);
		text2 = new JTextArea(5,5);
		text2.setBounds(150,400, 200, 200);
		text3 = new JTextArea(10,10);
		text3.setBounds(1000,20, 300, 300);
		text4 = new JTextArea(10,10);
		text4.setBounds(1000,400, 300, 300);
		an = new JButton("结算");
		an.setBounds(400, 500, 100,100);
	}
	public JTextArea fan() {
		//text1.append("\t欢迎光临!\n请在下面的文本框内输入你想要购买的物品编号\n小郭\t五元");
		Textone one = new Textone(text1);
		return one.fan();
	}
	Texttwo two ;
	public JTextArea fan2() {
		two = new Texttwo(text2,an,text3,text4);
		return two.fan2();
	}
	public JTextArea fan3() {
		return two.fan3();
	}
	public JTextArea fan4() {
		return two.fan4();
	}
	public JButton fan5() {
		return an;
	}
}


public class Test{
	public static void main(String []args) {
		JFrame f = new JFrame("商店");
		ImageIcon image = new ImageIcon("d:"+File.separator+"kese"+File.separator+"kese.jpg");
		JLabel lab = new JLabel(image);
		lab.setSize(image.getIconWidth(), image.getIconHeight());
		f.getLayeredPane().add(lab,new Integer(Integer.MIN_VALUE));
		JPanel pan = (JPanel)f.getContentPane();
		pan.setOpaque(false);
		pan.setLayout(null);//new FlowLayout()布局设置极为重要
		Tex kuan = new Tex();
		pan.add(kuan.fan());
		pan.add(kuan.fan2());
		pan.add(kuan.fan3());
		pan.add(kuan.fan4());
		//pan.add(new JButton("sdad"));
		pan.add(kuan.fan5());
		
		f.setSize(image.getIconWidth(), image.getIconHeight());
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}

 

有问题欢迎一起交流。


 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忆林520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值