Java編寫購物車系統
阿新 • • 發佈:2019-01-23
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JButton; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JTextField; public class list1 extends JFrame { String[] list1={"001","筆記本","3","0"}; String[] list2={"002","筆記本1","3","0"}; String[] list3={"003","筆記本2","3","0"}; String[] list4={"004","筆記本3","3","0"}; int flag = 0; double sum=0; list li; private JPanel contentPane;private JLabel lblNewLabel; private JPanel panel; private JPanel panel_1; private JButton btnNewButton; private JButton btnNewButton_1; private JPanel panel_2; private JPanel panel_3; private JLabel lblNewLabel_1; private JLabel lblNewLabel_2; private JLabel lblNewLabel_3;private JLabel lblNewLabel_4; private JLabel lblNewLabel_5; private JLabel lblNewLabel_6; private JLabel lblNewLabel_7; private JLabel lblNewLabel_8; private JLabel lblNewLabel_9; private JLabel lblNewLabel_10; private JLabel lblNewLabel_11; private JLabel lblNewLabel_12; private JLabel lblNewLabel_13; private JLabel lblNewLabel_14; private JLabel lblNewLabel_15; private JLabel lblNewLabel_16; private JLabel lblNewLabel_17; private JLabel lblNewLabel_18; private JLabel lblNewLabel_19; private JLabel lblNewLabel_20; private JLabel lblNewLabel_21; private JLabel lblNewLabel_22; private JButton btnNewButton_2; public list1() { li = new list(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("購物清單"); setBounds(100, 100, 483, 528); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); panel = new JPanel(); contentPane.add(panel, BorderLayout.NORTH); lblNewLabel = new JLabel("購物清單"); panel.add(lblNewLabel); panel_1 = new JPanel(); contentPane.add(panel_1, BorderLayout.SOUTH); btnNewButton = new JButton("結賬"); panel_1.add(btnNewButton); panel_2 = new JPanel(); contentPane.add(panel_2, BorderLayout.CENTER); panel_2.setLayout(new GridLayout(0, 4, 4, 0)); panel_3 = new JPanel(); panel_2.add(panel_3); lblNewLabel_1 = new JLabel("商品編號"); panel_3.add(lblNewLabel_1); JPanel panel_4 = new JPanel(); panel_2.add(panel_4); lblNewLabel_2 = new JLabel("名稱"); panel_4.add(lblNewLabel_2); JPanel panel_5 = new JPanel(); panel_2.add(panel_5); lblNewLabel_3 = new JLabel("單價(元)"); panel_5.add(lblNewLabel_3); JPanel panel_6 = new JPanel(); panel_2.add(panel_6); lblNewLabel_4 = new JLabel("數量"); panel_6.add(lblNewLabel_4); JPanel panel_7 = new JPanel(); panel_2.add(panel_7); lblNewLabel_5 = new JLabel(); panel_7.add(lblNewLabel_5); JPanel panel_8 = new JPanel(); panel_2.add(panel_8); lblNewLabel_6 = new JLabel(); panel_8.add(lblNewLabel_6); JPanel panel_9 = new JPanel(); panel_2.add(panel_9); lblNewLabel_7 = new JLabel(); panel_9.add(lblNewLabel_7); JPanel panel_10 = new JPanel(); panel_2.add(panel_10); lblNewLabel_8 = new JLabel(); panel_10.add(lblNewLabel_8); JPanel panel_11 = new JPanel(); panel_2.add(panel_11); lblNewLabel_9 = new JLabel(); panel_11.add(lblNewLabel_9); JPanel panel_12 = new JPanel(); panel_2.add(panel_12); lblNewLabel_10 = new JLabel(); panel_12.add(lblNewLabel_10); JPanel panel_13 = new JPanel(); panel_2.add(panel_13); lblNewLabel_11 = new JLabel(); panel_13.add(lblNewLabel_11); JPanel panel_14 = new JPanel(); panel_2.add(panel_14); lblNewLabel_12 = new JLabel(); panel_14.add(lblNewLabel_12); JPanel panel_15 = new JPanel(); panel_2.add(panel_15); lblNewLabel_13 = new JLabel(); panel_15.add(lblNewLabel_13); JPanel panel_16 = new JPanel(); panel_2.add(panel_16); lblNewLabel_14 = new JLabel(); panel_16.add(lblNewLabel_14); JPanel panel_17 = new JPanel(); panel_2.add(panel_17); lblNewLabel_15 = new JLabel(); panel_17.add(lblNewLabel_15); JPanel panel_18 = new JPanel(); panel_2.add(panel_18); lblNewLabel_16 = new JLabel(); panel_18.add(lblNewLabel_16); JPanel panel_19 = new JPanel(); panel_2.add(panel_19); lblNewLabel_17 = new JLabel(); panel_19.add(lblNewLabel_17); JPanel panel_20 = new JPanel(); panel_2.add(panel_20); lblNewLabel_18 = new JLabel(""); panel_20.add(lblNewLabel_18); JPanel panel_21 = new JPanel(); panel_2.add(panel_21); lblNewLabel_19 = new JLabel(""); panel_21.add(lblNewLabel_19); JPanel panel_22 = new JPanel(); panel_2.add(panel_22); lblNewLabel_20 = new JLabel(""); panel_22.add(lblNewLabel_20); JPanel panel_23 = new JPanel(); panel_2.add(panel_23); lblNewLabel_21 = new JLabel("總價:"); panel_23.add(lblNewLabel_21); JPanel panel_24 = new JPanel(); panel_2.add(panel_24); lblNewLabel_22 = new JLabel("0元"); panel_24.add(lblNewLabel_22); setVisible(true); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showConfirmDialog(null, "您的消費總額為"+sum+"元"); } }); } public void list_1(String str1,String str2){ if(str1.equals(list1[0])) { lblNewLabel_5.setText(list1[0]); lblNewLabel_6.setText(list1[1]); lblNewLabel_7.setText(list1[2]); list1[3]=str2; lblNewLabel_8.setText(list1[3]); Double d1 = new Double(list1[2]); Double d2 = new Double(list1[3]); sum =sum+ d1*d2; } if(str1.equals(list2[0])) { lblNewLabel_9.setText(list2[0]); lblNewLabel_10.setText(list2[1]); lblNewLabel_11.setText(list2[2]); list2[3]=str2; lblNewLabel_12.setText(list2[3]); Double d3 = new Double(list2[2]); Double d4 = new Double(list2[3]); sum =sum+ d3*d4; } if(str1.equals(list3[0])) { lblNewLabel_13.setText(list3[0]); lblNewLabel_14.setText(list3[1]); lblNewLabel_15.setText(list3[2]); list3[3]=str2; lblNewLabel_16.setText(list3[3]); Double d5 = new Double(list3[2]); Double d6 = new Double(list3[3]); sum =sum+ d5*d6; } if(str1.equals(list4[0])) { lblNewLabel_17.setText(list4[0]); lblNewLabel_18.setText(list4[1]); lblNewLabel_19.setText(list4[2]); list4[3]=str2; lblNewLabel_20.setText(list4[3]); Double d7 = new Double(list4[2]); Double d8 = new Double(list4[3]); sum =sum+ d7*d8; } lblNewLabel_22.setText(sum+"元"); } }