|
|
|
|
@ -1,84 +0,0 @@
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
|
|
public class BaseConversionGUI extends JFrame {
|
|
|
|
|
|
|
|
|
|
private JTextField inputNumber;
|
|
|
|
|
private JTextField originalBase;
|
|
|
|
|
private JTextField targetBase;
|
|
|
|
|
private JTextArea resultArea;
|
|
|
|
|
|
|
|
|
|
public BaseConversionGUI() {
|
|
|
|
|
setTitle("进制转换器");
|
|
|
|
|
setSize(400, 300);
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
setLocationRelativeTo(null);
|
|
|
|
|
setLayout(null);
|
|
|
|
|
|
|
|
|
|
// 输入数字标签和文本框
|
|
|
|
|
JLabel numberLabel = new JLabel("输入数字(2-16进制):");
|
|
|
|
|
numberLabel.setBounds(20, 20, 200, 25);
|
|
|
|
|
add(numberLabel);
|
|
|
|
|
|
|
|
|
|
inputNumber = new JTextField();
|
|
|
|
|
inputNumber.setBounds(220, 20, 150, 25);
|
|
|
|
|
add(inputNumber);
|
|
|
|
|
|
|
|
|
|
// 原进制标签和文本框
|
|
|
|
|
JLabel baseLabel1 = new JLabel("原进制 (R1, 2-16):");
|
|
|
|
|
baseLabel1.setBounds(20, 60, 200, 25);
|
|
|
|
|
add(baseLabel1);
|
|
|
|
|
|
|
|
|
|
originalBase = new JTextField();
|
|
|
|
|
originalBase.setBounds(220, 60, 150, 25);
|
|
|
|
|
add(originalBase);
|
|
|
|
|
|
|
|
|
|
// 目标进制标签和文本框
|
|
|
|
|
JLabel baseLabel2 = new JLabel("目标进制 (R2, 2-16):");
|
|
|
|
|
baseLabel2.setBounds(20, 100, 200, 25);
|
|
|
|
|
add(baseLabel2);
|
|
|
|
|
|
|
|
|
|
targetBase = new JTextField();
|
|
|
|
|
targetBase.setBounds(220, 100, 150, 25);
|
|
|
|
|
add(targetBase);
|
|
|
|
|
|
|
|
|
|
// 转换按钮
|
|
|
|
|
JButton convertButton = new JButton("转换");
|
|
|
|
|
convertButton.setBounds(150, 140, 100, 30);
|
|
|
|
|
add(convertButton);
|
|
|
|
|
|
|
|
|
|
// 结果区域
|
|
|
|
|
resultArea = new JTextArea();
|
|
|
|
|
resultArea.setBounds(20, 180, 350, 60);
|
|
|
|
|
resultArea.setEditable(false);
|
|
|
|
|
add(resultArea);
|
|
|
|
|
|
|
|
|
|
// 按钮事件处理
|
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
String x = inputNumber.getText();
|
|
|
|
|
int r1 = Integer.parseInt(originalBase.getText());
|
|
|
|
|
int r2 = Integer.parseInt(targetBase.getText());
|
|
|
|
|
|
|
|
|
|
// 进行进制转换
|
|
|
|
|
String result = BaseConversion.baseConversion(x, r1, r2);
|
|
|
|
|
resultArea.setText(x + " 从 " + r1 + " 进制转换到 " + r2 + " 进制的结果是: " + result);
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
resultArea.setText("请输入有效的数字和进制。");
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
resultArea.setText("发生错误:" + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
BaseConversionGUI gui = new BaseConversionGUI();
|
|
|
|
|
gui.setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|