|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
@ -46,7 +47,7 @@ public class BaseConversion2 {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
JFrame frame = new JFrame("进制转换器");
|
|
|
|
|
JFrame frame = new JFrame("基数转换器");
|
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
frame.setSize(400, 300);
|
|
|
|
|
frame.setLayout(new GridLayout(5, 2));
|
|
|
|
@ -65,4 +66,33 @@ public class BaseConversion2 {
|
|
|
|
|
|
|
|
|
|
convertButton.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerf
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
int sourceBase = Integer.parseInt(sourceBaseField.getText());
|
|
|
|
|
int targetBase = Integer.parseInt(targetBaseField.getText());
|
|
|
|
|
String number = numberField.getText();
|
|
|
|
|
|
|
|
|
|
int decimalValue = convertToDecimal(number, sourceBase);
|
|
|
|
|
String result = convertFromDecimal(decimalValue, targetBase);
|
|
|
|
|
|
|
|
|
|
resultLabel.setText("转换结果: " + result);
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
resultLabel.setText("请输入有效的数字和进制!");
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
resultLabel.setText("转换过程中发生错误: " + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
frame.add(sourceBaseLabel);
|
|
|
|
|
frame.add(sourceBaseField);
|
|
|
|
|
frame.add(targetBaseLabel);
|
|
|
|
|
frame.add(targetBaseField);
|
|
|
|
|
frame.add(numberLabel);
|
|
|
|
|
frame.add(numberField);
|
|
|
|
|
frame.add(convertButton);
|
|
|
|
|
frame.add(resultLabel);
|
|
|
|
|
|
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|