这是我实训时老师指导做的,分享给大家。- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class Shuifeijs
- {
- private JFrame frm=new JFrame("水费计算");
- private JPanel panel1=new JPanel();
-
- private JLabel label1=new JLabel("用水吨数");
- private JLabel label2=new JLabel("所交水费");
-
- private JTextField text1=new JTextField(10);
- private JTextField text2=new JTextField(10);
-
- private JRadioButton bt1=new JRadioButton("家庭用水",true);
- private JRadioButton bt2=new JRadioButton("工业用水");
- private ButtonGroup btg=new ButtonGroup();
-
- private JButton bjs=new JButton("计算");
- private JButton btc=new JButton("退出");
-
- private class ButtonHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- double fee,temp;
- if(e.getSource()==(JButton)bjs)
- {
- temp=Double.parseDouble(text1.getText());
- if(bt1.isSelected())
- {
- if(temp<15)fee=temp*3.0;
- else if(temp<30)fee=15*3.0+(temp-15)*6.0;
- else fee=temp*8.0;
- }
- else fee=temp*0.7;
- text2.setText(fee+"元");
- }
- else System.exit(0);
- }
- }
- public Shuifeijs()
- {
- //Container cp=frm.getContenPane();
- btg.add(bt1);
- btg.add(bt2);
- panel1.add(label1);
- panel1.add(text1);
- panel1.add(bt1);
- panel1.add(bt2);
- panel1.add(label2);
- panel1.add(text2);
- panel1.add(bjs);
- panel1.add(btc);
- frm.add(panel1);
-
- ButtonHandler buttonListener=new ButtonHandler();
- bjs.addActionListener(buttonListener);
- btc.addActionListener(buttonListener);
-
- frm.setSize(200,180);
- frm.setVisible(true);
- frm.setResizable(false);
- frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
- frm.setLocation((d.width-200)/2,(d.height-120)/2);
- }
-
- public static void main(String[]args)
- {
- new Shuifeijs();
- }
- }
复制代码 |
|