//package 娱乐; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.text.DecimalFormat; import javax.swing.*; public class 计算器 extends JFrame implements ActionListener, ItemListener { private JButton an[]; private JButton qingkong, zh; private JTextField k; private JPanel rq1, rq2, rq3; int zz = 1; private String string[] = { "7", "8", "9", "/", "6", "5", "4", "*", "3", "2", "1", "-", "0", ".", "=", "+" }; private String s = "", shu1 = "", shu2 = "", shu3 = ""; private static int fo = 0; JComboBox a; private boolean ff = true; public 计算_复杂() { super("进制计算器"); Container c = getContentPane(); c.setLayout(new FlowLayout()); rq1 = new JPanel(); k = new JTextField(13); rq1.add(k); qingkong = new JButton("C"); rq1.add(qingkong); c.add(rq1); rq3 = new JPanel(); a = new JComboBox(); a.addItem("10进制转换为2进制"); a.addItem("10进制转换为8进制"); a.addItem("10进制转换为16进制"); a.addItem("2进制转换为8进制"); a.addItem("2进制转换为10进制"); a.addItem("2进制转换为16进制"); a.addItem("8进制转换为2进制"); a.addItem("8进制转换为10进制"); a.addItem("8进制转换为16进制"); a.addItem("16进制转换为2进制"); a.addItem("16进制转换为8进制"); a.addItem("16进制转换为10进制"); rq3.add(a); zh = new JButton("转"); rq3.add(zh); c.add(rq3); rq2 = new JPanel(); rq2.setLayout(new GridLayout(4, 4, 10, 10)); an = new JButton[string.length]; for (int i = 0; i < string.length; i++) { an[i] = new JButton(string[i]); an[i].addActionListener(this); rq2.add(an[i]); } c.add(rq2); qingkong.addActionListener(this); zh.addActionListener(this); a.addItemListener(this); setSize(200, 250); setVisible(true); try { JMenuItem itemNative=new JMenuItem("系统平台风格"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // 设置界面样式 } catch (Exception e) { e.printStackTrace(); } setBounds(300,300,300,300); setSize(220, 270); setVisible(true); } public static void main(String[] args) { 计算_复杂 s = new 计算_复杂(); s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { if (a.getSelectedItem().equals("10进制转换为2进制")) zz = 1; if (a.getSelectedItem().equals("10进制转换为8进制")) zz = 2; if (a.getSelectedItem().equals("10进制转换为16进制")) zz = 3; if (a.getSelectedItem().equals("2进制转换为8进制")) zz = 4; if (a.getSelectedItem().equals("2进制转换为10进制")) zz = 5; if (a.getSelectedItem().equals("2进制转换为16进制")) zz = 6; if (a.getSelectedItem().equals("8进制转换为2进制")) zz = 7; if (a.getSelectedItem().equals("8进制转换为10进制")) zz = 8; if (a.getSelectedItem().equals("8进制转换为16进制")) zz = 9; if (a.getSelectedItem().equals("16进制转换为2进制")) zz = 10; if (a.getSelectedItem().equals("16进制转换为8进制")) zz = 11; if (a.getSelectedItem().equals("16进制转换为10进制")) zz = 12; } } public void actionPerformed(ActionEvent e) { if (!(e.getSource() == an[3] || e.getSource() == an[7] || e.getSource() == an[11] || e.getSource() == an[13] || e.getSource() == an[14] || e.getSource() == an[15] || e .getSource() == qingkong|| e.getSource() == zh)) { if (e.getSource() == an[12]) { s += e.getActionCommand(); char a = s.charAt(0); if (a == '0') { s = "0."; k.setText(s); } else{ k.setText(s); ff = true; } } else { s += e.getActionCommand(); k.setText(s); ff = true; } } else { if (e.getSource() == zh) { if (k.getText().equals("")) ; else { jinzhi(); fo = 0; shu1 = ""; s = ""; } } if (e.getSource() == qingkong) { k.setText(""); fo = 0; shu1 = ""; s = ""; } if (e.getSource() == an[13]) { String ss = s; ss += e.getActionCommand(); char a = ss.charAt(0); if (a == '.') { s = ""; } else { if (ss.matches("[0-9].")) { s += e.getActionCommand(); k.setText(s); } } } if (ff) { ff = false; if (e.getSource() == an[3]) { if (k.getText().equals("")) ; else { if (shu1.equals("")) { shu1 = k.getText(); k.setText("/"); s = ""; fo = 3; } else { shu2 = k.getText(); jishu(fo, shu1, shu2); fo = 3; } } } if (e.getSource() == an[7]) { if (k.getText().equals("")) ; else { if (shu1.equals("")) { shu1 = k.getText(); k.setText("*"); s = ""; fo = 7; } else { shu2 = k.getText(); jishu(fo, shu1, shu2); fo = 7; } } } if (e.getSource() == an[11]) { if (k.getText().equals("")) ; else { if (shu1.equals("")) { shu1 = k.getText(); k.setText("-"); s = ""; fo = 11; } else { shu2 = k.getText(); jishu(fo, shu1, shu2); fo = 11; } } } if (e.getSource() == an[15]) { if (k.getText().equals("")) ; else { if (shu1.equals("")) { shu1 = k.getText(); k.setText("+"); s = ""; fo = 15; } else { shu2 = k.getText(); jishu(fo, shu1, shu2); fo = 15; } } } if (e.getSource() == an[14]) { if (k.getText().equals("") || shu1.equals("")) ff = true; else { shu2 = k.getText(); jishu(fo, shu1, shu2); } } } } } public void jishu(int fj, String s1, String s2) { DecimalFormat twoDigits = new DecimalFormat("0.00"); double a, b; a = Double.parseDouble(s1); b = Double.parseDouble(s2); switch (fj) { case 3: shu3 = twoDigits.format((double) (a / b)); k.setText(shu3); break; case 7: shu3 = twoDigits.format((double) (a * b)); k.setText(shu3); break; case 11: shu3 = twoDigits.format((double) (a - b)); k.setText(shu3); break; case 15: shu3 = twoDigits.format((double) (a + b)); k.setText(shu3); break; } s = ""; shu1 = shu3; shu2 = ""; } public void jinzhi() { try{ if (zz == 1) k.setText(Integer.toBinaryString(Integer.parseInt(k.getText()))); else if (zz == 2) k.setText(Integer.toOctalString(Integer.parseInt(k.getText()))); else if (zz == 3) k.setText(Integer.toHexString(Integer.parseInt(k.getText()))); else if (zz == 4) { int a = j1(k.getText(), "[0-1]+", 2); k.setText(Integer.toOctalString(a)); } else if (zz == 5) { j1(k.getText(), "[0-1]+", 2); } else if (zz == 6) { int a = j1(k.getText(), "[0-1]+", 2); k.setText(Integer.toHexString(a)); } else if (zz == 7) { int a = j1(k.getText(), "[0-7]+", 8); k.setText(Integer.toBinaryString(a)); } else if (zz == 8) { j1(k.getText(), "[0-7]+", 8); } else if (zz == 9) { int a = j1(k.getText(), "[0-7]+", 8); k.setText(Integer.toHexString(a)); } else if (zz == 10) { int a = j2(); k.setText(Integer.toBinaryString(a)); } else if (zz == 11) { int a = j2(); k.setText(Integer.toOctalString(a)); } else if (zz == 12) { j2(); } } catch(Exception e) { JOptionPane.showMessageDialog(null, "检查输入的参数"); } } public int j1(String zhuanhuan, String a1, int a3) { if (zhuanhuan.matches(a1)) { int bb, gg = 0, cc = 1; String s = zhuanhuan; StringBuffer ss = new StringBuffer(s); ss.reverse(); for (int i = 0; i < ss.length(); i++) { String jj = ss.charAt(i) + ""; bb = Integer.parseInt(jj); if (i == 0) gg += bb * cc; else gg += bb * (cc *= a3); } k.setText(gg + ""); return gg; } return 0; } public int j2() { if (k.getText().matches("[0-9,a,b,c,d,e,f]+")) { int bb, gg = 0, cc = 1; String s = k.getText(); StringBuffer ss = new StringBuffer(s); ss.reverse(); for (int i = 0; i < ss.length(); i++) { String jj = ss.charAt(i) + ""; System.out.println(jj); if (jj.equals("a")) jj = "10"; if (jj.equals("b")) jj = "11"; if (jj.equals("c")) jj = "12"; if (jj.equals("d")) jj = "13"; if (jj.equals("e")) jj = "14"; if (jj.equals("f")) jj = "15"; System.out.println(jj); bb = Integer.parseInt(jj); if (i == 0) gg += bb * cc; else gg += bb * (cc *= 16); } k.setText(gg + ""); return gg; } return 0; } }