赞
踩
package work1; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class TestMain { JFrame jFrame;//成员变量,未设置访问类型,默认包内访问,先创建一个JF对象 String stringx;//保存第一个变量 int mark;//保存运算符号; public static void main(String[] args) { // TODO 自动生成的方法存根 TestMain testMain=new TestMain(); testMain.initView(); } public void initView() { jFrame=new JFrame("好好学习");//第一步,初始化 jFrame.setBounds(500, 500, 600, 600);//第二步,设置位置大小 jFrame.setLayout(null);//控制布局 null为不使用系统所给的任何一种布局 final JTextField jTextField=new JTextField(); jTextField.setBounds(50, 50,500, 50); jFrame.add(jTextField); JButton jButton0=new JButton("0");//********00000000000000000 jButton0.setBounds(50, 100, 50, 50); jFrame.add(jButton0); jButton0.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String string =jTextField.getText().trim();//去掉两端空格的代码trim if(!string.equals("0")) { string=string+"0"; jTextField.setText(string); } } }); JButton jButton1=new JButton("1");//**********111111111111111111111111 jButton1.setBounds(100, 100, 50, 50); jFrame.add(jButton1); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String string= jTextField.getText().trim(); if(string.equals("0")) { jTextField.setText("1"); } else { string=string+"1"; jTextField.setText(string); } } }) ; JButton jButton2=new JButton("2"); jButton2.setBounds(150, 100, 50, 50); jFrame.add(jButton2); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String string= jTextField.getText().trim(); if(string.equals("0")) { jTextField.setText("1"); } else { string=string+"1"; jTextField.setText(string); } } }) ; JButton jButtonC=new JButton("C"); jButtonC.setBounds(50, 150, 50, 50); jButtonC.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 jTextField.setText("0"); } }); jFrame.add(jButtonC);//................................ JButton jButtonD=new JButton("."); jButtonD.setBounds(100,150, 50, 50); jButtonD.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String string=jTextField.getText(); if(!string.contains(".")) { string=string+"."; } } }); jFrame.add(jButtonD); JButton jButtonE=new JButton("=");//======================================= jButtonE.setBounds(200,150, 50, 50); jButtonE.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(mark==1) { int x=Integer.parseInt(stringx); int y=Integer.parseInt(jTextField.getText()); int ans=x+y; jTextField.setText(Integer.toString(ans)); } } }); jFrame.add(jButtonE); JButton jButton11=new JButton("+"); jButton11.setBounds(150, 150, 50, 50); jButton11.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 mark=1; stringx=jTextField.getText(); jTextField.setText("");//清除,输入第二个数 } }); jFrame.add(jButton11); jFrame.setVisible(true);//第三部,让界面显示出来,这句话一定在最后 //但凡是成对的元素,必须要先写出来再将光标移到中间写其他的代码 } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。