import java.util.ArrayList;
创新互联公司是一家集网站建设,宣汉企业网站建设,宣汉品牌网站建设,网站定制,宣汉网站建设报价,网络营销,网络优化,宣汉网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMapString, String hm=new HashMapString, String();
float totlePrice=0;
//添加book到购物车
public void addBook(String bookId,String bookQuantity){
if(hm.containsKey(bookId)){
int value=Integer.parseInt(hm.get(bookId));
value+=Integer.parseInt(bookQuantity);
hm.put(bookId, value+"");
}else{
hm.put(bookId, bookQuantity);
}
}
//修改数量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//获取购物车的所有信息 并计算总价
public ArrayListBookBean getShoppingCart(){
ArrayListBookBean al=new ArrayListBookBean();
IteratorString i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空总价,防止无限累计
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//获取总价
public float getTotlePrice(){
return totlePrice;
}
//根据ID获取数量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空购物车
public void clear(){
hm.clear();
}
//删除购物车中的一本书
public void deleteById(String id){
hm.remove(id);
}
}
用java Swing做的?给你个代码,还有运行结果图
package my.test.main;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Purchase extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel label_9;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Purchase frame = new Purchase();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Purchase() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u5546\u54C11");
label.setBounds(56, 40, 54, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("\u5546\u54C12");
label_1.setBounds(56, 70, 54, 15);
contentPane.add(label_1);
JLabel label_2 = new JLabel("\u5546\u54C13");
label_2.setBounds(56, 100, 54, 15);
contentPane.add(label_2);
JLabel label_3 = new JLabel("\u5546\u54C14");
label_3.setBounds(56, 130, 54, 15);
contentPane.add(label_3);
JLabel label_4 = new JLabel("\u5546\u54C15");
label_4.setBounds(56, 160, 54, 15);
contentPane.add(label_4);
JLabel label_5 = new JLabel("\u5546\u54C1\u540D\u79F0");
label_5.setBounds(56, 7, 54, 15);
contentPane.add(label_5);
JLabel label_6 = new JLabel("单价");
label_6.setBounds(179, 7, 54, 15);
contentPane.add(label_6);
JLabel label_7 = new JLabel("数量");
label_7.setBounds(307, 7, 54, 15);
contentPane.add(label_7);
JLabel label_8 = new JLabel("总价");
label_8.setBounds(145, 224, 54, 15);
contentPane.add(label_8);
label_9 = new JLabel("0");
label_9.setBounds(209, 224, 54, 15);
contentPane.add(label_9);
JLabel label_10 = new JLabel("10");
label_10.setBounds(185, 40, 54, 15);
contentPane.add(label_10);
JLabel label_11 = new JLabel("20");
label_11.setBounds(185, 70, 54, 15);
contentPane.add(label_11);
JLabel label_12 = new JLabel("30");
label_12.setBounds(185, 100, 54, 15);
contentPane.add(label_12);
JLabel label_13 = new JLabel("40");
label_13.setBounds(185, 130, 54, 15);
contentPane.add(label_13);
JLabel label_14 = new JLabel("50");
label_14.setBounds(185, 160, 54, 15);
contentPane.add(label_14);
textField = new JTextField();
textField.setBounds(295, 37, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
textField.addKeyListener(new CalcTotle());
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(295, 67, 66, 21);
contentPane.add(textField_1);
textField_1.addKeyListener(new CalcTotle());
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(295, 97, 66, 21);
contentPane.add(textField_2);
textField_2.addKeyListener(new CalcTotle());
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(295, 127, 66, 21);
contentPane.add(textField_3);
textField_3.addKeyListener(new CalcTotle());
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(295, 157, 66, 21);
contentPane.add(textField_4);
textField_4.addKeyListener(new CalcTotle());
}
class CalcTotle extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int count1, count2, count3, count4, count5;
if (!textField.getText().isEmpty()) {
count1 = Integer.parseInt(textField.getText()) * 10;
} else {
count1 = 0;
}
if (!textField_1.getText().isEmpty()) {
count2 = Integer.parseInt(textField_1.getText()) * 20;
} else {
count2 = 0;
}
if (!textField_2.getText().isEmpty()) {
count3 = Integer.parseInt(textField_2.getText()) * 30;
} else {
count3 = 0;
}
if (!textField_3.getText().isEmpty()) {
count4 = Integer.parseInt(textField_3.getText()) * 40;
} else {
count4 = 0;
}
if (!textField_4.getText().isEmpty()) {
count5 = Integer.parseInt(textField_4.getText()) * 50;
} else {
count5 = 0;
}
final int totle = count1 + count2 + count3 + count4 + count5;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
label_9.setText(totle + "");
}
});
}
}
}
这篇文章主要介绍了java通过JFrame做一个登录系统的界面完整代码示例,具有一定借鉴价值,需要的朋友可以参考下。
在java的JFrame内通过创建匿名对象的方式做登录界面
package com.sxt;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame{
JTextField txtname=new JTextField();
JPasswordField txtpass=new JPasswordField();
JButton bl=new JButton("登录");
JButton bg=new JButton("关闭");
//构造无参构造器把主要的方法放在构造器里,然后在main方法里面调
public LoginFrame(){
setBounds(25,25,250,250);
Container c = getContentPane();
c.setLayout(new GridLayout(4,2,10,10));
c.add(new JLabel("用户名"));
c.add(txtname);
c.add(new JLabel("密码"));
c.add(txtpass);
c.add(bl);
c.add(bg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//注意:此处是匿名内部类
bg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
);
//注意:此处是匿名内部类
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
html
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8"
title购物与结算/title
style type="text/css"
/style
link rel="stylesheet" href="
script src="
script src="
script src="
script src="
(beta4)/IE9.js"/script
script src="
script type="text/javascript"
function pay()
{
var link = document.getElementById("a1");
link.click();
var a = new Array();
a[0]="html教程";
a[1]=12;
a[2]=1;
a[3]=a[1]*a[2];
var a1 = new Array();
a1[0]="java详解";
a1[1]=25;
a1[2]=2;
a1[3]=a1[1]*a1[2];
var b = new Array();
b[0]=a;
b[1]=a1;
var amount =0;
for(var i=0;ib.length;i++)
{
var c = new Array();
c=b[i];
amount = amount+c[3];
var tr ="tr";
for(var j=0;jc.length;j++)
{
tr+="td"+c[j]+"/td";
}
tr+="/tr";
$("#list").append(tr);
$("#all").html("b总计:/bfont color=\"red\""+amount+"/font")
}
}
function goon()
{
var link = document.getElementById("a2");
link.click();
$("#news").html("支付成功!!");
}
/script
/head
body
input type="button" class="btn btn-danger" value="点击结算" onclick="pay()"
a href="#myModaltishi" role="button" class="btn" data-toggle="modal" id="a1" style="display: none"/a
div id="myModaltishi" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
div class="modal-header"
button type="button" class="close" data-dismiss="modal" aria-hidden="true"×/button
h3 id="myModalLabel"购物车/h3
/div
div class="modal-body"
div
table id="list" style="width: 95%;" class="table"
th商品名称/th
th单价/th
th数量/th
th金额/th
/table
/div
div align="right" id="all"/div
/div
div class="modal-footer"
button class="btn" data-dismiss="modal" aria-hidden="true"继续挑选/button
button class="btn btn-primary" data-dismiss="modal" onclick="goon()"支付/button
/div
/div
a href="#myModaltishi1" role="button" class="btn" data-toggle="modal" id="a2" style="display: none"/a
div id="myModaltishi1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
div class="modal-header"
button type="button" class="close" data-dismiss="modal" aria-hidden="true"×/button
h3 id="myModalLabel"系统提示/h3
/div
div class="modal-body"
h3 id="news" align="center"/h3
/div
div class="modal-footer"
button class="btn btn-primary" data-dismiss="modal"知道了/button
/div
/div
/body
!-- 浏览器打开可查看效果,
另外由于页面使用了bootstrap框架建议使用渲染较高的浏览器,例如:火狐 --
/html