• JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)


    如果用eclipse需先装载windowsbuild

      1 //导入包
      2 import java.awt.BorderLayout;
      3 import java.awt.EventQueue;
      4 
      5 import javax.swing.JFrame;
      6 import javax.swing.JPanel;
      7 import javax.swing.border.EmptyBorder;
      8 import javax.swing.JTable;
      9 import javax.swing.table.DefaultTableModel;
     10 import javax.swing.JLabel;
     11 import javax.swing.SwingConstants;
     12 import java.awt.Font;
     13 import javax.swing.JTextField;
     14 import javax.swing.JTextArea;
     15 import javax.swing.JComboBox;
     16 import javax.swing.DefaultComboBoxModel;
     17 import javax.swing.JCheckBox;
     18 import javax.swing.JButton;
     19 import java.awt.Color;
     20 import java.awt.SystemColor;
     21 import java.awt.event.ActionListener;
     22 import java.awt.event.ActionEvent;
     23 import javax.swing.ImageIcon;
     24 
     25 public class CommondityChange extends JFrame {
     26     /**
     27      * 声明
     28      */
     29     private JPanel contentPane;
     30     private JTable table;
     31     private JTextField textField;
     32     private JTextField textField_1;
     33     private JTextField textField_2;
     34     float sumPrice;//应付金额
     35 
     36     /**
     37      * Launch the application.
     38      */
     39     public static void main(String[] args) {
     40         /**
     41          * star threa
     42          */
     43         EventQueue.invokeLater(new Runnable() {
     44             public void run() {
     45                 try {
     46                     CommondityChange frame = new CommondityChange();
     47                     frame.setVisible(true);//设置可见
     48                 } catch (Exception e) {
     49                     e.printStackTrace();
     50                 }
     51             }
     52         });
     53     }
     54 
     55     /**
     56      * Create the frame.
     57      */
     58     public CommondityChange() {
     59         
     60         {
     61         
     62         }
     63         setTitle("商品交易");
     64         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     65         setBounds(100, 100, 1091, 544);
     66         contentPane = new JPanel();
     67         contentPane.setBackground(SystemColor.controlHighlight);
     68         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
     69         setContentPane(contentPane);
     70         contentPane.setLayout(null);
     71         
     72         table = new JTable();
     73         table.setBackground(new Color(173, 216, 230));
     74         table.setModel(new DefaultTableModel(
     75             new Object[][] {//表格内容
     76                 {"u7F16u53F7", "u7C7Bu578B", "u5546u54C1", "u5355u4EF7/u5143", "库存"},
     77                 {"001", "u7537u88C5", "adidasu5706u9886polou886B", "298", "21"},
     78                 {"002", "u7535u5668", "u683Cu529Bu7A7Au8C03", "3888", "5"},
     79                 {"003", "u7537u978B", "Niceu5E73u5E95u8FD0u52A8u978Bu900Fu6C14u578B", "368", "19"},
     80                 {"004", "u6237u5916", "u9ED1u767Du6237u5916u53CCu80A9u80CCu5305", "259", "12"},
     81                 {"005", "u5316u5986", "chanelu7537u58EBu9999u6C34", "1988", "15"},
     82                 {"006", "u6570u7801", "u534Eu7855FL5900", "3988", "9"},
     83             },
     84             new String[] {
     85                 "u7F16u53F7", "u7C7Bu578B", "u5546u54C1", "u5355u4EF7/u5143", "库存"
     86             }
     87         ));
     88         table.getColumnModel().getColumn(0).setPreferredWidth(95);
     89         table.getColumnModel().getColumn(1).setPreferredWidth(95);
     90         table.getColumnModel().getColumn(2).setPreferredWidth(95);
     91         table.getColumnModel().getColumn(3).setPreferredWidth(95);
     92         table.getColumnModel().getColumn(4).setPreferredWidth(95);
     93         table.setBounds(10, 42, 812, 206);
     94         contentPane.add(table);
     95         table.setRowHeight(30);//设置单元格高度
     96         
     97         
     98         JLabel label = new JLabel("应付");//标签
     99         label.setFont(new Font("宋体", Font.BOLD, 14));//设置字体、大小、格式
    100         label.setHorizontalAlignment(SwingConstants.CENTER);//文字居中
    101         label.setBounds(858, 258, 60, 23);
    102         contentPane.add(label);
    103         
    104         JLabel label_1 = new JLabel("实收");
    105         label_1.setHorizontalAlignment(SwingConstants.CENTER);
    106         label_1.setFont(new Font("宋体", Font.BOLD, 14));
    107         label_1.setBounds(856, 301, 60, 23);
    108         contentPane.add(label_1);
    109         
    110         JLabel label_2 = new JLabel("找零");
    111         label_2.setHorizontalAlignment(SwingConstants.CENTER);
    112         label_2.setFont(new Font("宋体", Font.BOLD, 14));
    113         label_2.setBounds(858, 426, 60, 23);
    114         contentPane.add(label_2);
    115         
    116         textField = new JTextField();//文本框
    117         textField.setBounds(928, 259, 107, 23);
    118         contentPane.add(textField);
    119         textField.setColumns(10);
    120         
    121         textField_1 = new JTextField();
    122         textField_1.setColumns(10);
    123         textField_1.setBounds(928, 302, 107, 23);
    124         contentPane.add(textField_1);
    125         
    126         textField_2 = new JTextField();
    127         textField_2.setColumns(10);
    128         textField_2.setBounds(928, 426, 107, 23);
    129         contentPane.add(textField_2);
    130         
    131         JTextArea textArea = new JTextArea();//文本域
    132         textArea.setBackground(SystemColor.text);
    133         textArea.setBounds(10, 301, 802, 191);
    134         contentPane.add(textArea);
    135         
    136         JComboBox comboBox_0 = new JComboBox();//下拉列表
    137         comboBox_0.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));//下拉内容
    138         comboBox_0.setMaximumRowCount(5);
    139         comboBox_0.setBounds(934, 76, 32, 21);
    140         
    141         contentPane.add(comboBox_0);
    142         
    143         JLabel lblNewLabel = new JLabel("");
    144         lblNewLabel.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hewolianxi.jpg"));//导入图片,本地
    145         lblNewLabel.setBounds(981, 79, 66, 15);
    146         contentPane.add(lblNewLabel);
    147         
    148         JComboBox comboBox_1 = new JComboBox();
    149         comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
    150         comboBox_1.setMaximumRowCount(5);
    151         comboBox_1.setBounds(934, 107, 32, 21);
    152         contentPane.add(comboBox_1);
    153         
    154         JComboBox comboBox_2 = new JComboBox();
    155         comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
    156         comboBox_2.setMaximumRowCount(5);
    157         comboBox_2.setBounds(934, 138, 32, 21);
    158         contentPane.add(comboBox_2);
    159         
    160         JComboBox comboBox_3 = new JComboBox();
    161         comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
    162         comboBox_3.setMaximumRowCount(5);
    163         comboBox_3.setBounds(934, 169, 32, 21);
    164         contentPane.add(comboBox_3);
    165         //comboBox_3.getSelectedItem(); //获取当前选中内容
    166         
    167         JComboBox comboBox_4 = new JComboBox();
    168         comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
    169         comboBox_4.setMaximumRowCount(5);
    170         comboBox_4.setBounds(934, 200, 32, 21);
    171         contentPane.add(comboBox_4);
    172         
    173         
    174         JComboBox comboBox_5 = new JComboBox();
    175         comboBox_5.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
    176         comboBox_5.setMaximumRowCount(5);
    177         comboBox_5.setBounds(934, 231, 32, 21);
    178         contentPane.add(comboBox_5);
    179         
    180         JLabel label_3 = new JLabel("");
    181         label_3.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hewolianxi.jpg"));
    182         label_3.setBounds(981, 110, 66, 15);
    183         contentPane.add(label_3);
    184         
    185         JLabel label_4 = new JLabel("");
    186         label_4.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hewolianxi.jpg"));
    187         label_4.setBounds(981, 141, 66, 15);
    188         contentPane.add(label_4);
    189         
    190         JLabel label_5 = new JLabel("购买数量");
    191         label_5.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hewolianxi.jpg"));
    192         label_5.setBounds(981, 172, 66, 15);
    193         contentPane.add(label_5);
    194         
    195         JLabel label_6 = new JLabel("购买数量");
    196         label_6.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hewolianxi.jpg"));
    197         label_6.setBounds(981, 203, 66, 15);
    198         contentPane.add(label_6);
    199         
    200         JLabel label_7 = new JLabel("购买数量");
    201         label_7.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hewolianxi.jpg"));
    202         label_7.setBounds(981, 234, 66, 15);
    203         contentPane.add(label_7);
    204         
    205         JCheckBox checkBox_1 = new JCheckBox("购买");//复选框
    206         checkBox_1.setBounds(828, 75, 73, 23);
    207         contentPane.add(checkBox_1);
    208         
    209         JCheckBox checkBox_2 = new JCheckBox("购买");
    210         checkBox_2.setBounds(828, 106, 73, 23);
    211         contentPane.add(checkBox_2);
    212 //        checkBox.isSelected();    是否被选中
    213         
    214         JCheckBox checkBox_3 = new JCheckBox("购买");
    215         checkBox_3.setBounds(828, 137, 73, 23);
    216         contentPane.add(checkBox_3);
    217         
    218         JCheckBox checkBox_4 = new JCheckBox("购买");
    219         checkBox_4.setBounds(828, 168, 73, 23);
    220         contentPane.add(checkBox_4);
    221         
    222         JCheckBox checkBox_5 = new JCheckBox("购买");
    223         checkBox_5.setBounds(828, 199, 73, 23);
    224         contentPane.add(checkBox_5);
    225         
    226         JCheckBox checkBox_6 = new JCheckBox("购买");
    227         checkBox_6.setBounds(828, 230, 73, 23);
    228         contentPane.add(checkBox_6);
    229         
    230         JButton btnNewButton = new JButton("结算");
    231         btnNewButton.setIcon(new ImageIcon("D:\Desktop\maji\html\img\gwc_buy.gif"));
    232         btnNewButton.addActionListener(new ActionListener() {
    233             public void actionPerformed(ActionEvent e) {
    234                 float shiS = Float.valueOf(textField_1.getText());//实收金额
    235                 float zhL = shiS - sumPrice;        //找零
    236                 textField_2.setText(zhL+"");//在textField_2中显示
    237             }
    238         });
    239         btnNewButton.setForeground(Color.RED);
    240         btnNewButton.setBackground(Color.PINK);
    241         btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
    242         btnNewButton.setBounds(891, 352, 126, 40);
    243         contentPane.add(btnNewButton);
    244         
    245         JButton button = new JButton("确认");
    246         button.setIcon(new ImageIcon("D:\Desktop\maji\html\img\finalbutton.gif"));
    247         button.addActionListener(new ActionListener() {//监听事件
    248             public void actionPerformed(ActionEvent arg0) {
    249                 textArea.setText("");//初始化文本域内容为空
    250                 sumPrice = 0;
    251                 if(checkBox_1.isSelected()){//如果被选中
    252                     float pri = 298*Integer.valueOf(comboBox_0.getSelectedItem().toString());//单类价格
    253                     sumPrice +=pri;                                                            //累计价格
    254                     textArea.setText(textArea.getText()+
    255                             "adidasu5706u9886polou886B	"+
    256                             comboBox_0.getSelectedItem()+"
    ");//在textArea中显示
    257                 }
    258                 if(checkBox_2.isSelected()){//如果复选框被选中
    259                     float pri = 3888*Integer.valueOf(comboBox_1.getSelectedItem().toString());//单类价格
    260                     sumPrice +=pri;                                                            //累计价格
    261                     textArea.setText(textArea.getText()+
    262                             "u683Cu529Bu7A7Au8C03	"+
    263                             comboBox_1.getSelectedItem()+"
    ");//在textArea中显示
    264                 }
    265                 if(checkBox_3.isSelected()){//如果被选中
    266                     float pri = 368*Integer.valueOf(comboBox_2.getSelectedItem().toString());//单类价格
    267                     sumPrice +=pri;                                                            //累计价格
    268                     textArea.setText(textArea.getText()+
    269                             "Niceu5E73u5E95u8FD0u52A8u978Bu900Fu6C14u578B	"+
    270                             comboBox_2.getSelectedItem()+"
    ");//在textArea中显示
    271                 }
    272                 if(checkBox_4.isSelected()){//如果被选中
    273                     float pri = 259*Integer.valueOf(comboBox_3.getSelectedItem().toString());//单类价格
    274                     sumPrice +=pri;                                                            //累计价格
    275                     textArea.setText(textArea.getText()+
    276                             "u9ED1u767Du6237u5916u53CCu80A9u80CCu5305	"+
    277                             comboBox_3.getSelectedItem()+"
    ");//在textArea中显示
    278                 }
    279                 if(checkBox_5.isSelected()){//如果被选中
    280                     float pri = 1988*Integer.valueOf(comboBox_4.getSelectedItem().toString());//单类价格
    281                     sumPrice +=pri;                                                            //累计价格
    282                     textArea.setText(textArea.getText()+
    283                             "chanelu7537u58EBu9999u6C34	"+
    284                             comboBox_4.getSelectedItem()+"
    ");//在textArea中显示
    285                 }
    286                 if(checkBox_6.isSelected()){//如果被选中
    287                     float pri = 3988*Integer.valueOf(comboBox_5.getSelectedItem().toString());//单类价格
    288                     sumPrice +=pri;                                                            //累计价格
    289                     textArea.setText(textArea.getText()+
    290                             "u534Eu7855FL5900	"+
    291                             comboBox_5.getSelectedItem()+"
    ");//在textArea中显示
    292                 }
    293                 textField.setText(sumPrice+"");//在textField中显示sumPrice
    294             }
    295         });
    296         button.setBackground(new Color(255, 182, 193));
    297         button.setBounds(891, 43, 126, 26);
    298         contentPane.add(button);
    299         
    300         JLabel label_8 = new JLabel("选中的商品及数量");
    301         label_8.setIcon(new ImageIcon("D:\Desktop\maji\html\img\car_step8.gif"));
    302         label_8.setHorizontalAlignment(SwingConstants.CENTER);
    303         label_8.setBounds(10, 279, 134, 23);
    304         contentPane.add(label_8);
    305         
    306         JLabel label_9 = new JLabel("商品列表");
    307         label_9.setIcon(new ImageIcon("D:\Desktop\maji\html\img\hottitle.gif"));
    308         label_9.setBounds(10, 25, 126, 15);
    309         contentPane.add(label_9);
    310         
    311         JLabel label_10 = new JLabel("");
    312         label_10.setIcon(new ImageIcon("D:\Desktop\maji\html\img\logo.gif"));
    313         label_10.setBounds(473, 10, 95, 30);
    314         contentPane.add(label_10);
    315     }
    316 }

    运行效果:

  • 相关阅读:
    java位运算
    AmCharts realtime flush example //add by liuwei 20120929
    配置Linux—LVS (DR)
    LVS(Linux Virtual Server) 学习笔记
    一个由sizeof引出的有意思的问题
    关于IsDebuggerPresent
    我的第一个python程序
    听Robert C. Richardson的报告会很失望
    杯具了,为啥不去tencent的实习生招聘呢
    通过信号量机制解决生产者消费者问题的模拟程序
  • 原文地址:https://www.cnblogs.com/-maji/p/7152350.html
Copyright © 2020-2023  润新知