diff --git a/a.jpg b/a.jpg new file mode 100644 index 0000000000000000000000000000000000000000..acc9fadb0fa556b5c12ccf0043506c797ddb4af0 Binary files /dev/null and b/a.jpg differ diff --git a/b.jpg b/b.jpg new file mode 100644 index 0000000000000000000000000000000000000000..536342a8858fc25fb07aa17a208b893f48af10ea Binary files /dev/null and b/b.jpg differ diff --git a/src/java2020spring/JSQ.java b/src/java2020spring/JSQ.java new file mode 100644 index 0000000000000000000000000000000000000000..51edc922ec03dd2b37aa89a28c61fd36112d07dd --- /dev/null +++ b/src/java2020spring/JSQ.java @@ -0,0 +1,442 @@ +package java2020spring; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.awt.event.KeyEvent; +import java.awt.event.InputEvent; + +public class JSQ extends Frame implements ActionListener,WindowListener{ +private Container container; +private GridBagLayout layout; +private GridBagConstraints constraints; +private JTextField textField; //输入框 +private String getbutton; //用户点击的按钮 +private double result; +private boolean start; +private JMenuBar menubar; //菜单栏 +private JMenuItem menu_exit; //'关闭'选项 +private JMenuItem menu2_ejz; //'二进制'选项 +private JMenuItem menu2_sjz; //'三进制'选项 +private JMenuItem menu2_bjz; //'八进制'选项 +private JMenuItem menu2_sljz; //'十六进制'选项 +private Dialog dialog; +private Label label_dialog; //'关于'中弹出的对话框 +private JButton button0; +private JButton button1; +private JButton button2; +private JButton button3; +private JButton button4; +private JButton button5; +private JButton button6; +private JButton button7; +private JButton button8; +private JButton button9; +private JButton button_plus; +private JButton button_minus; +private JButton button_multiply; +private JButton button_divide; +private JButton button_point; +private JButton button_equal; +private JButton button_log; +private JButton button_sin; +private JButton button_cos; +private JButton button_tan; +private JButton button_exp; +private JButton button_sqrt; +private JButton button_minussign; //'负号'按钮 +private JButton button_Delete; +private JButton button_CE; + +public JSQ() +{ + +/**设置计算器整体属性**/ + super( "李家祺的计算器" ); + this.setLocation( 400,200 ); + this.setSize(400,300); + this.setResizable(true); + this.setLayout(new GridLayout( 7,2)); + this.addMenu(); + +/**设置输入框属性**/ + textField = new JTextField(50); + textField.setBackground(new Color(230,240,250)); + textField.setForeground(new Color(30,60,90)); + textField.setText("0"); + textField.setFont(new Font("黑体",Font.BOLD,22)); + textField.setEditable(false); + this.setResizable(false); //不可调整输入框大小 + this.add( textField ); + start = true; + result = 0; + getbutton = "="; + +/**第一排布局**/ + JPanel panel0 = new JPanel(); + panel0.setLayout( new GridLayout( 1,4,4,4 ) ); + JPanel panel1 = new JPanel(); + panel1.setLayout( new GridLayout( 1,5,4,4 ) ); + this.add( panel1 ); + button_sqrt = new JButton( "√" ); + button_sqrt.setForeground(new Color(30,60,90)); + button_minussign = new JButton( "-" ); + button_minussign.setForeground(new Color(30,60,90)); + button_exp = new JButton( "exp" ); + button_exp.setForeground(new Color(30,60,90)); + button_Delete = new JButton( "Delete"); + button_Delete.setForeground(new Color(30,60,90)); + button_CE = new JButton( "CE" ); + button_CE.setForeground(new Color(30,60,90)); + +/**第二排布局**/ + JPanel panel2 = new JPanel(); + panel2.setLayout( new GridLayout( 1,5,4,4 ) ); + this.add( panel2 ); + button7 = new JButton( "7" ); + button7.setBackground(new Color(230,240,250)); + button8 = new JButton( "8" ); + button8.setBackground(new Color(230,240,250)); + button9 = new JButton( "9" ); + button9.setBackground(new Color(230,240,250)); + button_log = new JButton( "log" ); + button_log.setForeground(new Color(30,60,90)); + button_divide = new JButton( "/" ); + button_divide.setForeground(new Color(30,60,90)); + +/**第三排布局**/ + JPanel panel3 = new JPanel(); + panel3.setLayout( new GridLayout(1,5,4,4) ); + this.add( panel3 ); + button4 = new JButton( "4" ); + button4.setBackground(new Color(230,240,250)); + button5 = new JButton( "5" ); + button5.setBackground(new Color(230,240,250)); + button6 = new JButton( "6" ); + button6.setBackground(new Color(230,240,250)); + button_tan = new JButton( "tan" ); + button_tan.setForeground(new Color(30,60,90)); + button_multiply = new JButton( "*" ); + button_multiply.setForeground(new Color(30,60,90)); + +/**第四排布局**/ + JPanel panel4=new JPanel(); + panel4.setLayout( new GridLayout( 1,5,4,4 ) ); + this.add(panel4); + button1 = new JButton( "1" ); + button1.setBackground(new Color(230,240,250)); + button2 = new JButton( "2" ); + button2.setBackground(new Color(230,240,250)); + button3 = new JButton( "3" ); + button3.setBackground(new Color(230,240,250)); + button_cos = new JButton( "cos"); + button_cos.setForeground(new Color(30,60,90)); + button_minus = new JButton( "-" ); + button_minus.setForeground(new Color(30,60,90)); + +/**第五排布局**/ + JPanel panel5 = new JPanel(); + panel5.setLayout( new GridLayout( 1,5,4,4 ) ); + this.add( panel5 ); + button_point=new JButton( "." ); + button_point.setBackground(new Color(230,240,250)); + button0 = new JButton( "0" ); + button0.setBackground(new Color(230,240,250)); + button_equal = new JButton( "=" ); + button_equal.setForeground(new Color(30,60,90)); + button_sin = new JButton( "sin" ); + button_sin.setForeground(new Color(30,60,90)); + button_plus = new JButton( "+" ); + button_plus.setForeground(new Color(30,60,90)); + +/**添加点击方法**/ + panel1.add( button_sqrt ); + panel1.add( button_minussign ); + panel1.add( button_exp ); + panel1.add( button_Delete ); + panel1.add( button_CE ); + panel2.add( button7 ); + panel2.add( button8 ); + panel2.add( button9 ); + panel2.add( button_log ); + panel2.add( button_divide ); + panel3.add( button4 ); + panel3.add( button5 ); + panel3.add( button6 ); + panel3.add( button_tan ); + panel3.add( button_multiply ); + panel4.add( button1 ); + panel4.add( button2 ); + panel4.add( button3 ); + panel4.add( button_cos ); + panel4.add( button_minus ); + panel5.add( button_point ); + panel5.add( button0 ); + panel5.add( button_equal ); + panel5.add( button_sin ); + panel5.add( button_plus) ; + +/**设置监听器**/ + button_sqrt.addActionListener( this ); + button_minussign.addActionListener( this ); + button_exp.addActionListener( this ); + button_Delete.addActionListener( this ); + button_CE.addActionListener( this ); + button7.addActionListener( this ); + button8.addActionListener( this ); + button9.addActionListener( this ); + button_log.addActionListener( this ); + button_divide.addActionListener( this ); + button4.addActionListener( this ); + button5.addActionListener( this ); + button6.addActionListener( this ); + button_tan.addActionListener( this ); + button_multiply.addActionListener( this ); + button1.addActionListener( this ); + button2.addActionListener( this ); + button3.addActionListener( this ); + button_cos.addActionListener( this ); + button_minus.addActionListener( this ); + button0.addActionListener( this ); + button_point.addActionListener( this ); + button_equal.addActionListener( this ); + button_sin.addActionListener( this ); + button_plus.addActionListener( this ); + +/**注册窗口监听器**/ + this.addWindowListener( new WinClose() ); + this.setVisible( true ); + } + +/**菜单的添加**/ + private void addMenu(){ + JMenuBar menubar = new JMenuBar(); + this.add( menubar ); + + /**'功能'菜单**/ + JMenu menu1 = new JMenu( "功能" ); + JMenuItem menu1_exit = new JMenuItem( "关闭" ,new ImageIcon("a.jpg"));//添加图标 + menu1_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));//添加快捷方式'Ctrl+X' + JMenuItem menu1_unitconversion = new JMenuItem( "单位转换器",new ImageIcon("b.jpg") );//添加图标 + menu1_unitconversion.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));//添加快捷方式'Ctrl+A' + menu1_unitconversion .addActionListener( this ); + + /**'进制转换'菜单**/ + JMenu menu2 = new JMenu( "进制转换"); + JMenuItem menu2_ejz = new JMenuItem( "二进制" ); + JMenuItem menu2_sjz=new JMenuItem("三进制"); + JMenuItem menu2_bjz = new JMenuItem("八进制"); + JMenuItem menu2_sljz = new JMenuItem("十六进制"); + + /**'关于'菜单**/ + JMenu menu3 =new JMenu("关于"); + JMenuItem menu3_developer=new JMenuItem("开发者"); + JMenuItem menu3_developmentTime=new JMenuItem("开发时间"); + dialog =new Dialog(this,"",true); + dialog.setSize(380,100); + label_dialog =new Label("",Label.CENTER); + dialog.add(label_dialog); + dialog.addWindowListener(this); + + /**添加点击方法**/ + menu1.add(menu1_unitconversion); + menu1.add( menu1_exit ); + menubar.add( menu1 ); + menu2.add( menu2_ejz ); + menu2.add(menu2_sjz); + menu2.add( menu2_bjz ); + menu2.add( menu2_sljz ); + menubar.add( menu2 ); + menu3.add(menu3_developer); + menu3.add(menu3_developmentTime); + menubar.add(menu3); + + /**设置监听器**/ + menu1_exit.addActionListener( this ); + menu2_ejz.addActionListener( this ); + menu2_sjz.addActionListener(this); + menu2_bjz.addActionListener( this ); + menu2_sljz.addActionListener( this ); + menu3_developer.addActionListener(this); + menu3_developmentTime.addActionListener(this); +} + +/**按钮的单击事件处理方法**/ + public void actionPerformed(ActionEvent e){ + if( + e.getSource().equals( button1 )||e.getSource().equals( button2 )|| + e.getSource().equals( button3 )||e.getSource().equals( button4 )|| + e.getSource().equals( button5 )|| e.getSource().equals( button6 )|| + e.getSource().equals( button7 )|| e.getSource().equals( button8 )|| + e.getSource().equals( button9 ) ||e.getSource().equals( button0 )|| + e.getSource().equals( button_point )||e.getSource().equals( button_minussign)|| + e.getSource().equals( button_CE )||e.getSource().equals( button_Delete ) + ) + { + String input = e.getActionCommand(); + if (start) + { + textField.setText(""); + start = false; + if( input.equals( "-" ) ) + textField.setText( textField.getText()+ "-" ); + } + if( !input.equals( "-" ) ) + { + String str = textField.getText(); + if( input.equals( "Delete") ) + { + if( str.length() > 0 ) + textField.setText( str.substring(0,str.length() - 1 ) ); + } + else if( input.equals( "CE" ) ) + { + textField.setText( "0"); + start = true; + } + else + textField.setText( textField.getText() + input ); + } + } + else if ( e.getActionCommand()=="二进制") + { + int n = Integer.parseInt( textField.getText() ); + textField.setText(Integer.toBinaryString(n)); + + } + else if(e.getActionCommand()=="三进制") + { + int n=Integer.parseInt(textField.getText()); + textField.setText(Integer.toString(n,3)); + } + else if ( e.getActionCommand() == "八进制" ) + { + int n = Integer.parseInt( textField.getText() ); + textField.setText( Integer.toOctalString( n ) ); + } + else if ( e.getActionCommand() == "十六进制" ) + { + int n = Integer.parseInt( textField.getText() ); + textField.setText( Integer.toHexString( n ) ); + } + + else if(e.getActionCommand()=="单位转换器") + { + this.dispose(); + ZHQ h=new ZHQ(); + h.main(null); + } + else if ( e.getActionCommand() == "关闭" ) + { + System.exit( 0 ); + } + + else if(e.getActionCommand()=="开发者") + { + label_dialog.setText("202025710311李家祺"); + dialog.setLocation( 400,300 ); + dialog.setVisible( true ); + } + + else if(e.getActionCommand()=="开发时间") + { + label_dialog.setText( "2021年6月11号" ); + dialog.setLocation( 400,300 ); + dialog.setVisible( true ); + } + + else + { + String command = e.getActionCommand(); + if( start ) + { + getbutton = command; + } + else + { + calculate( Double.parseDouble( textField.getText() ) ); + getbutton = command; + start = true; + } + } +} +/**运算**/ + public void calculate( double x ) + { + double a = 0; + if ( getbutton.equals( "+" ) ) result=result+x; + else if (getbutton.equals( "-" ) ) result=result-x; + else if ( getbutton.equals( "*" ) ) result=result*x; + else if ( getbutton.equals( "/" ) ) + { + if (Double.parseDouble(textField.getText()) == 0.0) { + textField.setText("ERROR"); } + else { + result=result/Double.parseDouble(textField.getText());} + } + else if ( getbutton.equals( "=" ) ) result=x; + else if ( getbutton.equals( "√" ) ) + { + if (a>=0) { + a = Math.sqrt(x); + result = a;} + else textField.setText("请输入一个非负数"); + } + else if ( getbutton.equals( "exp" ) ) //返回自然数底数e的参数次方 + { + a = Math.exp(x); + result = a; + } + else if ( getbutton.equals( "log" ) ) //返回参数的自然数底数的对数值 + { + a = Math.log(x); + result = a; + } + else if ( getbutton.equals( "tan" ) ) + { + a = Math.tan(x); + result = a; + } + else if ( getbutton.equals( "cos" ) ) + { + a = Math.cos(x); + result = a; + } + else if ( getbutton.equals( "sin" ) ) + { + a = Math.sin(x); + result = a; + } + textField.setText(""+ result ); + } + +/**隐藏对话框**/ + public void windowClosing( WindowEvent e ){ + if( e.getSource() == dialog ) + dialog.setVisible( false ); + else + System.exit(0); + } + + public void windowOpened( WindowEvent e ) {} + public void windowActivated( WindowEvent e ) {} + public void windowDeactivated( WindowEvent e ) {} + public void windowClosed( WindowEvent e ) {} + public void windowIconified( WindowEvent e ) {} + public void windowDeiconified( WindowEvent e ) {} +public static void main( String args[] ) + {JSQ calculator = new JSQ();} +} + +/**单击窗口关闭按钮时触发并执行实现窗口监听器接口中的方法**/ +class WinClose implements WindowListener{ + public void windowClosing( WindowEvent e ){ + System.exit( 0 ); + } + public void windowOpened( WindowEvent e ){ } + public void windowActivated( WindowEvent e ){} + public void windowDeactivated( WindowEvent e){ } + public void windowClosed( WindowEvent e ){ } + public void windowIconified( WindowEvent e ){ } + public void windowDeiconified( WindowEvent e ){ } +} \ No newline at end of file diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java index fc09c195f566d0e707f5e8a2f05fd168069c73a7..1e7ce9c0df531dc06982ab82d72962649a757512 100644 --- a/src/java2020spring/Test.java +++ b/src/java2020spring/Test.java @@ -3,7 +3,7 @@ package java2020spring; public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + System.out.println("Hello git!"); } diff --git a/src/java2020spring/ZHQ.java b/src/java2020spring/ZHQ.java new file mode 100644 index 0000000000000000000000000000000000000000..9dff233cd1fee5f88052ab5b915937e0317d88a2 --- /dev/null +++ b/src/java2020spring/ZHQ.java @@ -0,0 +1,186 @@ +package java2020spring; + +import javax.swing.*; +import javax.swing.event.*; +import java.awt.*; +import java.awt.event.*; + +public class ZHQ extends JFrame { + public static void main(String[] args){ + EventQueue.invokeLater(new Runnable(){public void run(){}}); +} +/**设置窗口大小**/ + private final int WIDTH = 400, HEIGHT = 300; + +/**设置窗口布局**/ + private JLabel InputUnit=new JLabel("输入单位"); + private JComboBox whichIn=new JComboBox(new String[]{"",""}); + private JLabel InputNum=new JLabel("输入数值"); + private JTextField Default=new JTextField("1"); + private JLabel OutputUnit=new JLabel("输出单位"); + private JLabel showResult=new JLabel("显示结果"); + private JLabel txtResult=new JLabel(" "); //显示结果处初始为空 + private JComboBox whichOut=new JComboBox(new String[]{"",""}); + private JButton goTrans = new JButton("转换"); + private JButton goClear = new JButton("清空"); + private JButton goreturn=new JButton("返回"); + private JRadioButton rdLeng = new JRadioButton("长度转换"); + private JRadioButton rdWeig = new JRadioButton("质量转换"); + private String [] lengthUnit={"米", "分米", "厘米", "毫米"}; + private String [] weighthUnit={"吨", "千克", "克", "斤"}; + +/**指定窗口**/ + public ZHQ(){ + super("单位转换器 "); + this.setSize(WIDTH, HEIGHT); + this.setLocation( 300,200 ); + this.setResizable(true); //允许窗口调整大小 + this.setLayout(null); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + ButtonGroup group = new ButtonGroup(); + group.add(rdLeng); + group.add(rdWeig); + this.getContentPane().add(rdLeng); //读取输入的长度 + this.getContentPane().add(rdWeig); //读取输入的质量 + this.getContentPane().add(goTrans); //'转换' + this.getContentPane().add(goClear); //'清空' + this.getContentPane().add(goreturn);//'返回' + this.getContentPane().add(InputNum);//'输入的数字' + this.getContentPane().add(Default); + this.getContentPane().add(InputUnit);//用户选择的输入单位 + this.getContentPane().add(whichIn); //用户输入的数字 + this.getContentPane().add(showResult);//显示结果单位 + this.getContentPane().add(txtResult); //结果框内出现的结果 + this.getContentPane().add(OutputUnit);//用户选择的输出单位 + this.getContentPane().add(whichOut); //显示结果数字 + +/**字体**/ + Default.setFont(new Font("宋体",Font.BOLD,20)); + InputUnit.setFont(new Font("宋体",Font.BOLD,15)); + InputNum.setFont(new Font("宋体",Font.BOLD,15)); + OutputUnit.setFont(new Font("宋体",Font.BOLD,15)); + showResult.setFont(new Font("宋体",Font.BOLD,15)); + goTrans.setFont(new Font("宋体",Font.BOLD,15)); + goClear.setFont(new Font("宋体",Font.BOLD,15)); + +/**按钮颜色**/ + goreturn.setForeground(new Color(30,60,90)); + goClear.setForeground(new Color(30,60,90)); + goTrans.setForeground(new Color(30,60,90)); + whichIn.setForeground(new Color(30,60,90)); + whichOut.setForeground(new Color(30,60,90)); + + this.setVisible(true); + this.doLayout(); +/**设置监听器**/ + goTrans.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + doConvert();}}); //'转换'键的 + goClear.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + Default.setText("0"); //当用户无操作时显示的是0 + txtResult.setText("0");}}); //'清除'键的 + goreturn.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + JSQ calculator = new JSQ(); + boolean flase = false; + setVisible(flase);}}); ////'返回'键的 + rdLeng.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + whichIn.setModel(new DefaultComboBoxModel(lengthUnit)); + whichOut.setModel(new DefaultComboBoxModel(lengthUnit)); + } + }); + rdWeig.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + whichIn.setModel(new DefaultComboBoxModel(weighthUnit)); + whichOut.setModel(new DefaultComboBoxModel(weighthUnit)); + } + }); + rdLeng.setSelected(true); + whichIn.setModel(new DefaultComboBoxModel(lengthUnit)); + whichOut.setModel(new DefaultComboBoxModel(weighthUnit)); + } + final int offX=100; + public void doLayout(){ + super.doLayout(); + +/**各框的位置大小**/ + rdLeng.setBounds(offX, 15, 100, 20); + rdWeig.setBounds(rdLeng.getX()+rdLeng.getWidth()+5, 15,100, 20); + InputUnit.setBounds(offX, rdLeng.getY()+rdLeng.getHeight()+5, 80, 20); + whichIn.setBounds(InputUnit.getX()+InputUnit.getWidth()+5, InputUnit.getY(), 80, 20); + InputNum.setBounds(offX, InputUnit.getY()+InputUnit.getHeight()+5, 80, 20); + Default.setBounds(InputNum.getX()+InputNum.getWidth()+5, Default.getY()+17, 80, 20); + OutputUnit.setBounds(offX, InputNum.getY()+InputNum.getHeight()+30, 80, 20); + whichOut.setBounds(OutputUnit.getX()+OutputUnit.getWidth()+5, OutputUnit.getY(), 80, 20); + showResult.setBounds(offX, whichOut.getY()+whichOut.getHeight()+5, 80, 20); + txtResult.setBounds(showResult.getX()+showResult.getWidth()+5, showResult.getY(), 100, 20); + +/**根据大框的大小确定'转换''返回'及'清空'按钮的位置**/ + int w=getWidth (); + int x=(w-70*2-5)/2; + goTrans.setBounds(x, showResult.getY()+showResult.getHeight()+30, 70, 30); + goClear.setBounds(goTrans.getX()+goTrans.getWidth()+3, goTrans.getY(), 70, 30); + goreturn.setBounds(20,10, 70, 30); + } + + public void doConvert(){ + double v=0; + try{ + v= Double.parseDouble(Default.getText()); + } + catch(Exception ex){ + Default.setText("???"); + return; + } + +/**长度单位转换功能(以"米"为标准单位)**/ + if(rdLeng.isSelected()){ + switch(whichIn.getSelectedIndex()){ + case 0: break; + case 1: v=v/10; break; + case 2: v=v/100; break; + case 3: v=v/1000;break; + default: return; + } + switch(whichOut.getSelectedIndex()){ + case 0: break; + case 1: v=v*10; break; + case 2: v=v*100; break; + case 3: v=v*1000; break; + default:return; + } + if(v<0.01){ + txtResult.setText(String.format("%2.3f", v)); + } + else{ + txtResult.setText(String.format("%2.3f", v)); + } + } + +/**质量单位转换功能(以"千克"为标准单位)**/ + else{ + switch(whichIn.getSelectedIndex()){ + case 0:v=v*1000;break; + case 1: break; + case 2:v=v/1000;break; + case 3:v=v/2; break; + default: return; + } + switch(whichOut.getSelectedIndex()){ + case 0:v=v/1000;break; + case 1: break; + case 2:v=v*1000;break; + case 3:v=v*2; break; + default: return; + } + if(v<0.01){ + txtResult.setText(String.format("%2.3f",v)); + } + else{ + txtResult.setText(String.format("%2.3f",v)); + } + } + } + } \ No newline at end of file