|
|
@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
package com.example.baseconverter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BaseConverterGUI extends JFrame {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel contentPane;
|
|
|
|
|
|
|
|
private JTextField sourceBaseField;
|
|
|
|
|
|
|
|
private JTextField sourceNumberField;
|
|
|
|
|
|
|
|
private JTextField targetBaseField;
|
|
|
|
|
|
|
|
private JButton convertButton;
|
|
|
|
|
|
|
|
private JLabel resultLabel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseConverterGUI() {
|
|
|
|
|
|
|
|
setTitle("Base Converter");
|
|
|
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
setBounds(100, 100, 450, 150);
|
|
|
|
|
|
|
|
contentPane = new JPanel();
|
|
|
|
|
|
|
|
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
|
|
|
|
|
|
|
setContentPane(contentPane);
|
|
|
|
|
|
|
|
contentPane.setLayout(null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JLabel sourceBaseLabel = new JLabel("原始进制 (2-16):");
|
|
|
|
|
|
|
|
sourceBaseLabel.setBounds(10, 11, 85, 14);
|
|
|
|
|
|
|
|
contentPane.add(sourceBaseLabel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sourceBaseField = new JTextField();
|
|
|
|
|
|
|
|
sourceBaseField.setBounds(99, 8, 86, 20);
|
|
|
|
|
|
|
|
contentPane.add(sourceBaseField);
|
|
|
|
|
|
|
|
sourceBaseField.setColumns(10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JLabel sourceNumberLabel = new JLabel("原始数:");
|
|
|
|
|
|
|
|
sourceNumberLabel.setBounds(10, 42, 85, 14);
|
|
|
|
|
|
|
|
contentPane.add(sourceNumberLabel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sourceNumberField = new JTextField();
|
|
|
|
|
|
|
|
sourceNumberField.setBounds(99, 39, 86, 20);
|
|
|
|
|
|
|
|
contentPane.add(sourceNumberField);
|
|
|
|
|
|
|
|
sourceNumberField.setColumns(10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JLabel targetBaseLabel = new JLabel("目标进制 (2-16):");
|
|
|
|
|
|
|
|
targetBaseLabel.setBounds(10, 73, 85, 14);
|
|
|
|
|
|
|
|
contentPane.add(targetBaseLabel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
targetBaseField = new JTextField();
|
|
|
|
|
|
|
|
targetBaseField.setBounds(99, 70, 86, 20);
|
|
|
|
|
|
|
|
contentPane.add(targetBaseField);
|
|
|
|
|
|
|
|
targetBaseField.setColumns(10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
convertButton = new JButton("转换");
|
|
|
|
|
|
|
|
convertButton.setBounds(199, 110, 89, 23);
|
|
|
|
|
|
|
|
contentPane.add(convertButton);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resultLabel = new JLabel("");
|
|
|
|
|
|
|
|
resultLabel.setBounds(299, 70, 130, 14);
|
|
|
|
|
|
|
|
contentPane.add(resultLabel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int sourceBase = Integer.parseInt(sourceBaseField.getText());
|
|
|
|
|
|
|
|
String sourceNumber = sourceNumberField.getText();
|
|
|
|
|
|
|
|
int targetBase = Integer.parseInt(targetBaseField.getText());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sourceBase < 2 || sourceBase > 16 || targetBase < 2 || targetBase > 16) {
|
|
|
|
|
|
|
|
resultLabel.setText("进制必须在2到16之间!");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int decimalValue = BaseConverter.toDecimal(sourceNumber, sourceBase);
|
|
|
|
|
|
|
|
String targetNumber = BaseConverter.fromDecimal(decimalValue, targetBase);
|
|
|
|
|
|
|
|
resultLabel.setText("转换后的数是: " + targetNumber);
|
|
|
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
|
|
|
resultLabel.setText("请输入有效的数字");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
EventQueue.invokeLater(new Runnable() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
BaseConverterGUI frame = new BaseConverterGUI();
|
|
|
|
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|