import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Stack;
public class ConversionOfNumberSystems extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField codeInputField;
private JTextField charOutputField;
private String s1="2进制",s2="2进制";
private String string[]={"进制选择","2进制","8进制","10进制","16进制"};
public int Locate(String s){
int count=0;
for(int i=0;i<5;i++) {
if(string[i].equals(s)) {
count=i-1;
break;
}
}
return count;
}
public void ChangeToOther(int sum,int k) {
StringBuilder builder = new StringBuilder();// 创建字符串构建器
int al,ah;
char Hexadecimal[]={'A','B','C','D','E','F'};
Stack stack=new Stack();
al=sum;
while(al!=0) {
ah=al%k;
stack.push(ah);
al=al/k;
}
while(!stack.empty()) {
int a=stack.pop();
if(a>=10) {
builder.append(Hexadecimal[a-10]);
} else {
builder.append(a);
}
}
charOutputField.setText(builder.toString());// 结果输出到文本框
}
public int GetTen(char[] charArray,String text,int len,int b) {
int sum=0;
int Decimal[]={10,11,12,13,14,15};
text = codeInputField.getText();
charArray = text.toCharArray();
len=charArray.length;
for(int i=0;i=0) {
sum+=Math.pow(b, len-i-1)*(Decimal[charArray[i]-'A']);
} else {
sum+=Math.pow(b, len-i-1)*(charArray[i]-'0');
}
}
return sum;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
try {//使用默认窗口形式
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ConversionOfNumberSystems frame = new ConversionOfNumberSystems();//新建窗口
frame.setVisible(true);//设置可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ConversionOfNumberSystems() {
setTitle("\u8FDB\u5236\u8F6C\u6362\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 171);
contentPane = new JPanel();//设置面板位置
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();//设置面板标题
panel.setBorder(new TitledBorder(null,
"\u591A\u79CD\u8FDB\u5236\u8F6C\u6362",
TitledBorder.LEADING, TitledBorder.TOP, null, new Color(59, 59,
59)));
contentPane.add(panel, BorderLayout.CENTER);//将从面板加入到主面板中
panel.setLayout(new GridLayout(0, 3, 5, 20));
JButton codeButton = new JButton("\u6E90\u8F6C\u6362\u6570");
panel.add(codeButton);
codeInputField = new JTextField();
codeInputField.setHorizontalAlignment(JTextField.CENTER);
panel.add(codeInputField);
codeInputField.setColumns(10);
JComboBox jc = new JComboBox(string);
jc.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
do_charButton_actionPerformed(e);
}
});
panel.add(jc);
JButton charButton = new JButton("\u76EE\u6807\u8FDB\u5236\u6570");
panel.add(charButton);
charOutputField = new JTextField();
charOutputField.setHorizontalAlignment(JTextField.CENTER);//居中设置
charOutputField.setEditable(false);
panel.add(charOutputField);
charOutputField.setColumns(10);
JComboBox jc1 = new JComboBox(string);
jc1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
do_codeButton_actionPerformed(e);
}
});
panel.add(jc1);
}
protected void do_charButton_actionPerformed(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
s1 = (String)e.getItem();
}
}
protected void do_codeButton_actionPerformed(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
s2 = (String)e.getItem();
int l1=Locate(s1);
int l2=Locate(s2);
int choice=10*l1+l2;
char[] charArray = null;
String text = null;
int len = 0;
int sum=0;
switch(choice) {
case 1:
sum=GetTen(charArray,text,len,2);
ChangeToOther(sum,8);
break;
case 2:
sum=GetTen(charArray,text,len,2);
charOutputField.setText(String.valueOf(sum));
break;
case 3:
sum=GetTen(charArray,text,len,2);
ChangeToOther(sum,16);
break;
case 10:
sum=GetTen(charArray,text,len,8);
ChangeToOther(sum,2);
break;
case 12:
sum=GetTen(charArray,text,len,8);
charOutputField.setText(String.valueOf(sum));
break;
case 13:
sum=GetTen(charArray,text,len,8);
ChangeToOther(sum,16);
break;
case 20:
text = codeInputField.getText();
sum=Integer.valueOf(text);
ChangeToOther(sum,2);
break;
case 21:
text = codeInputField.getText();
sum=Integer.valueOf(text);
ChangeToOther(sum,8);
break;
case 23:
text = codeInputField.getText();
sum=Integer.valueOf(text);
ChangeToOther(sum,16);
break;
case 30:
sum=GetTen(charArray,text,len,16);
ChangeToOther(sum,2);
break;
case 31:
sum=GetTen(charArray,text,len,16);
ChangeToOther(sum,8);
break;
case 32:
sum=GetTen(charArray,text,len,16);
ChangeToOther(sum,10);
break;
}
}
}
}