Java实现:类windows平台下简易数制转换源代码
2009-09-16 00:00:00 来源:WEB开发网Java swing 计算器代码
package com.youngmaster;
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.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class NumberSystemConversion extends JFrame implements ActionListener, ItemListener {
private JButton jButton[];
private JButton clear, converJButton;
private JTextField jTextField;
private JPanel jPanel1, jPanel2, jPanel3;
int number = 1;
private String string[] = { "7", "8", "9", "/", "6", "5", "4", "*", "3",
"2", "1", "-", "0", ".", "=", "+" };
private String s = "", num1 = "", num2 = "", num3 = "";
private static int tem = 0;
JComboBox jComboBox;
private boolean flag = true;
public NumberSystemConversion() {
super("简易计算器");
Container c = getContentPane();
c.setLayout(new FlowLayout());
jPanel1 = new JPanel();
jTextField = new JTextField(13);
jPanel1.add(jTextField);
clear = new JButton("C");
jPanel1.add(clear);
c.add(jPanel1);
jPanel3 = new JPanel();
jComboBox = new JComboBox();
jComboBox.jComboBoxddItem("10--2");
jComboBox.addItem("10--8");
jComboBox.addItem("10--16");
jComboBox.addItem("2 --8");
jComboBox.addItem("2 --10");
jComboBox.addItem("2 --16");
jComboBox.addItem("8 --2");
jComboBox.addItem("8 --10");
jComboBox.addItem("8 --16");
jComboBox.addItem("16--2");
jComboBox.addItem("16--8");
jComboBox.addItem("16--10");
jPanel3.add(jComboBox);
converJButton = new JButton("转换");
jPanel3.add(converJButton);
c.add(jPanel3);
jPanel2 = new JPanel();
jPanel2.setLayout(new GridLayout(4, 4, 10, 10));
jButton = new JButton[string.length];
for (int i = 0; i < string.length; i++) {
jButton[i] = new JButton(string[i]);
jButton[i].addActionListener(this);
jPanel2.add(jButton[i]);
}
c.add(jPanel2);
clear.addActionListener(this);
converJButton.addActionListener(this);
jComboBox.addItemListener(this);
setSize(200, 250);
setVisible(true);
try {
JMenuItem itemNative=new JMenuItem("系统平台风格");
UIManager.setLoojTextFieldAndFeel(UIManager.getSystemLookAndFeelClassName()); // 设置界面样式
} catch (Exception e) {
e.printStackTrace();
}
setSize(220, 270);
setVisible(true);
}
public static void main(String[] args) {
NumberSystemConversion s = new NumberSystemConversion();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
if (jComboBox.getSelectedItem().equals("10--2"))
number = 1;
if (jComboBox.getSelectedItem().equals("10--8"))
number = 2;
if (jComboBox.getSelectedItem().equals("10--16"))
number = 3;
if (jComboBox.getSelectedItem().equals("2 --8"))
number = 4;
if (jComboBox.getSelectedItem().equals("2 --10"))
number = 5;
if (jComboBox.getSelectedItem().equals("2 --16"))
number = 6;
if (jComboBox.getSelectedItem().equals("8 --2"))
number = 7;
if (jComboBox.getSelectedItem().equals("8 --10"))
number = 8;
if (jComboBox.getSelectedItem().equals("8 --16"))
number = 9;
if (jComboBox.getSelectedItem().equals("16--2"))
number = 10;
if (jComboBox.getSelectedItem().equals("16--8"))
number = 11;
if (jComboBox.getSelectedItem().equals("16--10"))
number = 12;
}
}
public void actionPerformed(ActionEvent e) {
if (!(e.getSource() == jButton[3] || e.getSource() == jButton[7]
|| e.getSource() == jButton[11] || e.getSource() == jButton[13]
|| e.getSource() == jButton[14] || e.getSource() == jButton[15] || e
.getSource() == clear|| e.getSource() == converJButton)) {
if (e.getSource() == jButton[12]) {
s += e.getActionCommand();
char a = s.charAt(0);
if (a == '0') {
s = "0.";
jTextField.setText(s);
}
else{
jTextField.setText(s);
flag = true;
}
}
else {
s += e.getActionCommand();
jTextField.setText(s);
flag = true;
}
} else {
if (e.getSource() == converJButton) {
if (jTextField.getText().equals(""))
;
else
{
jinconverJButtoni();
tem = 0;
num1 = "";
s = "";
}
}
if (e.getSource() == clear) {
jTextField.setText("");
tem = 0;
num1 = "";
s = "";
}
if (e.getSource() == jButton[13]) {
String ss = s;
ss += e.getActionCommand();
char a = ss.charAt(0);
if (a == '.') {
s = "";
} else {
if (ss.matches("[0-9].")) {
s += e.getActionCommand();
jTextField.setText(s);
}
}
}
if (flag) {
flag = false;
if (e.getSource() == jButton[3]) {
if (jTextField.getText().equals(""))
;
else {
if (num1.equals("")) {
num1 = jTextField.getText();
jTextField.setText("/");
s = "";
tem = 3;
} else {
num2 = jTextField.getText();
count(tem, num1, num2);
tem = 3;
}
}
}
if (e.getSource() == jButton[7]) {
if (jTextField.getText().equals(""))
;
else {
if (num1.equals("")) {
num1 = jTextField.getText();
jTextField.setText("*");
s = "";
tem = 7;
} else {
num2 = jTextField.getText();
count(tem, num1, num2);
tem = 7;
}
}
}
if (e.getSource() == jButton[11]) {
if (jTextField.getText().equals(""))
;
else {
if (num1.equals("")) {
num1 = jTextField.getText();
jTextField.setText("-");
s = "";
tem = 11;
} else {
num2 = jTextField.getText();
count(tem, num1, num2);
tem = 11;
}
}
}
if (e.getSource() == jButton[15]) {
if (jTextField.getText().equals(""))
;
else {
if (num1.equals("")) {
num1 = jTextField.getText();
jTextField.setText("+");
s = "";
tem = 15;
} else {
num2 = jTextField.getText();
count(tem, num1, num2);
tem = 15;
}
}
}
if (e.getSource() == jButton[14]) {
if (jTextField.getText().equals("") || num1.equals(""))
flag = true;
else {
num2 = jTextField.getText();
count(tem, num1, num2);
}
}
}
}
}
public void count(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:
num3 = twoDigits.format((double) (a / b));
jTextField.setText(num3);
breajTextField;
case 7:
num3 = twoDigits.format((double) (a * b));
jTextField.setText(num3);
break;
case 11:
num3 = twoDigits.format((double) (a - b));
jTextField.setText(num3);
break;
case 15:
num3 = twoDigits.format((double) (a + b));
jTextField.setText(num3);
break;
}
s = "";
num1 = num3;
num2 = "";
}
public void jinconverJButtoni() {
if (number == 1)
jTextField.setText(Integer.toBinaryString(Integer.parseInt(jTextField.getText())));
else if (number == 2)
jTextField.setText(Integer.toOctalString(Integer.parseInt(jTextField.getText())));
else if (number == 3)
jTextField.setText(Integer.toHexString(Integer.parseInt(jTextField.getText())));
else if (number == 4) {
int a = j1(jTextField.getText(), "[0-1]+", 2);
jTextField.setText(Integer.toOctalString(a));
} else if (number == 5) {
j1(jTextField.getText(), "[0-1]+", 2);
} else if (number == 6) {
int a = j1(jTextField.getText(), "[0-1]+", 2);
jTextField.setText(Integer.toHexString(a));
} else if (number == 7) {
int a = j1(jTextField.getText(), "[0-7]+", 8);
jTextField.setText(Integer.toBinaryString(a));
} else if (number == 8) {
j1(jTextField.getText(), "[0-7]+", 8);
} else if (number == 9) {
int a = j1(jTextField.getText(), "[0-7]+", 8);
jTextField.setText(Integer.toHexString(a));
} else if (number == 10) {
int a = j2();
jTextField.setText(Integer.toBinaryString(a));
} else if (number == 11) {
int a = j2();
jTextField.setText(Integer.toOctalString(a));
} else if (number == 12) {
j2();
}
}
public int j1(String converJButtonuanhuan, String a1, int a3) {
if (converJButtonuanhuan.matches(a1)) {
int number3, number1 = 0, number2 = 1;
String s = converJButtonuanhuan;
StringBuffer ss = new StringBuffer(s);
ss.reverse();
for (int i = 0; i < ss.length(); i++) {
String jj = ss.charAt(i) + "";
number3 = Integer.parseInt(jj);
if (i == 0)
number1 += number3 * number2;
else
number1 += number3 * (number2 *= a3);
}
jTextField.setText(number1 + "");
return number1;
}
return 0;
}
public int j2() {
if (jTextField.getText().matches("[0-9,a,b,c,d,e,f]+")) {
int intNum1, intNum2 = 0, intNum3 = 1;
String s = jTextField.getText();
StringBuffer stringBuffer = new StringBuffer(s);
stringBuffer.reverse();
for (int i = 0; i < stringBuffer.length(); i++) {
String ptr = stringBuffer.charAt(i) + "";
System.out.println(ptr);
if (ptr.equals("a"))
ptr = "10";
if (ptr.equals("b"))
ptr = "11";
if (ptr.equals("c"))
ptr = "12";
if (ptr.equals("d"))
ptr = "13";
if (ptr.equals("e"))
ptr = "14";
if (ptr.equals("f"))
ptr = "15";
System.out.println(ptr);
intNum1 = Integer.parseInt(ptr);
if (i == 0)
intNum2 += intNum1 * intNum3;
else
intNum2 += intNum1 * (intNum3 *= 16);
}
jTextField.setText(intNum2 + "");
return intNum2;
}
return 0;
}
}
更多精彩
赞助商链接