import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Menoy extends JFrame{
JTextField text1;
JButton but1,but2;
JRadioButton Home,Industry;
ButtonGroup radioGroup;
JLabel lbl1;
public int a;
public Menoy(){
super("电费收费系统");
Container c=getContentPane();
setSize(350,300);
c.setBackground(Color.blue);
lbl1=new JLabel("请选择用电类型");
lbl1.setBounds(30,20,350,20);
lbl1.setFont(new Font("TimesRoman",Font.BOLD,16));
c.add(lbl1);
text1=new JTextField(50);
text1.setBounds(50,50,250,30);
c.add(text1);
c.setLayout(null);
Home=new JRadioButton("家用电",false);
Home.setBounds(55,90,100,30);
Home.setFont(new Font("TimesRoman",Font.BOLD,14));
c.add(Home);
Home.addItemListener(new handler1());
Industry=new JRadioButton("工业用电",false);
Industry.setBounds(200,90,100,30);
Industry.setFont(new Font("TimesRoman",Font.BOLD,14));
c.add(Industry);
Industry.addItemListener(new handler2());
radioGroup=new ButtonGroup();
radioGroup.add(Home);
radioGroup.add(Industry);
but1=new JButton("计算");
but1.setBounds(60,150,100,40);
but1.setFont(new Font("TimesRoman",Font.BOLD,16));
c.add(but1);
but1.addActionListener(new jishu());
but2=new JButton("清除");
but2.setBounds(180,150,100,40);
but2.setFont(new Font("TimesRoman",Font.BOLD,16));
c.add(but2);
but2.addActionListener(new Clear());
show();
}
public static void main(String ar[]){
Menoy app=new Menoy();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
}
private class handler1 implements ItemListener{
public void itemStateChanged(ItemEvent e){
a=1;
}
}
private class handler2 implements ItemListener{
public void itemStateChanged(ItemEvent e){
a=2;
}
}
private class jishu implements ActionListener{
public void actionPerformed(ActionEvent e){
float value=(new Float(text1.getText())).floatValue();
if(a==1)
{ lbl1.setText("家用电");
if(value<110)
{ lbl1.setText("家用电在110度内:价格3元一度电:");
text1.setText(String.valueOf(value*3));}
else if(value<330)
{ lbl1.setText("家用电在110到330度之间:价格3.3元一度电:");
text1.setText(String.valueOf(value*3.3));}
else
{ lbl1.setText("家用电超过330度:价格3.5元一度电:");
text1.setText(String.valueOf(value*3.5));}
}
else if(a==2){
lbl1.setText("工业用电:价格3元一度电");
text1.setText(String.valueOf(value*3));
}
else{lbl1.setText("请选择用电的类型:");}
}
}
private class Clear implements ActionListener{
public void actionPerformed(ActionEvent e){
text1.setText("");
}
}
}
import java.awt.event.*;
import javax.swing.*;public class Menoy extends JFrame{
JTextField text1;
JButton but1,but2;
JRadioButton Home,Industry;
ButtonGroup radioGroup;
JLabel lbl1;
public int a;
public Menoy(){
super("电费收费系统");
Container c=getContentPane();
setSize(350,300);
c.setBackground(Color.blue);
lbl1=new JLabel("请选择用电类型");
lbl1.setBounds(30,20,350,20);
lbl1.setFont(new Font("TimesRoman",Font.BOLD,16));
c.add(lbl1);
text1=new JTextField(50);
text1.setBounds(50,50,250,30);
c.add(text1);
c.setLayout(null);
Home=new JRadioButton("家用电",false);
Home.setBounds(55,90,100,30);
Home.setFont(new Font("TimesRoman",Font.BOLD,14));
c.add(Home);
Home.addItemListener(new handler1());
Industry=new JRadioButton("工业用电",false);
Industry.setBounds(200,90,100,30);
Industry.setFont(new Font("TimesRoman",Font.BOLD,14));
c.add(Industry);
Industry.addItemListener(new handler2());
radioGroup=new ButtonGroup();
radioGroup.add(Home);
radioGroup.add(Industry);
but1=new JButton("计算");
but1.setBounds(60,150,100,40);
but1.setFont(new Font("TimesRoman",Font.BOLD,16));
c.add(but1);
but1.addActionListener(new jishu());
but2=new JButton("清除");
but2.setBounds(180,150,100,40);
but2.setFont(new Font("TimesRoman",Font.BOLD,16));
c.add(but2);
but2.addActionListener(new Clear());
show();
}
public static void main(String ar[]){
Menoy app=new Menoy();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
}
private class handler1 implements ItemListener{
public void itemStateChanged(ItemEvent e){
a=1;
}
}
private class handler2 implements ItemListener{
public void itemStateChanged(ItemEvent e){
a=2;
}
}
private class jishu implements ActionListener{
public void actionPerformed(ActionEvent e){
float value=(new Float(text1.getText())).floatValue();
if(a==1)
{ lbl1.setText("家用电");
if(value<110)
{ lbl1.setText("家用电在110度内:价格3元一度电:");
text1.setText(String.valueOf(value*3));}
else if(value<330)
{ lbl1.setText("家用电在110到330度之间:价格3.3元一度电:");
text1.setText(String.valueOf(value*3.3));}
else
{ lbl1.setText("家用电超过330度:价格3.5元一度电:");
text1.setText(String.valueOf(value*3.5));}
}
else if(a==2){
lbl1.setText("工业用电:价格3元一度电");
text1.setText(String.valueOf(value*3));
}
else{lbl1.setText("请选择用电的类型:");}
}
}
private class Clear implements ActionListener{
public void actionPerformed(ActionEvent e){
text1.setText("");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货