当前位置:首页 » 网购平台 » swing实现购物车
扩展阅读
宁波奥德赛优惠价格 2021-03-15 14:26:02
丹尼斯购物卡能挂失么 2021-03-15 14:25:58
淘宝购物指纹验证失败 2021-03-15 14:24:44

swing实现购物车

发布时间: 2021-03-03 10:22:22

1. 运用Java Swing技术实现一个简单的计算

一共写了四个文件,
1.BlueButton.java
import java.awt.Color;

import javax.swing.JButton;

public class BlueButton extends JButton {

/**
*
*/
private static final long serialVersionUID = 1L;

public BlueButton(String text){
(text);
setForeground(Color.BLUE);
}
}
2.RedButton.java
import java.awt.Color;

import javax.swing.JButton;

public class RedButton extends JButton {
/**
*
*/
private static final long serialVersionUID = 1L;

public RedButton(String text){
super(text);
setForeground(Color.RED);
}
}
3.GUI.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;

JMenuBar bar;
JMenu edit, view, help;

protected JMenuItem , paste, typical, science, group, subject, about;

protected JTextField txt;
protected JPanel buttonPane, numPane;
protected JPanel subNorthPane, subWestPane;
protected JButton[] numButtons;
protected JButton btnAdd;
protected JButton btnSub;
protected JButton btnMul;
protected JButton btnDiv;
protected JButton btnSig;
protected JButton btnPoint;
protected JButton btnSqrt;
protected JButton btnBack;
protected JButton btnC;
protected JButton btnCE;
protected JButton btnReciprocal;
protected JButton btnCalc;

public GUI(){
super("计算器");
Toolkit tk = getToolkit();
Image img = tk.getImage("");
setIconImage(img);

setLocation(400, 500);
setResizable(false);

getContentPane().setLayout(new BorderLayout());

setGUI();

setSize(380, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void setGUI() {
setMenu();
Container c = getContentPane();

txt = new JTextField("0.");
txt.setHorizontalAlignment(JTextField.RIGHT);
txt.setEditable(false);
txt.setBackground(Color.WHITE);
c.add(txt, BorderLayout.NORTH);

buttonPane = new JPanel();
buttonPane.setLayout(new BorderLayout(8, 3));

subWestPane = new JPanel();
subWestPane.setLayout(new GridLayout(4, 1, 3, 3));

subWestPane.add(new RedButton("MC"));
subWestPane.add(new RedButton("MR"));
subWestPane.add(new RedButton("MS"));
subWestPane.add(new RedButton("M+"));

subNorthPane = new JPanel();
subNorthPane.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
JLabel j = new JLabel(" ");
j.setBorder(BorderFactory.createLoweredBevelBorder());
subNorthPane.add(j);

btnBack = new RedButton("Backspace");
btnCE = new RedButton(" CE ");
btnC = new RedButton(" C ");
subNorthPane.add(btnBack);
subNorthPane.add(btnCE);
subNorthPane.add(btnC);

numPane = new JPanel();
numPane.setLayout(new GridLayout(4, 5, 3, 3));
numButtons = new JButton[10];
for(int i=0; i<numButtons.length; i++){
numButtons[i] = new BlueButton(String.valueOf(i));
}

numPane.add(numButtons[7]);
numPane.add(numButtons[8]);
numPane.add(numButtons[9]);
btnDiv = new RedButton("/");
numPane.add(btnDiv);
btnSqrt = new RedButton("sqrt");
numPane.add(btnSqrt);

numPane.add(numButtons[4]);
numPane.add(numButtons[5]);
numPane.add(numButtons[6]);
btnMul = new RedButton("*");
numPane.add(btnMul);
numPane.add(new RedButton("%"));

numPane.add(numButtons[1]);
numPane.add(numButtons[2]);
numPane.add(numButtons[3]);
btnSub = new RedButton("-");
numPane.add(btnSub);
btnReciprocal = new RedButton("1/x");
numPane.add(btnReciprocal);

numPane.add(numButtons[0]);
btnSig = new RedButton("+ / -");
numPane.add(btnSig);
btnPoint = new RedButton(".");
numPane.add(btnPoint);
btnAdd = new RedButton("+");
numPane.add(btnAdd);
btnCalc = new RedButton("=");
numPane.add(btnCalc);

buttonPane.add(subWestPane, BorderLayout.WEST);
buttonPane.add(subNorthPane, BorderLayout.NORTH);
buttonPane.add(numPane, BorderLayout.CENTER);

c.add(buttonPane, BorderLayout.CENTER);
}

private void setMenu() {
bar = new JMenuBar();

= new JMenuItem("Copy");
.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK, false));
paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK, false));

edit = new JMenu("EDIT");
edit.add();
edit.add(paste);

typical = new JMenuItem("Typical");
science = new JMenuItem("Science");
group = new JMenuItem("Group");
view = new JMenu("VIEW");
view.add(typical);
view.add(science);
view.addSeparator();
view.add(group);

subject = new JMenuItem("Subject");
about = new JMenuItem("About");
help =new JMenu("HELP");
help.add(subject);
help.addSeparator();
help.add(about);

bar.add(edit);
bar.add(view);
bar.add(help);

setJMenuBar(bar);
}
}
4.Calc.java
public class Calc {

/**
* @param args
*/
public static void main(String[] args) {
new GUI();

}

}

2. JAVA 购物车示例代码

import java.awt.*;
import java.awt.event.*;
class ShopFrame extends Frame implements ActionListener
{ Label label1,label2,label3,label4;
Button button1,button2,button3,button4,button5;
TextArea text;
Panel panel1,panel2;
static float sum=0.0f;
ShopFrame(String s)
{ super(s);
setLayout(new BorderLayout());
label1=new Label("面纸:3元",Label.LEFT);
label2=new Label("钢笔:5元",Label.LEFT);
label3=new Label("书:10元",Label.LEFT);
label4=new Label("袜子:8元",Label.LEFT);
button1=new Button("加入购物车");
button2=new Button("加入购物车");
button3=new Button("加入购物车");
button4=new Button("加入购物车");
button5=new Button("查看购物车");
text=new TextArea("商品有:"+"\n",5,10);
text.setEditable(false);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
panel1=new Panel();
panel2=new Panel();
panel1.add(label1);
panel1.add(button1);
panel1.add(label2);
panel1.add(button2);
panel1.add(label3);
panel1.add(button3);
panel1.add(label4);
panel1.add(button4);
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.NORTH);
panel2.add(text,BorderLayout.SOUTH);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
setBounds(100,100,350,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ text.append("一个面纸、");
sum=sum+3;
}
else if(e.getSource()==button2)
{ text.append("一只钢笔、");
sum=sum+5;
}
else if(e.getSource()==button3)
{ text.append("一本书、");
sum=sum+10;
}
else if(e.getSource()==button4)
{ text.append("一双袜子、");
sum=sum+8;
}
else if(e.getSource()==button5)
{
text.append("\n"+"总价为:"+"\n"+sum);
}
}

}

public class Shopping {
public static void main(String[] args) {
new ShopFrame("购物车");

}

}
我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。

3. 我想用java swing实现窗口嵌套,不知道应该怎么做。能给个示例吗

c#的好啊
简单方便
开发工具也多
swing太麻烦了!布局太多布局管理器还不好用!
就netBeans这个开发工具还勉强方便点
但是不能在生成的程序里改代码!
必须用记事本复制出来改
忒麻烦!

4. java(swing编程)如何这个简单效果

public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = (JPanel) jf.getContentPane();
jp.setLayout(null);
JPanel group = new JPanel();
group.setBorder(BorderFactory.createTitledBorder("日常用品"));
group.setBounds(50, 50, 400, 400);
jp.add(group);
group.setLayout(null);

JTabbedPane jtp = new JTabbedPane();
group.add(jtp);
jtp.setBounds(50, 50, 300, 300);
jtp.setTabPlacement(JTabbedPane.TOP);
jtp.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);

jtp.addTab("adfasdddddddddddddddddddddd", new JPanel());
jtp.addTab("sssddddddddddddddddddddddds", new JPanel());

jf.setBounds(200, 200, 500, 500);
jf.setVisible(true);
}


那其实并不是真正竖着,而是使用了JTabbedPane.WRAP_TAB_LAYOUT布局,也就是选项卡自动换行,然后给选项卡足够的宽度,宽到横向只能容纳一个选项卡的程度,就会出现这种竖着的效果


group.setBorder(BorderFactory.createTitledBorder("日常用品"));是为面板创建一个带标题的边框,BorderFactory有以下创建边框的方法,选一个适合你的用——

==================================

static TitledBorder createTitledBorder(Border border)
创建一个空标题的新标题边框,使其具有指定的边框对象、默认的文本位置(位于顶线上)、默认的调整 (leading),以及默认的字体和文本颜色(由当前外观确定)。

static TitledBorder createTitledBorder(Border border, String title)
向现有边框添加一个标题,使其具有默认的位置(位于顶线上)、默认的调整 (leading),以及默认的字体和文本颜色(由当前外观确定)。

static TitledBorder createTitledBorder(Border border, String title, int titleJustification, int titlePosition)
向现有边框添加一个标题,使其具有指定的位置和默认字体和文本颜色(由当前外观确定)。

static TitledBorder createTitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont)
向现有边框添加一个标题,使其具有指定的位置和默认的文本颜色(由当前外观确定)。

static TitledBorder createTitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont, Color titleColor)
向现有边框添加一个标题,使其具有指定的位置、字体和颜色。

5. 为什么使用swing实现支付宝支付

你的java和图片放在一个目录,
我都是放在C盘根目录了,
给你稍微改了一下代码:
import java.awt.*;
import javax.swing.*;
public class TestGra extends JFrame {
 Container c = getContentPane();
 JLabel lb;
 Image image;
 public TestGra() {
  // 就改这里了
  ImageIcon img = new ImageIcon(System.getProperty("user.dir") + "\\1.jpeg");
  lb = new JLabel(img);
  
  add(lb, BorderLayout.CENTER);
  setSize(800, 600);
  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
 public static void main(String as[]) {
  new TestGra();
 }
}

6. 关于swing的一个功能实现

我放心地远去,
一切都不能阻止那拖着我们上升的死亡的洪水
我的身体是个谜。
待在车上的我们
就象我过去忍受太阳照晒一样……
这人世中的愁怨哈哈

7. 你好,java购物车代码

import java.awt.*;
import java.awt.event.*;
class ShopFrame extends Frame implements ActionListener
{ Label label1,label2,label3,label4;
Button button1,button2,button3,button4,button5;
TextArea text;
Panel panel1,panel2;
static float sum=0.0f;
ShopFrame(String s)
{ super(s);
setLayout(new BorderLayout());
label1=new Label("面纸:3元",Label.LEFT);
label2=new Label("钢笔:5元",Label.LEFT);
label3=new Label("书:10元",Label.LEFT);
label4=new Label("袜子:8元",Label.LEFT);
button1=new Button("加入购物车");
button2=new Button("加入购物车");
button3=new Button("加入购物车");
button4=new Button("加入购物车");
button5=new Button("查看购物车");
text=new TextArea("商品有:"+"\n",5,10);
text.setEditable(false);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
panel1=new Panel();
panel2=new Panel();
panel1.add(label1);
panel1.add(button1);
panel1.add(label2);
panel1.add(button2);
panel1.add(label3);
panel1.add(button3);
panel1.add(label4);
panel1.add(button4);
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.NORTH);
panel2.add(text,BorderLayout.SOUTH);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
setBounds(100,100,350,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ text.append("一个面纸、");
sum=sum+3;
}
else if(e.getSource()==button2)
{ text.append("一只钢笔、");
sum=sum+5;
}
else if(e.getSource()==button3)
{ text.append("一本书、");
sum=sum+10;
}
else if(e.getSource()==button4)
{ text.append("一双袜子、");
sum=sum+8;
}
else if(e.getSource()==button5)
{
text.append("\n"+"总价为:"+"\n"+sum);
}
}

}

public class Shopping {
public static void main(String[] args) {
new ShopFrame("购物车");

}

}
我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。

8. 用java swing 设计一个网上购物系统,要求实现基本功能(注册,登录,购物车等等),代码怎么写啊没有学过

完全可以
我已用java swing写了个登陆这侧搜索股票的功能,我完全可以很快实现你的功能,不过要付费的哦

9. 用java swing 做一个点菜系统,如何将菜单做成按钮的形式,并将图片显示在按钮上

自己写一个类 继承 JButton 重写方法 自己定义一个 图片按钮

10. 用javaswing实现网店一键搬家功能

API中的内容:来

从类 javax.swing.AbstractButton 继承的方法自

doClick
public void doClick()

以编程方式执行“单击”。此方法的效果等同于用户按下并随后释放按钮。

doClick
public void doClick(int pressTime)

以编程方式执行“单击”。此方法的效果等同于用户按下并随后释放按钮。按钮在虚拟“按下”状态下停留 pressTime 毫秒的时间。

参数:
pressTime - “按下”按钮的时间,以毫秒为单位