📄 taxvalue.java
字号:
package src;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
/**************************************************************
* Class implements JDialog to get the amount and rate values*
* in order to insert a new item into @see TaxLevelTable or to*
* update a item in the @see TaxLevelTable. *
**************************************************************/
class ValueDialog extends JDialog{
JLabel amountLabel=new JLabel("Amount:"),rateLabel=new JLabel("Rate:");
JTextField amountJF=new JTextField(10),rateJF=new JTextField(10);
JButton confirm=new JButton(" OK ");
String amount,rate;
double preAmount,preRate,nextAmount,nextRate;
/**************************************************************
* Class implement ActionListener to respond to JButton action*
**************************************************************/
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==confirm){
boolean firstCheck=true;
amount=amountJF.getText();
rate=rateJF.getText();
boolean rateDec=false,amouDec=false;
for(int i=0;i<rate.length();i++){
/***************************************
*Firstly check if the decimal is legal*
***************************************/
if(rate.charAt(i)=='.'){
if(rateDec){
firstCheck=false;
break;
}
else if(i!=0&&i!=rate.length()-1)
rateDec=true;
else {
firstCheck=false;
break;
}
}
/******************************************************
*Secondly check if other charters are between 0 and 9*
******************************************************/
else if(!(48<=rate.charAt(i)&&rate.charAt(i)<=57)){
firstCheck=false;
break;
}
}
if(firstCheck){
for(int i=0;i<amount.length();i++)
if(amount.charAt(i)=='.'){
if(amouDec){
firstCheck=false;
break;
}
else if(i!=0&&i!=amount.length()-1)
amouDec=true;
else{
firstCheck=false;
break;
}
}
else if(!(48<=amount.charAt(i)&&amount.charAt(i)<=57)){
firstCheck=false;
break;
}
}
/***********************************************************
*If the inputting string is illegal,prompt a MessageDialog*
*for reminding,and then wait for the next inputting *
***********************************************************/
if(!firstCheck){
JOptionPane.showMessageDialog(null,"Contains illegal charecters.Values RE:[0-9]+ . [0-9]*","ILLEGAL INPUTTING",JOptionPane.OK_OPTION);
amountJF.setText("");
rateJF.setText("");
amountJF.grabFocus();
}
/**********************************************************
*If the inputting string is legal,then check if the value*
*is between its two neighbors in the @see TaxTableLevel * *
**********************************************************/
else{
if(Double.parseDouble(amount)<=nextAmount||Double.parseDouble(amount)>=preAmount){
JOptionPane.showMessageDialog(null,"Amount: "+nextAmount+" < Amount < "+preAmount+" Input again!","Illegal Number",JOptionPane.OK_OPTION);
amountJF.setText("");
amountJF.grabFocus();
}
else if(Double.parseDouble(rate)<=nextRate||Double.parseDouble(rate)>=preRate){
JOptionPane.showMessageDialog(null,"Rate: "+nextRate+" < Rate < "+preRate+" Input again!","Illegal Number",JOptionPane.OK_OPTION);
rateJF.setText("");
rateJF.grabFocus();
}
else dispose();
}
}
}
}
/************************************************
*Create a instance for the class ButtonListener*
************************************************/
ButtonListener bl=new ButtonListener();
public ValueDialog(JFrame parent,String amValue,String raValue,double nextAmount,double preAmount,double nextRate,double preRate){
super(parent,"TaxLevel",true);
this.preAmount=preAmount;
this.preRate=preRate;
this.nextAmount=nextAmount;
this.nextRate=nextRate;
amount=amValue;
rate=raValue;
amountJF.setText(amount);
rateJF.setText(rate);
Container cp=getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
/*******************************************
*Add items to the Content Pane for display*
*******************************************/
cp.add(amountLabel);
cp.add(amountJF);
cp.add(rateLabel);
cp.add(rateJF);
cp.add(confirm);
/**************************
*Register event listeners*
**************************/
confirm.addActionListener(bl);
setSize(400,125);
}
public String getAmount(){
return amount;
}
public String getRate(){
return rate;
}
}
/******************************************
*Package the ValueDialog into the JApplet*
******************************************/
public class TaxValue extends JApplet{
String amountValue,rateValue;
double preAmount,preRate,nextAmount,nextRate;
public TaxValue(){
amountValue="0.0";
rateValue="0.0";
}
/**
* Constructor
* @param amount String
* @param rate String
* @param nextAmount double
* @param preAmount double
* @param nextRate double
* @param preRate double
*/
public TaxValue(String amount,String rate,double nextAmount,double preAmount,double nextRate,double preRate){
amountValue=amount;
rateValue=rate;
this.preAmount=preAmount;
this.preRate=preRate;
this.nextAmount=nextAmount;
this.nextRate=nextRate;
}
public void init(){
ValueDialog dlg=new ValueDialog(null,amountValue,rateValue,nextAmount,preAmount,nextRate,preRate);
dlg.show();
amountValue=dlg.getAmount();
rateValue=dlg.getRate();
}
/**
* Get the tax amount of certain level
* @return String
*/
public String GetTaxValue(){
return amountValue;
}
/**
* Get the tax rate of certain level
* @return String
*/
public String GetRateValue(){
return rateValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -