标签:package 技术 判断 get add new cti out code
源代码:
package jisuanqi;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class jisuanqi extends JFrame implements ActionListener{
private JButton yunsuan;
private JTextField text;
public jisuanqi(){
super("运算器");
this.setSize(400,100);
this.setLocation(300,240);
this.setLayout(new FlowLayout());
this.add(new JTextField("10",8));//区域为8
this.add(new Label("+"));
this.add(new JTextField("20",8));
this.yunsuan=new JButton("=");
this.add(this.yunsuan);
this.add(new JTextField(10));
this.text=new JTextField(8);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
}
public void actionPerformed(ActionEvent e) {//判断运算符来进行预算
text.setText(text.getText()+yunsuan.getText());
if(text.getText().contains("+")){//做加法运算
StringTokenizer st1 = new StringTokenizer(text.getText(),"+");
double d1,d2 = 0;
d1 = Double.parseDouble(st1.nextToken());
while(st1.hasMoreTokens()){
StringTokenizer st2 = new StringTokenizer(st1.nextToken(),"=");
d2 = Double.parseDouble(st2.nextToken());
}
text.setText(yunsuan.getText()+" "+(d1+d2));
}
}
public void Text(JButton button){
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String str = button.getText();
text.setText(text.getText()+str);
}
});
}
public static void main(String[] args) {
new jisuanqi();
}
}
结果界面为:
心得:只要掌握了那些方法的原理,就可以做出来
标签:package 技术 判断 get add new cti out code
原文地址:https://www.cnblogs.com/LHT-548/p/10965077.html