小弟刚开始学java 昨天用JCreater写的一个小程序 虽然简单却让我欣喜若狂 迫不及待的拿出来共享 虽然会让群里的大虾们笑话 import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class jisuanqi extends JFrame{ private JButton JButton0; private JButton JButton1; private JButton JButton2; private JButton JButton3; private JButton JButton4; private JButton JButton5; private JButton JButton6; private JButton JButton7; private JButton JButton8; private JButton JButton9; private JButton JButtonjia; private JButton JButtonjian; private JButton JButtoncheng; private JButton JButtonchu; private JButton JButtondeng; private JButton JButtonqing; private JButton JButtontui; private JTextField JTextField1; static int fuhao; static long shu; public jisuanqi(){ createUserInterface(); } public void createUserInterface(){ Container contentPane = getContentPane(); contentPane.setLayout( null ); JButton1 =new JButton(); JButton1.setText("1"); JButton1.setBounds(0,60,60,60); contentPane.add(JButton1); JButton1.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton1ActionPerformed( event ); } }
); JButton2 =new JButton(); JButton2.setText("2"); JButton2.setBounds(60,60,60,60); contentPane.add(JButton2); JButton2.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton2ActionPerformed( event ); } }
); JButton3 =new JButton(); JButton3.setText("3"); JButton3.setBounds(120,60,60,60); contentPane.add(JButton3); JButton3.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton3ActionPerformed( event ); } }
); JButton4 =new JButton(); JButton4.setText("4"); JButton4.setBounds(0,120,60,60); contentPane.add(JButton4); JButton4.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton4ActionPerformed( event ); } }
); JButton5 =new JButton(); JButton5.setText("5"); JButton5.setBounds(60,120,60,60); contentPane.add(JButton5); JButton5.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton5ActionPerformed( event ); } }
); JButton6 =new JButton(); JButton6.setText("6"); JButton6.setBounds(120,120,60,60); contentPane.add(JButton6); JButton6.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton6ActionPerformed( event ); } }
); JButton7 =new JButton(); JButton7.setText("7"); JButton7.setBounds(0,180,60,60); contentPane.add(JButton7); JButton7.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton7ActionPerformed( event ); } }
); JButton8 =new JButton(); JButton8.setText("8"); JButton8.setBounds(60,180,60,60); contentPane.add(JButton8); JButton8.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton8ActionPerformed( event ); } }
); JButton9 =new JButton(); JButton9.setText("9"); JButton9.setBounds(120,180,60,60); contentPane.add(JButton9); JButton9.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton9ActionPerformed( event ); } }
); JButton0 =new JButton(); JButton0.setText("0"); JButton0.setBounds(60,240,60,60); contentPane.add(JButton0); JButton0.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton0ActionPerformed( event ); } }
); JButtonjia =new JButton(); JButtonjia.setText("+"); JButtonjia.setBounds(180,60,60,60); contentPane.add(JButtonjia); JButtonjia.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonjiaActionPerformed( event ); } }
); JButtonjian =new JButton(); JButtonjian.setText("-"); JButtonjian.setBounds(180,120,60,60); contentPane.add(JButtonjian); JButtonjian.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonjianActionPerformed( event ); } }
); JButtoncheng =new JButton(); JButtoncheng.setText("*"); JButtoncheng.setBounds(180,180,60,60); contentPane.add(JButtoncheng); JButtoncheng.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonchengActionPerformed( event ); } }
); JButtonchu =new JButton(); JButtonchu.setText("/"); JButtonchu.setBounds(180,240,60,60); contentPane.add(JButtonchu); JButtonchu.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonchuActionPerformed( event ); } }
); JButtondeng =new JButton(); JButtondeng.setText("="); JButtondeng.setBounds(120,240,60,60); contentPane.add(JButtondeng); JButtondeng.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtondengActionPerformed( event ); } }
); JButtonqing =new JButton(); JButtonqing.setText("清0"); JButtonqing.setBounds(0,240,60,60); contentPane.add(JButtonqing); JButtonqing.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonqingActionPerformed( event ); } }
); JButtontui =new JButton(); JButtontui.setText("退格"); JButtontui.setBounds(180,0,60,60); contentPane.add(JButtontui); JButtontui.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtontuiActionPerformed( event ); } }
); JTextField1 = new JTextField(); JTextField1.setText( "0" ); JTextField1.setBounds( 20, 20, 140, 20 ); JTextField1.setHorizontalAlignment( JTextField.RIGHT ); contentPane.add( JTextField1 ); setTitle("jisuanqi"); this.setSize(240,360); this.setVisible(true);
} private void JButton1ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+1; JTextField1.setText( String.valueOf(b) ); } private void JButton2ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+2; JTextField1.setText( String.valueOf(b) ); } private void JButton3ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+3; JTextField1.setText( String.valueOf(b) ); } private void JButton4ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+4; JTextField1.setText( String.valueOf(b) ); } private void JButton5ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+5; JTextField1.setText( String.valueOf(b) ); } private void JButton6ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+6; JTextField1.setText( String.valueOf(b) ); } private void JButton7ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+7; JTextField1.setText( String.valueOf(b) ); } private void JButton8ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+8; JTextField1.setText( String.valueOf(b) ); } private void JButton9ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+9; JTextField1.setText( String.valueOf(b) ); } private void JButton0ActionPerformed( ActionEvent event ){ long a=0,b=0; if( JTextField1.getText()==""||Integer.parseInt( JTextField1.getText() )==0) JTextField1.setText(String.valueOf(b)); else {a=Integer.parseInt( JTextField1.getText() );b=a*10;} JTextField1.setText( String.valueOf(b) ); } private void JButtonqingActionPerformed( ActionEvent event ){ JTextField1.setText( String.valueOf(0) ); } private void JButtontuiActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); if(a<=10) JTextField1.setText( String.valueOf(0) ); else{b=a%10;a=(a-b)/10;JTextField1.setText( String.valueOf(a) );} } private void JButtonjiaActionPerformed( ActionEvent event ){ fuhao=1; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtonjianActionPerformed( ActionEvent event ){ fuhao=2; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtonchengActionPerformed( ActionEvent event ){ fuhao=3; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtonchuActionPerformed( ActionEvent event ){ fuhao=4; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtondengActionPerformed( ActionEvent event ){ long a; long b=Integer.parseInt( JTextField1.getText() ); if(fuhao==1) {a=b+shu;JTextField1.setText( String.valueOf(a) );} else if(fuhao==2) {a=shu-b;JTextField1.setText( String.valueOf(a) );} else if(fuhao==3) {a=b*shu;JTextField1.setText( String.valueOf(a) );} else if(fuhao==4) {a=shu/b;JTextField1.setText( String.valueOf(a) );} } public static void main( String[] args ){ jisuanqi application = new jisuanqi(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }