import java.awt.*; import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class calculate extends JFrame { private JPanel contentPane; private JTextField textFieldForexpression; private JButton btnEnter = new JButton(); private JLabel lblInput = new JLabel("Please input the math expression"); public calculate() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 700, 500); contentPane = new JPanel(); contentPane.setBackground(new Color(238, 238, 209 )); contentPane.setBorder(BorderFactory.createEtchedBorder(Color.red, Color.darkGray)); contentPane.setLayout(null); setContentPane(contentPane); textFieldForexpression = new JTextField(); textFieldForexpression.setBounds(200, 200, 300, 30); textFieldForexpression.setVisible(true); contentPane.add(textFieldForexpression); btnEnter.setHorizontalAlignment(SwingConstants.CENTER); btnEnter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!hasInputError()) JOptionPane.showMessageDialog(null, "請輸入正確的算式"); else { if (getAnswer() == Float.MAX_VALUE) return; int i = (int) getAnswer(); if ( getAnswer() == (int) getAnswer()) JOptionPane.showMessageDialog(null, "答案是: " + i); else JOptionPane.showMessageDialog(null, "答案是: " + getAnswer()); } } }); btnEnter.setBounds(300, 300, 100, 40); btnEnter.setText("Enter"); btnEnter.setVisible(true); btnEnter.setForeground(Color.blue); btnEnter.setFont(new Font("Vrinda", Font.BOLD, 20)); contentPane.add(btnEnter); lblInput.setFont(new Font("Aparajita", Font.BOLD, 24)); lblInput.setBounds(150, 80, 400, 35); lblInput.setHorizontalAlignment(JLabel.CENTER); lblInput.setVisible(true); lblInput.setForeground(new Color(255, 71, 0)); contentPane.add(lblInput); } //end of calculate() public boolean isFloat(String str) { try { Float.parseFloat(str); return true; } catch (NumberFormatException e) { return false; } } //end of boolean isFloat(String str) public boolean hasInputError() { String s = textFieldForexpression.getText(); if (s.length() == 0) return false; else for (int i = 0; i < s.length() - 1; i ++ ) { if ((s.charAt(i) == '+' || s.charAt(i) == '-') && (s.charAt(i + 1) == '+' || s.charAt(i + 1) == '-')) return false; else if( (s.charAt(i) >= '0' && s.charAt(i) <= '9') || s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '.' ); else return false; } if ( !(s.charAt(s.length() - 1) >= '0' && s.charAt(s.length() - 1) <= '9')) return false; return true; } //end of boolean hasInputError() public float getAnswer() { int previousJiaJianIndex = 0; String s = textFieldForexpression.getText(); float result = 0; int i; for ( i = 1; i < s.length(); i ++) { if (s.charAt(i) == '+' || s.charAt(i) == '-') { if (isFloat(s.substring(previousJiaJianIndex, i))) result += Float.parseFloat(s.substring(previousJiaJianIndex, i)); else { JOptionPane.showMessageDialog(null, "請輸入正確的算式"); return Float.MAX_VALUE; } previousJiaJianIndex = i; } } if (previousJiaJianIndex == 0) { JOptionPane.showMessageDialog(null, "請輸入正確的算式"); return Float.MAX_VALUE; } else return result + Float.parseFloat(s.substring(previousJiaJianIndex, i)); } //end of float getAnswer() public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { calculate frame = new calculate(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } //end of static void main }
This blog contains posts regarding my practice in programming. The posts are about questions in Yahoo Knowledge or some program design ideas or implementation of algorithms.
Tuesday, February 28, 2012
具加減功能的簡易Java GUI計算機
Subscribe to:
Posts (Atom)