适合新手参考参考,实现的功能就是:输入第一个数,输入第二个数,对这两个数进行加减乘除。
代码中均有注释,很好理解,如果发现有问题可以康康是哪里出的问题。
十分简易,十分捞。如果评论区有大佬指点指点改进一下,后续会实现平方,开方,取余(其实这些只要会一个就都可以实现,但是我不会啊T-T)
页面:
代码如下:
package start; import util.Constant; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Caculator extends Jframe implements ActionListener{ private JPanel jp_north = new JPanel(); //设置窗口组件 private JTextField input_text = new JTextField(); //输入 private JButton c_button = new JButton("C"); //归零~ private JPanel jp_center = new JPanel(); public Caculator() throws HeadlessException { //抛出异常 this.init(); this.addNorthComponent(); this.addCenterButton(); } //初始化 public void init(){ this.setTitle(Constant.Title); this.setSize(Constant.Init_W,Constant.Init_H); //设置计算器界面大小 this.setLayout(new BorderLayout()); //用指定的组件之间的水平间距构造一个边界布局 this.setResizable(false); this.setLocation(Constant.x,Constant.y); //用来初试化程序让计算器界面显示在正中央 this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); //关闭敞口的时候退出程序 } //北面北面 大专牌面 public void addNorthComponent(){ this.input_text.setPreferredSize(new Dimension(430,50));//输入值的长宽大小 就是1+1的位置 jp_north.add(input_text); jp_north.add(c_button); this.c_button.setPreferredSize(new Dimension(50,50)); c_button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { input_text.setText(""); } }); this.c_button.setForeground(Color.PINK);//把归零设置成猛男粉色 this.add(jp_north,BorderLayout.NORTH); } //中间按钮区 public void addCenterButton(){ String botton_text = "123+456-789*0.=/bsⅡⅢ";//添加按钮上的文字 String regex = "[\+\-*/.=]"; String numb = "[0123456789]"; this.jp_center.setLayout(new GridLayout(4,4)); for(int i = 0;i < 16;i++){ String temp = botton_text.substring(i,i+1);//每次添加一个按钮上的文字 JButton button = new JButton(); button.setText(temp); if(temp.matches(numb)){ button.setFont(new Font("Times New Roman",Font.BOLD,30)); button.setForeground(Color.BLACK); //给数字加♂大加♂粗 } if(temp.matches(regex)){ button.setFont(new Font("粗体",Font.BOLD,30)); button.setForeground(Color.RED); //给加减乘除定义字体大小和颜色 } button.addActionListener(this); jp_center.add(button); } this.add(jp_center,BorderLayout.CENTER); } public static void main(String[] args) { Caculator caculator = new Caculator(); caculator.setVisible(true); } private String firstInput = null; //初试输入为空 private String operator = null; @Override public void actionPerformed(ActionEvent e) { String clickStr = e.getActionCommand(); //捕获点击数字按钮 if("0123456789.".indexOf(clickStr) != -1 ){ this.input_text.setText(input_text.getText()+ clickStr); //将输入数字(不仅仅是个位数)显示到北面输入框内 this.input_text.setHorizontalAlignment(JTextField.RIGHT); //将输入数字居右显示 // JOptionPane.showMessageDialog(this,clickStr); //点击数字按钮.按钮出现弹框 }else if(clickStr.matches("[\+\-*/]{1}")){ operator = clickStr; //记录+-*/的哪一个 firstInput = this.input_text.getText(); //记录第一次输入值 this.input_text.setText(""); //当点击+-*/时候就会清空输入的值 }else if(clickStr.equals("=")){ Double a = Double.valueOf(firstInput); Double b = Double.valueOf(this.input_text.getText()); Double result = null; // if(operator == "s"){ // result = Math.pow(a,2); // } switch (operator){ case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "/": if(b != 0){ result = a / b; } break; } this.input_text.setText(result.toString()); } } } //2021.11.24
结果显示:
第一个数输入完之后点击+-*/任意一个之后会清空屏幕,此时输入第二个数
例如1+1=?
答案是:
哈哈
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)