简易商品购物系统购物界面程序设计

xiaoxiao2021-02-28  87

购物界面

import java.awt.BorderLayout; public class Supermarket extends JFrame{ private JPanel contentPane; private JTable table; private JTextField txtId; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; private JTextField textField_5; private JTextField textField; private JTextField textField_6; private JTextField textField_7; private JTextField textField_8; private JTextField textField_9; private JTextField textField_10; private JTextField textField_11; private JTextField textField_12; // String name; // Integer count; // Integer price; // Integer pricetotal; // Integer Total; /**  * Launch the application.  */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Supermarket frame = new Supermarket(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /**  * Create the frame.  */ public Supermarket() { int count = 1; final Vector<Object> v = new Vector<Object>(); v.add(001); v.add("云烟"); v.add(count); v.add(15); v.add(15); final Vector<Object> v1 = new Vector<Object>(); v1.add(002); v1.add("利群"); v1.add(count); v1.add(10); v1.add(10); setTitle("\u6536\u94F6\u53F0"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 852, 520); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(); panel.setBounds(10, 57, 400, 415); contentPane.add(panel); panel.setLayout(null); table = new JTable();  final JTable table = new JTable(); String[] tableHeads = new String[] { "ID","商品列表", "数量", "单价", "小计","选择" }; final DefaultTableModel dtm = (DefaultTableModel) table.getModel();//创建model dtm.setColumnIdentifiers(tableHeads);//创建表头,表头的类型可以是vector 或者Object[] table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // new JScrollPane(table); TableColumn CountColumn2 = table.getColumnModel().getColumn(2); TableColumn CountColumn5 = table.getColumnModel().getColumn(5); JComboBox comboBox = new JComboBox(); comboBox.addItem("1"); comboBox.addItem("2"); comboBox.addItem("3"); comboBox.addItem("4"); comboBox.addItem("5"); comboBox.addItem("6"); comboBox.addItem("7"); comboBox.addItem("8"); comboBox.addItem("9"); CountColumn2.setCellEditor(new DefaultCellEditor(comboBox)); //        table.addMouseListener(new MouseAdapter(){ //            public void mouseClicked(MouseEvent e) {//仅当鼠标单击时响应 //               //得到选中的行列的索引值 //              int r= table.getSelectedRow(); //             // System.out.println(table.getValueAt(1, 2)); // comboBox.addItemListener(new ItemListener(){ // // @Override // public void itemStateChanged(ItemEvent e) { // // TODO Auto-generated method stub // // if(e.getStateChange() == ItemEvent.SELECTED){ // // System.out.println(ItemEvent.SELECTED); // System.out.println(table.getValueAt(0, 2)); // System.out.println(table.getValueAt(1, 2)); // System.out.println(table.getValueAt(2, 2)); // System.out.println(table.getValueAt(3, 2)); // for(int i = 1;i < table.getRowCount();i++){ // int aValue = Integer.parseInt((String) table.getValueAt(i, 2))*Integer.parseInt((String) table.getValueAt(i, 3)); // System.out.println(aValue); // table.setValueAt(aValue, i, 4); // System.out.println(i); // System.out.println(table.getValueAt(i, 4)); // } // // } // // } // // }); JCheckBox JCB = new JCheckBox(); CountColumn5.setCellEditor(new DefaultCellEditor(JCB)); table.setBounds(10, 98, 380, 242); panel.add(table); txtId = new JTextField(); txtId.setHorizontalAlignment(SwingConstants.LEFT); txtId.setText("ID"); txtId.setBounds(10, 70, 63, 28); panel.add(txtId); txtId.setColumns(10); textField_1 = new JTextField(); textField_1.setText("\u5546\u54C1\u540D\u79F0"); textField_1.setHorizontalAlignment(SwingConstants.LEFT); textField_1.setColumns(10); textField_1.setBounds(73, 70, 63, 28); panel.add(textField_1); textField_2 = new JTextField(); textField_2.setText("\u5355\u4EF7"); textField_2.setHorizontalAlignment(SwingConstants.LEFT); textField_2.setColumns(10); textField_2.setBounds(199, 70, 63, 28); panel.add(textField_2); textField_3 = new JTextField(); textField_3.setText("\u6570\u91CF"); textField_3.setHorizontalAlignment(SwingConstants.LEFT); textField_3.setColumns(10); textField_3.setBounds(136, 70, 63, 28); panel.add(textField_3); textField_4 = new JTextField(); textField_4.setBounds(70, 10, 250, 50); panel.add(textField_4); textField_4.setColumns(10); JPanel panel_2 = new JPanel(); panel_2.setBounds(10, 10, 816, 39); contentPane.add(panel_2); panel_2.setLayout(null); JButton btnNewButton_1 = new JButton("\u786E\u8BA4"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(textField_4.getText().equals("001")){ dtm.addRow(v); } if(textField_4.getText().equals("002")){ dtm.addRow(v1); } } }); btnNewButton_1.setBounds(320, 10, 70, 50); panel.add(btnNewButton_1); JButton button_23 = new JButton("\u641C\u7D22"); button_23.setBounds(10, 10, 60, 50); panel.add(button_23); textField_5 = new JTextField(); textField_5.setText("\u5C0F\u8BA1"); textField_5.setHorizontalAlignment(SwingConstants.LEFT); textField_5.setColumns(10); textField_5.setBounds(262, 70, 64, 28); panel.add(textField_5); textField = new JTextField(); textField.setBounds(314, 350, 76, 40); panel.add(textField); textField.setColumns(10); JButton btnNewButton_2 = new JButton("\u5408\u8BA1\uFF1A"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int pricetotal = 0; int tableCol = table.getRowCount(); int[] tableforthcol = new int[tableCol]; for(int i=0;i<tableCol;i++){ tableforthcol[i] = Integer.parseInt(table.getValueAt(i, 4).toString()); pricetotal += tableforthcol[i]; } textField.setText(String.valueOf(pricetotal)); textField_6.setText(textField.getText()); // System.out.println(table.getValueAt(1, 1)); } }); btnNewButton_2.setBounds(238, 350, 76, 40); panel.add(btnNewButton_2); JButton button_24 = new JButton("\u5220\u9664\u8D2D\u4E70"); button_24.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(table.getRowCount() != 0){ dtm.removeRow(table.getRowCount()-1); }else{ } } }); button_24.setBounds(10, 350, 87, 40); panel.add(button_24); textField_12 = new JTextField(); textField_12.setText("\u9009\u62E9"); textField_12.setHorizontalAlignment(SwingConstants.LEFT); textField_12.setColumns(10); textField_12.setBounds(326, 70, 65, 28); panel.add(textField_12); JPanel panel_1 = new JPanel(); panel_1.setBounds(426, 57, 400, 415); contentPane.add(panel_1); panel_1.setLayout(null); JButton btnNewButton = new JButton("\u73B0\u91D1\u652F\u4ED8"); btnNewButton.setBounds(10, 126, 93, 48); panel_1.add(btnNewButton); JButton button = new JButton("1"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField_7.setText(textField_7.getText()+"1"); } }); button.setBounds(113, 126, 55, 48); panel_1.add(button); JButton button_1 = new JButton("2"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"2"); } }); button_1.setBounds(178, 126, 55, 48); panel_1.add(button_1); JButton button_2 = new JButton("3"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"3"); } }); button_2.setBounds(243, 126, 55, 48); panel_1.add(button_2); JButton button_3 = new JButton("\uFFE5100"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField_7.getText().equals("")){ textField_7.setText("100"); }else{ textField_7.setText(String.valueOf(Integer.parseInt(textField_7.getText())+100)); } } }); button_3.setBounds(308, 126, 82, 48); panel_1.add(button_3); JButton button_4 = new JButton("\u652F\u4ED8\u5B9D"); button_4.setBounds(10, 184, 93, 48); panel_1.add(button_4); JButton button_5 = new JButton("4"); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"4"); } }); button_5.setBounds(113, 184, 55, 48); panel_1.add(button_5); JButton button_6 = new JButton("5"); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"5"); } }); button_6.setBounds(178, 184, 55, 48); panel_1.add(button_6); JButton button_7 = new JButton("6"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"6"); } }); button_7.setBounds(243, 184, 55, 48); panel_1.add(button_7); JButton button_8 = new JButton("\uFFE550"); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField_7.getText().equals("")){ textField_7.setText("50"); }else{ textField_7.setText(String.valueOf(Integer.parseInt(textField_7.getText())+50)); } } }); button_8.setBounds(308, 184, 82, 48); panel_1.add(button_8); JButton button_9 = new JButton("\u5FAE\u4FE1"); button_9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new QR_cord().setVisible(true); } }); button_9.setBounds(10, 242, 93, 48); panel_1.add(button_9); JButton button_10 = new JButton("7"); button_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"7"); } }); button_10.setBounds(113, 242, 55, 48); panel_1.add(button_10); JButton button_11 = new JButton("8"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"8"); } }); button_11.setBounds(178, 242, 55, 48); panel_1.add(button_11); JButton button_12 = new JButton("9"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"9"); } }); button_12.setBounds(243, 242, 55, 48); panel_1.add(button_12); JButton button_13 = new JButton("\uFFE520"); button_13.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField_7.getText().equals("")){ textField_7.setText("20"); }else{ textField_7.setText(String.valueOf(Integer.parseInt(textField_7.getText())+20)); } } }); button_13.setBounds(308, 242, 82, 48); panel_1.add(button_13); JButton button_14 = new JButton("\u94F6\u884C\u5361"); button_14.setBounds(10, 300, 93, 48); panel_1.add(button_14); JButton button_15 = new JButton("0"); button_15.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"0"); } }); button_15.setBounds(113, 300, 55, 48); panel_1.add(button_15); JButton button_16 = new JButton("00"); button_16.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"00"); } }); button_16.setBounds(178, 300, 55, 48); panel_1.add(button_16); JButton button_17 = new JButton("."); button_17.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_7.setText(textField_7.getText()+"."); } }); button_17.setBounds(243, 300, 55, 48); panel_1.add(button_17); JButton button_18 = new JButton("\uFFE510"); button_18.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField_7.getText().equals("")){ textField_7.setText("10"); }else{ textField_7.setText(String.valueOf(Integer.parseInt(textField_7.getText())+10)); } } }); button_18.setBounds(308, 300, 82, 48); panel_1.add(button_18); JButton button_19 = new JButton("\u4FE1\u7528\u5361"); button_19.setBounds(10, 357, 93, 48); panel_1.add(button_19); JButton button_20 = new JButton("\u5220\u9664"); button_20.setFont(new Font("微软雅黑", Font.PLAIN, 10)); button_20.setBounds(113, 357, 55, 48); panel_1.add(button_20); JButton button_21 = new JButton("\u6E05\u7A7A"); button_21.setFont(new Font("微软雅黑", Font.PLAIN, 10)); button_21.setBounds(178, 357, 55, 48); panel_1.add(button_21); JButton button_22 = new JButton("\u7ED3\u7B97"); button_22.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int change = Integer.parseInt(textField_7.getText())-Integer.parseInt(textField.getText()); textField_8.setText(String.valueOf(change)); } }); button_22.setFont(new Font("微软雅黑", Font.PLAIN, 11)); button_22.setBounds(243, 357, 147, 48); panel_1.add(button_22); textField_6 = new JTextField(); textField_6.setHorizontalAlignment(SwingConstants.CENTER); textField_6.setBounds(10, 47, 126, 67); panel_1.add(textField_6); textField_6.setColumns(10); textField_7 = new JTextField(); textField_7.setHorizontalAlignment(SwingConstants.CENTER); textField_7.setColumns(10); textField_7.setBounds(136, 47, 126, 67); panel_1.add(textField_7); textField_8 = new JTextField(); textField_8.setHorizontalAlignment(SwingConstants.CENTER); textField_8.setColumns(10); textField_8.setBounds(262, 47, 126, 67); panel_1.add(textField_8); textField_9 = new JTextField(); textField_9.setBackground(Color.LIGHT_GRAY); textField_9.setText("\u5E94\u6536\uFF1A"); textField_9.setHorizontalAlignment(SwingConstants.CENTER); textField_9.setBounds(10, 10, 126, 37); panel_1.add(textField_9); textField_9.setColumns(10); textField_10 = new JTextField(); textField_10.setBackground(Color.LIGHT_GRAY); textField_10.setText("\u5B9E\u6536\uFF1A"); textField_10.setHorizontalAlignment(SwingConstants.CENTER); textField_10.setColumns(10); textField_10.setBounds(136, 10, 126, 37); panel_1.add(textField_10); textField_11 = new JTextField(); textField_11.setBackground(Color.LIGHT_GRAY); textField_11.setText("\u627E\u96F6\uFF1A"); textField_11.setHorizontalAlignment(SwingConstants.CENTER); textField_11.setColumns(10); textField_11.setBounds(262, 10, 126, 37); panel_1.add(textField_11); } }

转载请注明原文地址: https://www.6miu.com/read-78969.html

最新回复(0)