Topic: 关于上次的问题!! |
Print this page |
1.关于上次的问题!! | Copy to clipboard |
Posted by: dalor_cheng Posted on: 2005-12-27 09:37 我上次问的问题没有一个人回答,我很失望,现在我完成 了. 不要小看我 还有上次那位向我要Money的那位老兄,你的眼里只有钱,无耻!!!! import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Shopping implements ItemListener,ActionListener { JCheckBox cb1,cb2,cb3; JRadioButton rb1,rb2; ButtonGroup group=new ButtonGroup(); JLabel lbl; JButton btn; int cost,totalCost; double discount =0.9; public static void main(String args[]) { ImageIcon icon=new ImageIcon("3.jpg"); JFrame frame=new JFrame("123网上购物"); Shopping app=new Shopping(); JPanel pane=app.createComponents(); frame.getContentPane().add(pane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(350,200); frame.setVisible(true); frame.setIconImage(icon.getImage()); } public JPanel createComponents() { JPanel mainpane=new JPanel(); JPanel paneC=new JPanel(); JPanel paneR=new JPanel(); paneC.setLayout(new BoxLayout(paneC,BoxLayout.PAGE_AXIS)); paneR.setLayout(new BoxLayout(paneR,BoxLayout.PAGE_AXIS)); btn=new JButton("购买"); lbl=new JLabel("总金额: 0 $ "); mainpane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); //paneC.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitleBorder("产品价格"),BorderFactory.createEmptyBorder(5,5,5,5)));//30 //paneR.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitleBorder("会员优费"),BorderFactory.createEmptyBorder(5,5,5,5))); cb1=new JCheckBox("复印机(4688)",false); cb1.addItemListener(this); paneC.add(cb1); cb2=new JCheckBox("复印机(8898)",false); cb2.addItemListener(this); paneC.add(cb2); cb3=new JCheckBox("复印机(2780)",false); cb3.addItemListener(this); paneC.add(cb3); rb1=new JRadioButton("VIP会员(10%)"); rb1.addActionListener(this); rb1.setSelected(true); paneR.add(rb1); rb2=new JRadioButton("普通会员(2%)"); rb2.addActionListener(this); btn.addActionListener(this); paneR.add(rb2); group.add(rb1); group.add(rb2); mainpane.add(paneC); mainpane.add(paneR); mainpane.add(lbl); mainpane.add(btn); return mainpane; } public void itemStateChanged(ItemEvent e) { JCheckBox cb=(JCheckBox)e.getItem(); if(cb==cb1) if(e.getStateChange()==ItemEvent.SELECTED) cost+=4688; else cost-=4688; if(cb==cb2) if(e.getStateChange()==ItemEvent.SELECTED) cost+=8898; else cost-=8898; if(cb==cb3) if(e.getStateChange()==ItemEvent.SELECTED) cost+=2780; else cost-=2780; totalCost=(int)(cost*discount); lbl.setText("总金额:"+totalCost); } public void actionPerformed(ActionEvent e) { if(e.getSource()==btn) lbl.setText("感谢你的购买!"); else if(e.getSource()==rb1) { discount=0.9; totalCost=(int)(cost*discount); lbl.setText("总金额:"+totalCost); } else { discount=0.98; totalCost=(int)(cost*discount); lbl.setText("总金额:"+totalCost); } } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |