组件编号/类型 | 名称 /属性 |
---|---|
1/Label | label/基础水价 |
2/Label | label_1/月用水量 |
3/Label | label_2/家庭人数 |
4/Button | button/水费计算 |
5/Label | label_3/应交水费 |
6/TextField | textField/3.7 |
7TextField | textField_1/ |
8/TextField | textField_2/ |
9/Button | button_1/清屏复位 |
10/Label | label_4/ |
(左边一列依次编号为1-5,右边一列依次编号为6-10)
水费计算:获取用户输入信息。
double price,t,RMB;/*基础水价,家庭月用水量,当月应缴纳
水费总额*/
int i,n;
String str;
str=textField.getText();
price=Double.parseDouble(str);
str=textField_1.getText();
t=Double.parseDouble(str);
str=textField_2.getText();
n=Integer.parseInt(str);//家庭人数```
通过三目运算符判断用户用水量属于哪一阶梯。
i=(t<n*3.5 ? 1:0)+(t>3.5 && t<=n*3.5*1.2 ? 2:0)+
(t>n*3.5*1.2 && t<=n*3.5*1.4 ? 3:0)+
(t>n*3.5*1.4 ? 4:0);
switch语句进行水费的计算。
switch(i) {
case 1:RMB=t*price;
break;
case 2:RMB=n*3.5*price+(t-n*3.5)*price*2;
break;
case 3:RMB=n*3.5*price+n*3.5*(1.2-1)*price*2+n*3.5*
(1.4-1.2)*price*3+(t-n*3.5*(1+0.4))*price*4;
break;
case 4:RMB=n*3.5*price+n*3.5*(1.2-1)*price*2+
n*3.5*(1.4-1.2)*price*3+
(t-n*3.5*(1+0.4))*price*4;
break;
default:RMB=-1;
错误提示。
if(RMB!=-1) {
str=Double.toString(RMB);
label_4.setText(str);}
else
str="Error,please again!";
label_4.setText(str);
}
清屏复功能实现。
String nl="";
textField.setText(nl);
textField_1.setText(nl);
textField_2.setText(nl);
label_4.setText(nl);
package 阶梯型水费收取;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 529, 391);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Label label = new Label("\u57FA\u672C\u6C34\u4EF7");
label.setBounds(67, 27, 69, 23);
contentPane.add(label);
Label label_1 = new Label("\u6708\u7528\u6C34\u91CF");
label_1.setBounds(67, 86, 69, 23);
contentPane.add(label_1);
Label label_2 = new Label("\u5BB6\u5EAD\u4EBA\u6570");
label_2.setBounds(67, 154, 69, 23);
contentPane.add(label_2);
TextField textField = new TextField();
textField.setText("3.7");
textField.setBounds(207, 27, 117, 23);
contentPane.add(textField);
TextField textField_1 = new TextField();
textField_1.setBounds(210, 86, 114, 23);
contentPane.add(textField_1);
TextField textField_2 = new TextField();
textField_2.setBounds(207, 154, 117, 23);
contentPane.add(textField_2);
Label label_4 = new Label("New label");
label_4.setBounds(207, 301, 142, 23);
contentPane.add(label_4);
Button button = new Button("\u6C34\u8D39\u8BA1\u7B97");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
double price,t,RMB;/*基础水价,家庭月用水量,当月应缴纳
水费总额*/
int i,n;
String str;
str=textField.getText();
price=Double.parseDouble(str);
str=textField_1.getText();
t=Double.parseDouble(str);
str=textField_2.getText();
n=Integer.parseInt(str);//家庭人数
i=(t<n*3.5 ? 1:0)+(t>n*3.5 && t<=n*3.5*1.2 ? 2:0)+
(t>n*3.5*1.2 && t<=n*3.5*1.4 ? 3:0)+
(t>n*3.5*1.4 ? 4:0);
switch(i) {
case 1:RMB=t*price;
break;
case 2:RMB=n*3.5*price+(t-n*3.5)*price*2;
break;
case 3:RMB=n*3.5*price+n*3.5*(1.2-1)*price*2+n*3.5*
(1.4-1.2)*price*3+(t-n*3.5*(1+0.4))*price*4;
break;
case 4:RMB=n*3.5*price+n*3.5*(1.2-1)*price*2+
n*3.5*(1.4-1.2)*price*3+
(t-n*3.5*(1+0.4))*price*4;
break;
default:RMB=-1;
}
if(RMB!=-1) {
str=Double.toString(RMB);
label_4.setText(str);}
else
str="Error,please again!";
label_4.setText(str);
}
});
button.setBounds(99, 254, 76, 23);
contentPane.add(button);
Button button_1 = new Button("\u6E05\u5C4F\u590D\u4F4D");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String nl="";
textField.setText(nl);
textField_1.setText(nl);
textField_2.setText(nl);
label_4.setText(nl);
}
});
button_1.setBounds(289, 254, 76, 23);
contentPane.add(button_1);
Label label_3 = new Label("\u5E94\u4EA4\u6C34\u8D39");
label_3.setBounds(67, 301, 69, 23);
contentPane.add(label_3);
}
}