|
|
@ -2,11 +2,11 @@ import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
public class BaseConverter {
|
|
|
|
public class BaseConverter {
|
|
|
|
|
|
|
|
|
|
|
|
// 字符数组,用于表示大于9的数字
|
|
|
|
|
|
|
|
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -27,6 +27,7 @@ public class BaseConverter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 将十进制整数转换为目标进制的字符串
|
|
|
|
* 将十进制整数转换为目标进制的字符串
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -47,6 +48,7 @@ public class BaseConverter {
|
|
|
|
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
return result.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 将任意进制的数转换为另一个任意进制的数
|
|
|
|
* 将任意进制的数转换为另一个任意进制的数
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -73,8 +75,8 @@ public class BaseConverter {
|
|
|
|
public static class BaseConverterGUI extends JFrame {
|
|
|
|
public static class BaseConverterGUI extends JFrame {
|
|
|
|
|
|
|
|
|
|
|
|
private JTextField inputField;
|
|
|
|
private JTextField inputField;
|
|
|
|
private JTextField sourceBaseField;
|
|
|
|
private JComboBox<Integer> sourceBaseBox;
|
|
|
|
private JTextField targetBaseField;
|
|
|
|
private JComboBox<Integer> targetBaseBox;
|
|
|
|
private JButton convertButton;
|
|
|
|
private JButton convertButton;
|
|
|
|
private JLabel resultLabel;
|
|
|
|
private JLabel resultLabel;
|
|
|
|
|
|
|
|
|
|
|
@ -83,32 +85,76 @@ public class BaseConverter {
|
|
|
|
setSize(400, 200);
|
|
|
|
setSize(400, 200);
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
setLocationRelativeTo(null); // 居中显示
|
|
|
|
setLocationRelativeTo(null); // 居中显示
|
|
|
|
setLayout(new GridLayout(5, 2));
|
|
|
|
setLayout(new GridBagLayout());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
|
|
|
|
|
|
|
|
|
|
// 输入框
|
|
|
|
// 输入框
|
|
|
|
|
|
|
|
gbc.gridx = 0;
|
|
|
|
|
|
|
|
gbc.gridy = 0;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.EAST;
|
|
|
|
|
|
|
|
gbc.insets = new Insets(5, 5, 5, 5);
|
|
|
|
|
|
|
|
add(new JLabel("输入数据(例如 A1):"), gbc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
|
|
|
|
|
gbc.gridy = 0;
|
|
|
|
|
|
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
|
|
|
|
gbc.weightx = 1;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.WEST;
|
|
|
|
inputField = new JTextField();
|
|
|
|
inputField = new JTextField();
|
|
|
|
add(new JLabel("输入数据(例如 A1):"));
|
|
|
|
add(inputField, gbc);
|
|
|
|
add(inputField);
|
|
|
|
|
|
|
|
|
|
|
|
// 源进制下拉菜单
|
|
|
|
// 源进制输入框
|
|
|
|
gbc.gridx = 0;
|
|
|
|
sourceBaseField = new JTextField();
|
|
|
|
gbc.gridy = 1;
|
|
|
|
add(new JLabel("源进制数(例如 16):"));
|
|
|
|
gbc.fill = GridBagConstraints.NONE;
|
|
|
|
add(sourceBaseField);
|
|
|
|
gbc.weightx = 0;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.EAST;
|
|
|
|
// 目标进制输入框
|
|
|
|
add(new JLabel("源进制数(例如 16):"), gbc);
|
|
|
|
targetBaseField = new JTextField();
|
|
|
|
|
|
|
|
add(new JLabel("目标进制数(例如 2):"));
|
|
|
|
gbc.gridx = 1;
|
|
|
|
add(targetBaseField);
|
|
|
|
gbc.gridy = 1;
|
|
|
|
|
|
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
|
|
|
|
gbc.weightx = 1;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.WEST;
|
|
|
|
|
|
|
|
List<Integer> bases = Arrays.asList(2, 4, 8, 10, 16, 32);
|
|
|
|
|
|
|
|
sourceBaseBox = new JComboBox<>(bases.toArray(new Integer[0]));
|
|
|
|
|
|
|
|
add(sourceBaseBox, gbc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 目标进制下拉菜单
|
|
|
|
|
|
|
|
gbc.gridx = 0;
|
|
|
|
|
|
|
|
gbc.gridy = 2;
|
|
|
|
|
|
|
|
gbc.fill = GridBagConstraints.NONE;
|
|
|
|
|
|
|
|
gbc.weightx = 0;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.EAST;
|
|
|
|
|
|
|
|
add(new JLabel("目标进制数(例如 2):"), gbc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
|
|
|
|
|
gbc.gridy = 2;
|
|
|
|
|
|
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
|
|
|
|
gbc.weightx = 1;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.WEST;
|
|
|
|
|
|
|
|
targetBaseBox = new JComboBox<>(bases.toArray(new Integer[0]));
|
|
|
|
|
|
|
|
add(targetBaseBox, gbc);
|
|
|
|
|
|
|
|
|
|
|
|
// 转换按钮
|
|
|
|
// 转换按钮
|
|
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
|
|
|
|
|
gbc.gridy = 3;
|
|
|
|
|
|
|
|
gbc.fill = GridBagConstraints.NONE;
|
|
|
|
|
|
|
|
gbc.weightx = 0;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.CENTER;
|
|
|
|
convertButton = new JButton("转换");
|
|
|
|
convertButton = new JButton("转换");
|
|
|
|
add(new JLabel());
|
|
|
|
add(convertButton, gbc);
|
|
|
|
add(convertButton);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 结果标签
|
|
|
|
// 结果标签
|
|
|
|
|
|
|
|
gbc.gridx = 0;
|
|
|
|
|
|
|
|
gbc.gridy = 4;
|
|
|
|
|
|
|
|
gbc.gridwidth = 2;
|
|
|
|
|
|
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
|
|
|
|
gbc.weightx = 1;
|
|
|
|
|
|
|
|
gbc.anchor = GridBagConstraints.LINE_START;
|
|
|
|
resultLabel = new JLabel("转换结果:");
|
|
|
|
resultLabel = new JLabel("转换结果:");
|
|
|
|
add(new JLabel());
|
|
|
|
add(resultLabel, gbc);
|
|
|
|
add(resultLabel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加监听器到按钮
|
|
|
|
// 添加监听器到按钮
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
@ -116,8 +162,8 @@ public class BaseConverter {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String input = inputField.getText().toUpperCase();
|
|
|
|
String input = inputField.getText().toUpperCase();
|
|
|
|
int sourceBase = Integer.parseInt(sourceBaseField.getText());
|
|
|
|
int sourceBase = (Integer) sourceBaseBox.getSelectedItem();
|
|
|
|
int targetBase = Integer.parseInt(targetBaseField.getText());
|
|
|
|
int targetBase = (Integer) targetBaseBox.getSelectedItem();
|
|
|
|
String result = convertBase(input, sourceBase, targetBase);
|
|
|
|
String result = convertBase(input, sourceBase, targetBase);
|
|
|
|
resultLabel.setText("转换结果:" + result);
|
|
|
|
resultLabel.setText("转换结果:" + result);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
} catch (Exception ex) {
|
|
|
@ -126,8 +172,8 @@ public class BaseConverter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pack(); // 自动调整大小以适应所有组件
|
|
|
|
setVisible(true);
|
|
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|