概述
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
无聊做个java加减乘除计算器,int型,没有小数,,呵呵,,真特么无聊,,
package swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends JFrame { //这里误用void,返回空值
JTextField jTextField;
String num1="";
String num2="";
String flag="";
public Test() {
super();
this.setTitle("Calculator"); //窗体标题显示
this.setSize(310, 350);
this.getContentPane().setLayout(null);
this.add(getjTextField());
for(int i = 0; i<10;i++){
this.add(getjButton(i+""));
}
this.add(getFlag("+"));
this.add(getFlag("-"));
this.add(getFlag("X"));
this.add(getFlag("/"));
this.add(getEqual());
this.add(getClear());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public JTextField getjTextField(){
if(jTextField == null){
JTextField jTextField = new JTextField();
jTextField.setBounds(50, 49, 200, 20);
this.jTextField = jTextField;
}
return jTextField;
}
public JButton getjButton(final String num){
JButton jButton = new JButton(num);
int i = Integer.parseInt(num);
jButton.setBounds(50+i%3*50,80+i/3*50, 50, 50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(flag.equals("")){
num1 = num1+num;
jTextField.setText(num1);
}else{
num2= num2 + num;
jTextField.setText(num1+" "+flag +" "+num2);
}
}
});
return jButton;
}
public JButton getFlag(final String flaginput){
JButton jButton = new JButton(flaginput);
if(flaginput.equals("+")){
jButton.setBounds(200,80, 50, 50);
}else if(flaginput.equals("-")){
jButton.setBounds(200,130, 50, 50);
}else if(flaginput.equals("X")){
jButton.setBounds(200,180, 50, 50);
}else if(flaginput.equals("/")){
jButton.setBounds(200,230, 50, 50);
}
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = flaginput;
jTextField.setText(num1 + " " + flag);
}
});
return jButton;
}
public JButton getEqual(){
JButton jButton = new JButton("=");
jButton.setBounds(150,230, 50, 50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!flag.equals("")&&!"".equals(num2)&&!"".equals(num1)){
Integer n1 = Integer.parseInt(num1);
Integer n2 = Integer.parseInt(num2);
if("+".equals(flag)){
num1=n1+n2+"";
jTextField.setText((n1+n2)+"");
}else if("-".equals(flag)){
num1=n1-n2+"";
jTextField.setText((n1-n2)+"");
}else if("X".equals(flag)){
num1=n1*n2+"";
jTextField.setText((n1*n2)+"");
}else if("/".equals(flag)){
num1=n1/n2+"";
jTextField.setText((n1/n2)+"");
}
num2="";
flag="";
}
}
});
return jButton;
}
public JButton getClear(){
JButton jButton = new JButton("C");
jButton.setBounds(100,230,50,50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1="";
num2="";
flag="";
jTextField.setText("");
}
});
return jButton;
}
public static void main(String[] args) throws HeadlessException {
Test kk = new Test() ;
kk.setVisible(true); //原来的show()显示已过时
}
}
最后
以上就是顺心芒果为你收集整理的java简单计算_java简单计算器的全部内容,希望文章能够帮你解决java简单计算_java简单计算器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复