怎么在java中使用swing组件实现一个计算器功能
怎么在java中使用swing组件实现一个计算器功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
山海关网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
具体如下:
package awtDemo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; @SuppressWarnings("serial") public class Calculator extends JFrame implements ActionListener { private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" }; private final String[] COMMAND = { "Backspace", "CE", "C" }; private JButton keys[] = new JButton[KEYS.length]; private JButton commands[] = new JButton[COMMAND.length]; private JTextField resultText = new JTextField("0"); private boolean firstDigit = true; private double resultNum = 0.0; private String operator = "="; private boolean operateValidFlag = true; public Calculator() { init(); this.setBackground(Color.LIGHT_GRAY); this.setTitle("www.jb51.net - 计算器"); this.setLocation(500, 300); this.setResizable(false); this.pack(); } private void init() { resultText.setHorizontalAlignment(JTextField.RIGHT); resultText.setEditable(false); resultText.setBackground(Color.white); JPanel calckeysPanel = new JPanel(); calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3)); for (int i = 0; i < KEYS.length; i++) { keys[i] = new JButton(KEYS[i]); calckeysPanel.add(keys[i]); keys[i].setForeground(Color.blue); } keys[3].setForeground(Color.red); keys[8].setForeground(Color.red); keys[13].setForeground(Color.red); keys[18].setForeground(Color.red); keys[19].setForeground(Color.red); JPanel commandsPanel = new JPanel(); commandsPanel.setLayout(new GridLayout(1, 3, 3, 3)); for (int i = 0; i < COMMAND.length; i++) { commands[i] = new JButton(COMMAND[i]); commandsPanel.add(commands[i]); commands[i].setForeground(Color.red); } JPanel panel1 = new JPanel(); panel1.setLayout(new BorderLayout(3, 3)); panel1.add("North", commandsPanel); panel1.add("West", calckeysPanel); JPanel top = new JPanel(); top.setLayout(new BorderLayout()); top.add("Center", resultText); getContentPane().setLayout(new BorderLayout(3, 5)); getContentPane().add("North", top); getContentPane().add("Center", panel1); for (int i = 0; i < KEYS.length; i++) { keys[i].addActionListener(this); } for (int i = 0; i < COMMAND.length; i++) { commands[i].addActionListener(this); } } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); if (label.equals(COMMAND[0])) { handleBackspace(); } else if (label.equals(COMMAND[1])) { resultText.setText("0"); } else if (label.equals(COMMAND[2])) { handleC(); } else if ("0123456789.".indexOf(label) >= 0) { handleNumber(label); } else { handleOperator(label); } } private void handleBackspace() { String text = resultText.getText(); int i = text.length(); if (i > 0) { text = text.substring(0, i - 1); if (text.length() == 0) { resultText.setText("0"); firstDigit = true; operator = "="; } else { resultText.setText(text); } } } private void handleNumber(String key) { if (firstDigit) { resultText.setText(key); } else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) { resultText.setText(resultText.getText() + "."); } else if (!key.equals(".")) { resultText.setText(resultText.getText() + key); } firstDigit = false; } private void handleC() { resultText.setText("0"); firstDigit = true; operator = "="; } private void handleOperator(String key) { if (operator.equals("/")) { if (getNumberFromText() == 0.0) { operateValidFlag = false; resultText.setText("除数不能为零"); } else { resultNum /= getNumberFromText(); } } else if (operator.equals("1/x")) { if (resultNum == 0.0) { operateValidFlag = false; resultText.setText("零没有倒数"); } else { resultNum = 1 / resultNum; } } else if (operator.equals("+")) { resultNum += getNumberFromText(); } else if (operator.equals("-")) { resultNum -= getNumberFromText(); } else if (operator.equals("*")) { resultNum *= getNumberFromText(); } else if (operator.equals("sqrt")) { resultNum = Math.sqrt(resultNum); } else if (operator.equals("%")) { resultNum = resultNum / 100; } else if (operator.equals("+/-")) { resultNum = resultNum * (-1); } else if (operator.equals("=")) { resultNum = getNumberFromText(); } if (operateValidFlag) { long t1; double t2; t1 = (long) resultNum; t2 = resultNum - t1; if (t2 == 0) { resultText.setText(String.valueOf(t1)); } else { resultText.setText(String.valueOf(resultNum)); } } operator = key; firstDigit = true; operateValidFlag = true; } private double getNumberFromText() { double result = 0; try { result = Double.valueOf(resultText.getText()).doubleValue(); } catch (NumberFormatException e) { } return result; } public static void main(String args[]) { Calculator calculator1 = new Calculator(); calculator1.setVisible(true); calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
文章标题:怎么在java中使用swing组件实现一个计算器功能
本文URL:http://scgulin.cn/article/pceihs.html