📄 setbasedlg.java
字号:
package src;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
/*********************************
*Dialog used to change the value*
*of baseIncome in TaxCalculator *
*@see TaxCalulator *
*********************************/
class BaseDialog extends JDialog{
private JLabel baseLabel=new JLabel("BaseIncome:");
private JTextField baseJF=new JTextField(10);
private JButton confirm=new JButton(" OK ");
private String baseValueString="1600.0";
private boolean check,firstDec=false;
/***************************************************************
* Class implement ActionListener to respond to JButton action *
***************************************************************/
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==confirm){
check=true;
baseValueString=baseJF.getText();
/*****************************************
* Firstly check if the decimal is legal *
*****************************************/
for(int i=0;i<baseValueString.length();i++){
if(baseValueString.charAt(i)=='.'){
if(firstDec){
check=false;
break;
}
else if(i!=0&&i!=baseValueString.length()-1)
firstDec=true;
else{
check=false;
break;
}
}
else if(!(48<=baseValueString.charAt(i)&&baseValueString.charAt(i)<=57)){
check=false;
break;
}
}
/*****************************************
*secondly confirm the string should only*
*contain 0-9 except for the decimal. *
*****************************************/
if(!check){
JOptionPane.showMessageDialog(null,"Contains illegal charecters.Values RE:[0-9]+ . [0-9]*","ILLEGAL INPUTTING",JOptionPane.OK_OPTION);
baseJF.setText("");
baseJF.grabFocus();
}
else if(Double.parseDouble(baseValueString)<=0){
JOptionPane.showMessageDialog(null,"Please input a positive number!","Illegal Number",JOptionPane.OK_OPTION);
baseJF.setText("");
baseJF.grabFocus();
}
/*******************************
*If success,dispose the dialog*
*******************************/
else dispose();
}
}
}
/**
* Constructor
* @param parent{@null}
*/
public BaseDialog(JFrame parent){
super(parent,"BaseIncome",true);
baseJF.setText( baseValueString);
Container cp=getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
/**************************************************
*Add components into the Content Pane for Display*
**************************************************/
cp.add(baseLabel);
cp.add(baseJF);
cp.add(confirm);
/*************************
*Register ActionListener*
*************************/
confirm.addActionListener(new ButtonListener());
setSize(300,100);
}
/**
* Get base value as a String
* @return String
*/
public String getBaseValueString(){
return baseValueString;
}
}
/**
* Load the Dialog into JApplet
* @author ShanNa
*/
public class SetBaseDlg extends JApplet{
BaseDialog dlg=new BaseDialog(null);
String baseValue;
public void init(){
dlg.show();
baseValue=dlg.getBaseValueString();
}
public String getBaseValue(){
return baseValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -