當前位置:首頁 » 網購平台 » 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 - 「按下」按鈕的時間,以毫秒為單位