📄 createbutton.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreateButton extends JPanel
{
//界面组件部分
private JButton useButton[],normalButton[],noroptButton[],logicButton[],mButton[],capButton[],scienceButton[];
private JCheckBox checkBox;
private JRadioButton radioButton[];
private ButtonGroup decGroup,angleGroup,byteGroup;
private JTextField displayField,layerField,memField;
private Container container;
//计算器状态参数
private String display,memory;
private int carry,layer,fe,point,next,forword;
private EvaluateExpression expression;
/******************************空构造函数**********************************/
public CreateButton( )
{}
/****************************带参数构造函数********************************/
public CreateButton( int layout )
{
useButton = new JButton[3];
noroptButton = new JButton[2];
normalButton = new JButton[16];
logicButton = new JButton[8];
mButton = new JButton[5];
capButton = new JButton[6];
scienceButton = new JButton[15];
radioButton = new JRadioButton[8];
initGUI();
initVariable();
handlerGUI();
if(layout==1)scienceLayout();
else normalLayout();
}//end method CreateButton
/******************************初始化GUI组件******************************/
private void initGUI()
{
int i=0;
String normal[] = {"7","8" ,"9","/","4" ,"5","6","*","1","2","3","-","0","+/-", ".","+"};
for( i=0; i<normalButton.length; i++){
normalButton[i] = new JButton( normal[i] );
normalButton[i].setForeground(new Color(0,0,255));
}
noroptButton[0] = new JButton( "sqrt" );
noroptButton[0].setForeground(new Color(0,0,255));
noroptButton[1] = new JButton( "%" );
noroptButton[1].setForeground(new Color(0,0,255));
String use[] = {"BackSpace","CE","C"};
for( i=0; i<useButton.length; i++){
useButton[i] = new JButton( use[i] );
useButton[i].setForeground(new Color(255,0,0));
}
String logic[] = {"Mod","And","Or","Xor","Lsh","Not","=","Int"};
for( i=0; i<logicButton.length; i++){
logicButton[i] = new JButton( logic[i] );
logicButton[i].setForeground(new Color(255,0,0));
}
String m[] = {"MC","MR","MS","M+","pi"};
for( i=0; i<mButton.length; i++){
mButton[i] = new JButton( m[i] );
mButton[i].setForeground(new Color(255,0,0));
}
mButton[4].setForeground(new Color(0,0,255));
String cap[] = {"A","B","C","D","E","F"};
for( i=0; i<capButton.length; i++){
capButton[i] = new JButton( cap[i] );
capButton[i].setForeground( new Color(0,0,255));
capButton[i].setEnabled( false );
}
String science[] = {"F-E","(",")","dms","Exp","ln" ,"sin","x^y","log","cos","x^3","n!","tan","x^2","1/x"};
for( i=0; i<scienceButton.length; i++){
scienceButton[i] = new JButton( science[i] );
scienceButton[i].setForeground(new Color(255,0,255));
}
String radio[] ={"十六进制","十进制" ,"八进制" ,"二进制","角度","弧度","单字","字节"};
for( i=0; i<radioButton.length; i++){
radioButton[i] = new JRadioButton( radio[i], false );
if( i<4 )radioButton[i].addItemListener( new RadioHandler() );
}
radioButton[1].setSelected( true );
radioButton[4].setSelected( true );
radioButton[7].setSelected( true );
decGroup = new ButtonGroup();
for( i=0; i<4; i++)decGroup.add( radioButton[i] );
angleGroup = new ButtonGroup();
for( i=4; i<6; i++)angleGroup.add( radioButton[i] );
byteGroup = new ButtonGroup();
for( i=6; i<8; i++)byteGroup.add( radioButton[i] );
checkBox = new JCheckBox( "Inv" );
memField = new JTextField( 3 );
memField.setEditable( false );
memField.setHorizontalAlignment(JTextField.CENTER);
layerField = new JTextField( 3 );
layerField.setEditable( false );
layerField.setHorizontalAlignment(JTextField.CENTER);
displayField = new JTextField( 20 );
displayField.setEditable( false );
displayField.setText("0.");
displayField.setHorizontalAlignment(JTextField.RIGHT);
displayField.setBackground(new Color(255, 255, 255));
}//end method initGUI
/******************************初始化参数*********************************/
private void initVariable()
{
expression = new EvaluateExpression(); //表达式储存器
fe = 0; //是否以科学记数格式显示
layer = 0; //括号层数
carry = 10; //进制
point = 0; //小数点防重复机制
memory = "0"; //储存器
display = "0"; //显示
next = 2; //下一次不能输入,0数字,1左括号,-1右括号,2双目运算符
forword = 0; //上一次输入,0数字,2双目运算符
}//end method initVariable
/******************************普通计算器界面布局*************************/
private void normalLayout()
{
this.setLayout( new BorderLayout( 10, 10 ) );
JPanel normalPanel = new JPanel();
normalPanel.setLayout( new GridLayout( 4, 5, 5, 5 ) );
JPanel mPanel = new JPanel();
mPanel.setLayout( new GridLayout( 4, 1, 5, 5 ) );
JPanel usePanel = new JPanel();
usePanel.setLayout( new GridLayout( 1, 6, 5, 5 ) );
int i=0;
for( i=0; i<useButton.length; i++ )
usePanel.add( useButton[ i ] );
for( i=0; i<normalButton.length; i++ ){
normalPanel.add( normalButton[i] );
if( (i+1)%4==0 ){
normalButton[i].setForeground(new Color(255,0,0));
if( (i+1)/4==1)normalPanel.add( noroptButton[0] );
else if( (i+1)/4==2)normalPanel.add( noroptButton[1] );
else if( (i+1)/4==3)normalPanel.add( scienceButton[14] );
else if( (i+1)/4==4)normalPanel.add( logicButton[6] );
}
}
scienceButton[14].setForeground(new Color(0,0,255));
for( i=0; i<mButton.length-1; i++ )
mPanel.add( mButton[ i ] );
JPanel mainUpPanel = new JPanel();
mainUpPanel.setLayout( new GridLayout( 1, 1, 10, 10 ) );
mainUpPanel.add( displayField );
JPanel mainMidPanel = new JPanel();
mainMidPanel.setLayout( new BorderLayout( 10, 10 ) );
mainMidPanel.add( memField, BorderLayout.WEST );
mainMidPanel.add( usePanel,BorderLayout.EAST );
JPanel mainDownPanel = new JPanel();
mainDownPanel.setLayout( new BorderLayout( 10, 10 ) );
mainDownPanel.add( mPanel, BorderLayout.WEST );
mainDownPanel.add( normalPanel, BorderLayout.CENTER );
this.add( mainUpPanel, BorderLayout.NORTH );
this.add( mainMidPanel, BorderLayout.CENTER );
this.add( mainDownPanel, BorderLayout.SOUTH );
}//end method normalLayout
/******************************科学计算器界面布局*************************/
private void scienceLayout()
{
this.setLayout( new BorderLayout( 10, 10 ) );
JPanel normalPanel = new JPanel();
normalPanel.setLayout( new GridLayout( 5, 6, 5, 5 ) );
JPanel mPanel = new JPanel();
mPanel.setLayout( new GridLayout( 5, 1, 5, 5 ) );
JPanel usePanel = new JPanel();
usePanel.setLayout( new GridLayout( 1, 6, 5, 5 ) );
JPanel sciencePanel = new JPanel();
sciencePanel.setLayout( new GridLayout( 5, 3, 5, 5 ) );
JPanel checkPanel = new JPanel();
checkPanel.setLayout( new GridLayout( 1, 4, 8, 8 ) );
Box radioHorizontal = Box.createHorizontalBox();
int i=0;
for( i=0; i<useButton.length; i++ )
usePanel.add( useButton[ i ] );
for( i=0; i<scienceButton.length; i++ )
sciencePanel.add( scienceButton[i] );
for( i=0; i<normalButton.length; i++ ){
normalPanel.add( normalButton[i] );
if( (i+1)%4==0 ){
normalButton[i].setForeground(new Color(255,0,0));
normalPanel.add( logicButton[0+(i/4)*2] );
normalPanel.add( logicButton[1+(i/4)*2] );
}
}
for( i=0; i<capButton.length; i++ )
normalPanel.add( capButton[i] );
for( i=0; i<mButton.length; i++ )
mPanel.add( mButton[ i ] );
checkPanel.add( checkBox );
checkPanel.add( layerField );
checkPanel.add( memField );
for( i=0; i<radioButton.length; i++ ){
radioHorizontal.add( radioButton[i] );
if( i==3 || i==5 )radioHorizontal.add( Box.createHorizontalStrut( 30 ));
}
JPanel mainUpPanel = new JPanel();
mainUpPanel.setLayout( new GridLayout( 2, 1, 10, 10 ) );
mainUpPanel.add( displayField );
mainUpPanel.add( radioHorizontal );
JPanel mainMidPanel = new JPanel();
mainMidPanel.setLayout( new BorderLayout( 10, 10 ) );
mainMidPanel.add( checkPanel, BorderLayout.WEST );
mainMidPanel.add( usePanel,BorderLayout.EAST );
JPanel mainDownPanel = new JPanel();
mainDownPanel.setLayout( new BorderLayout( 10, 10 ) );
mainDownPanel.add( sciencePanel, BorderLayout.WEST );
mainDownPanel.add( mPanel, BorderLayout.CENTER );
mainDownPanel.add( normalPanel, BorderLayout.EAST );
this.add( mainUpPanel, BorderLayout.NORTH );
this.add( mainMidPanel, BorderLayout.CENTER );
this.add( mainDownPanel, BorderLayout.SOUTH );
}//end method scienceLayout
/*************************提交GUI组件监听方式*****************************/
private void handlerGUI()
{
allOptr optrHandler = new allOptr(); //运算符监听
Input inputHandler = new Input(); //输入监听
noroptButton[0].addActionListener( optrHandler );
noroptButton[1].addActionListener( optrHandler );
for( int i=0; i<useButton.length; i++ )
useButton[i].addActionListener( optrHandler );
for( int i=0; i<logicButton.length; i++ )
logicButton[i].addActionListener( optrHandler );
for( int i=0; i<mButton.length; i++ )
mButton[i].addActionListener( optrHandler );
for( int i=0; i<scienceButton.length; i++ )
scienceButton[i].addActionListener( optrHandler );
int array1[]= {3,7,11,15};
for( int i=0; i<array1.length; i++ )
normalButton[array1[i]].addActionListener( optrHandler );
int array2[] = {0,1,2,4,5,6,8,9,10,12,13,14};
for( int i=0; i<array2.length; i++ )
normalButton[array2[i]].addActionListener( inputHandler );
for( int i=0; i<capButton.length; i++ )
capButton[i].addActionListener( inputHandler );
}//end method handlerGUI
/*************************************************************************/
/***********************进制转换事件监听器********************************/
/*************************************************************************/
private class RadioHandler implements ItemListener
{
private boolean th16=false,th10=false,th8=false,th2=false;
public void itemStateChanged( ItemEvent event )
{
int fcarry = carry;
if( event.getSource()==radioButton[0] )
{
th16 = true;
carry = 16;
}
else if( event.getSource()==radioButton[2] )
{
th8=true;
carry = 8;
}
else if( event.getSource()==radioButton[3] )
{
th2=true;
carry = 2;
}
else
{
th10=true;
carry = 10;
}
for( int i=0; i<capButton.length; i++)
capButton[i].setEnabled( th16 );
normalButton[9].setEnabled( !th2 ); //2
normalButton[10].setEnabled( !th2 ); //3
normalButton[4].setEnabled( !th2 ); //4
normalButton[5].setEnabled( !th2 ); //5
normalButton[6].setEnabled( !th2 ); //6
normalButton[0].setEnabled( !th2 ); //7
normalButton[1].setEnabled( th10||th16 ); //8
normalButton[2].setEnabled( th10||th16 ); //9
scienceButton[0].setEnabled( th10 ); //F-E
scienceButton[3].setEnabled( th10 ); //dms
scienceButton[6].setEnabled( th10 ); //sin
scienceButton[9].setEnabled( th10 ); //cos
scienceButton[12].setEnabled( th10 ); //tan
scienceButton[4].setEnabled( th10 ); //Exp
mButton[4].setEnabled( th10 ); //pi
radioButton[4].setEnabled( th10 );
radioButton[5].setEnabled( th10 );
radioButton[6].setEnabled( !th10 );
radioButton[7].setEnabled( !th10 );
if( display != null )
if(!display.equals("0")){
display = mToN( fcarry, carry, display );
showResult( );
}
}//end method itemStateChanged of class RadioHandler
}//end inner class RadioHandler
/*************************************************************************/
/*********************运算符输入事件监听器********************************/
/*************************************************************************/
private class allOptr implements ActionListener
{
public void actionPerformed( ActionEvent E )
{
if(!inputLegal( E.getActionCommand() ))return;
else
{
runSingalOptr( E.getActionCommand() );
showResult();
}
}//end method actionPerformed of class allOptr
public void runSingalOptr( String optr )
{
if( optr.equals("F-E") )fe();
else if( optr.equals("sqrt") )sqrt();
else if( optr.equals("%") )percent();
else if( optr.equals("dms") )dms();
else if( optr.equals("sin") )sin();
else if( optr.equals("cos") )cos();
else if( optr.equals("tan") )tan();
else if( optr.equals("x^3") )xpow(3);
else if( optr.equals("x^2") )xpow(2);
else if( optr.equals("ln") )ln();
else if( optr.equals("log") )log();
else if( optr.equals("n!") )display = ""+nFactorial( (int)Double.parseDouble(display));
else if( optr.equals("1/x") )xpow(-1);
else if( optr.equals("Int") )intX();
else if( optr.equals("MC") )mc();
else if( optr.equals("MR") )mr();
else if( optr.equals("MS") )ms();
else if( optr.equals("M+") )mp();
else if( optr.equals("pi") )display=""+3.1415926535897932384626433832795;
else if( optr.equals("Not") )notX();
else if( optr.equals("C") ) clearAll();
else if( optr.equals("CE") )display = "0";
else if( optr.equals("BackSpace") ) backSpace();
else if( optr.equals("(") || optr.equals(")") )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -