简单的计算器,包含UI

xiaoxiao2021-02-28  128

package test0704; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import net.miginfocom.swing.MigLayout; import java.awt.Color; import javax.swing.JButton; import java.awt.Font; import javax.swing.LayoutStyle.ComponentPlacement; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.ImageIcon; public class JiSuanQi extends JFrame { private JPanel contentPane; private JTextField textField; private double num=0.0; //截取运算符前面的数字 public static double addDoubleA(String a){ double num1=Double.parseDouble(a.substring(0, a.indexOf(' '))); return num1; }   //获取运算符到=之间的数值 public static double addDoubleB(String a){ double num2=Double.parseDouble(a.substring(a.lastIndexOf(' '),a.indexOf('='))); return num2; } //获取运算符 public static String addYunSuanFu(String a){ String mark=a.substring(a.indexOf(' '),a.lastIndexOf(' '));      return mark; }        //连续运算方法 public static void lianXu(String a){ if(a.indexOf('+')!=-1|a.indexOf('-')!=-1|a.indexOf('*')!=-1|a.indexOf('/')!=-1){} } /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JiSuanQi frame = new JiSuanQi(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public JiSuanQi() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 305, 431); contentPane = new JPanel(); contentPane.setBackground(Color.WHITE); contentPane.setForeground(Color.BLUE); contentPane.setBorder(new EmptyBorder(5, 0, 20, 7)); setContentPane(contentPane); JButton btnNewButton = new JButton("6"); btnNewButton.setFont(new Font("黑体", Font.PLAIN, 18)); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "6"); } }); JButton button = new JButton("5"); button.setFont(new Font("黑体", Font.PLAIN, 18)); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "5"); } }); JButton button_1 = new JButton("4"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "4"); } }); button_1.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_2 = new JButton("7"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "7"); } }); button_2.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_4 = new JButton("9"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "9"); } }); button_4.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_3 = new JButton("8"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "8"); } }); button_3.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_5 = new JButton("."); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "."); } }); button_5.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_7 = new JButton("0"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "0"); } }); button_7.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_8 = new JButton("3"); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "3"); } }); button_8.setFont(new Font("黑体", Font.PLAIN, 18)); JButton btnX = new JButton("2"); btnX.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(textField.getText() + "2"); } }); btnX.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_10 = new JButton("1"); button_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "1"); } }); button_10.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_11 = new JButton("+"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField.getText().indexOf(" +")!=-1| textField.getText().indexOf(" -")!=-1| textField.getText().indexOf(" *")!=-1| textField.getText().indexOf(" /")!=-1) { }else{textField.setText(textField.getText() + " + ");} } }); button_11.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_12 = new JButton("-"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + " - "); } }); button_12.setFont(new Font("黑体", Font.PLAIN, 18)); JButton btnX_1 = new JButton("X"); btnX_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + " * "); } }); btnX_1.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_14 = new JButton("/"); button_14.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + " / "); } }); button_14.setFont(new Font("黑体", Font.PLAIN, 18)); JButton btnBackspace = new JButton("Backspace"); btnBackspace.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(""); } }); btnBackspace.setFont(new Font("黑体", Font.PLAIN, 14)); JButton button_6 = new JButton("="); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "="); double num1=JiSuanQi.addDoubleA(textField.getText()); double num2=JiSuanQi.addDoubleB(textField.getText()); String mark =JiSuanQi.addYunSuanFu(textField.getText()); double result=0; if(mark.equals(" +")){ result=num1+num2; }else if(mark.equals(" -")){ result=num1-num2; }else if(mark.equals(" *")){ result=num1*num2; }else if(mark.equals(" /")){ result=num1/num2; }else{ JOptionPane.showMessageDialog(null, "输入错误!", "错误!", JOptionPane.ERROR_MESSAGE); } String strResult=String.valueOf(result); textField.setText(strResult ); } }); button_6.setFont(new Font("黑体", Font.PLAIN, 18)); JButton button_9 = new JButton("\u00B1"); button_9.setFont(new Font("黑体", Font.PLAIN, 13)); JButton btnMc = new JButton("MC"); btnMc.setFont(new Font("新宋体", Font.PLAIN, 12)); JButton btnMr = new JButton("MR"); btnMr.setFont(new Font("黑体", Font.PLAIN, 12)); JButton btnMs = new JButton("MS"); btnMs.setFont(new Font("黑体", Font.PLAIN, 12)); JButton btnM = new JButton("M+"); btnM.setFont(new Font("黑体", Font.PLAIN, 12)); JButton btnM_1 = new JButton("M-"); btnM_1.setFont(new Font("黑体", Font.PLAIN, 12)); textField = new JTextField(); textField.setEditable(false); textField.setColumns(10); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup() .addContainerGap() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(textField, GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnMs, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnX_1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(button_2, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_3, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_4, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnMr, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_12, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addGap(10) .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnMc, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_11, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(button_10, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnX, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_8, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnM, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_14, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(button_5, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnM_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_9, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(button_6, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false) .addComponent(btnBackspace, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(button_7, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))))) .addContainerGap()) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addComponent(textField, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(button_11, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button_10, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button_8, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(btnX, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)) .addComponent(btnMc, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(btnMr, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(button_12, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(button_4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(btnX_1, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button_3, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button_2, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)) .addComponent(btnMs, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(button_7, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(button_5, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(button_14, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(btnM, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(button_9, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(btnM_1, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)) .addComponent(button_6, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) .addComponent(btnBackspace, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))) ); contentPane.setLayout(gl_contentPane); } }
转载请注明原文地址: https://www.6miu.com/read-38472.html

最新回复(0)