|
|
@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
|
|
|
|
public class BaseConverter {
|
|
|
|
public class BaseConverter {
|
|
|
@ -55,27 +59,75 @@ public class BaseConverter {
|
|
|
|
int decimalValue = toDecimal(str, sourceBase);
|
|
|
|
int decimalValue = toDecimal(str, sourceBase);
|
|
|
|
return fromDecimal(decimalValue, targetBase);
|
|
|
|
return fromDecimal(decimalValue, targetBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 主方法,用于测试
|
|
|
|
* 主方法,用于测试
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
EventQueue.invokeLater(() -> {
|
|
|
|
|
|
|
|
new BaseConverterGUI();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取用户输入
|
|
|
|
// GUI 类
|
|
|
|
System.out.print("请输入数据(例如 A1): ");
|
|
|
|
public static class BaseConverterGUI extends JFrame {
|
|
|
|
String input = scanner.nextLine().toUpperCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入源进制数(例如 16): ");
|
|
|
|
private JTextField inputField;
|
|
|
|
int sourceBase = scanner.nextInt();
|
|
|
|
private JTextField sourceBaseField;
|
|
|
|
|
|
|
|
private JTextField targetBaseField;
|
|
|
|
|
|
|
|
private JButton convertButton;
|
|
|
|
|
|
|
|
private JLabel resultLabel;
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入目标进制数(例如 2): ");
|
|
|
|
public BaseConverterGUI() {
|
|
|
|
int targetBase = scanner.nextInt();
|
|
|
|
setTitle("进制转换器");
|
|
|
|
|
|
|
|
setSize(400, 200);
|
|
|
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
setLocationRelativeTo(null); // 居中显示
|
|
|
|
|
|
|
|
setLayout(new GridLayout(5, 2));
|
|
|
|
|
|
|
|
|
|
|
|
// 进制转换
|
|
|
|
// 输入框
|
|
|
|
String result = convertBase(input, sourceBase, targetBase);
|
|
|
|
inputField = new JTextField();
|
|
|
|
|
|
|
|
add(new JLabel("输入数据(例如 A1):"));
|
|
|
|
|
|
|
|
add(inputField);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 源进制输入框
|
|
|
|
|
|
|
|
sourceBaseField = new JTextField();
|
|
|
|
|
|
|
|
add(new JLabel("源进制数(例如 16):"));
|
|
|
|
|
|
|
|
add(sourceBaseField);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 目标进制输入框
|
|
|
|
|
|
|
|
targetBaseField = new JTextField();
|
|
|
|
|
|
|
|
add(new JLabel("目标进制数(例如 2):"));
|
|
|
|
|
|
|
|
add(targetBaseField);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 转换按钮
|
|
|
|
|
|
|
|
convertButton = new JButton("转换");
|
|
|
|
|
|
|
|
add(new JLabel());
|
|
|
|
|
|
|
|
add(convertButton);
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("转换结果:" + result); // 输出转换结果
|
|
|
|
// 结果标签
|
|
|
|
|
|
|
|
resultLabel = new JLabel("转换结果:");
|
|
|
|
|
|
|
|
add(new JLabel());
|
|
|
|
|
|
|
|
add(resultLabel);
|
|
|
|
|
|
|
|
|
|
|
|
scanner.close();
|
|
|
|
// 添加监听器到按钮
|
|
|
|
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String input = inputField.getText().toUpperCase();
|
|
|
|
|
|
|
|
int sourceBase = Integer.parseInt(sourceBaseField.getText());
|
|
|
|
|
|
|
|
int targetBase = Integer.parseInt(targetBaseField.getText());
|
|
|
|
|
|
|
|
String result = convertBase(input, sourceBase, targetBase);
|
|
|
|
|
|
|
|
resultLabel.setText("转换结果:" + result);
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
|
|
JOptionPane.showMessageDialog(BaseConverterGUI.this, "输入错误,请检查后重试!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setVisible(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|