|
|
|
@ -3,68 +3,66 @@ import java.awt.*;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
public class Transform2158 extends JFrame {
|
|
|
|
|
|
|
|
|
|
private JTextField inputField;
|
|
|
|
|
private JComboBox<String> fromBaseComboBox;
|
|
|
|
|
private JComboBox<String> toBaseComboBox;
|
|
|
|
|
private JTextField resultField;
|
|
|
|
|
private JComboBox<String> fromBaseBox;
|
|
|
|
|
private JComboBox<String> toBaseBox;
|
|
|
|
|
private JTextArea resultArea;
|
|
|
|
|
|
|
|
|
|
public Transform2158() {
|
|
|
|
|
// 初始化界面
|
|
|
|
|
setTitle("进制转换");
|
|
|
|
|
setTitle("进制转换器");
|
|
|
|
|
setSize(400, 300);
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
setLocationRelativeTo(null); // 居中显示
|
|
|
|
|
|
|
|
|
|
initUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initUI() {
|
|
|
|
|
JPanel panel = new JPanel(new GridLayout(4, 2));
|
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
|
panel.setLayout(new GridLayout(5, 2));
|
|
|
|
|
|
|
|
|
|
JLabel inputLabel = new JLabel("输入数字:");
|
|
|
|
|
inputField = new JTextField(20);
|
|
|
|
|
panel.add(inputLabel);
|
|
|
|
|
panel.add(new JLabel("输入数字: "));
|
|
|
|
|
inputField = new JTextField();
|
|
|
|
|
panel.add(inputField);
|
|
|
|
|
|
|
|
|
|
JLabel fromBaseLabel = new JLabel("从进制:");
|
|
|
|
|
String[] bases = {"2", "8", "10", "16"};
|
|
|
|
|
fromBaseComboBox = new JComboBox<>(bases);
|
|
|
|
|
panel.add(fromBaseLabel);
|
|
|
|
|
panel.add(fromBaseComboBox);
|
|
|
|
|
panel.add(new JLabel("从进制: "));
|
|
|
|
|
fromBaseBox = new JComboBox<>(new String[]{"2", "8", "10", "16"});
|
|
|
|
|
panel.add(fromBaseBox);
|
|
|
|
|
|
|
|
|
|
JLabel toBaseLabel = new JLabel("到进制:");
|
|
|
|
|
toBaseComboBox = new JComboBox<>(bases);
|
|
|
|
|
panel.add(toBaseLabel);
|
|
|
|
|
panel.add(toBaseComboBox);
|
|
|
|
|
panel.add(new JLabel("到进制: "));
|
|
|
|
|
toBaseBox = new JComboBox<>(new String[]{"2", "8", "10", "16"});
|
|
|
|
|
panel.add(toBaseBox);
|
|
|
|
|
|
|
|
|
|
JButton convertButton = new JButton("转换");
|
|
|
|
|
convertButton.addActionListener(e -> performConversion());
|
|
|
|
|
panel.add(convertButton);
|
|
|
|
|
|
|
|
|
|
JLabel resultLabel = new JLabel("结果:");
|
|
|
|
|
resultField = new JTextField(20);
|
|
|
|
|
resultField.setEditable(false);
|
|
|
|
|
panel.add(resultLabel);
|
|
|
|
|
panel.add(resultField);
|
|
|
|
|
resultArea = new JTextArea(5, 20);
|
|
|
|
|
resultArea.setEditable(false);
|
|
|
|
|
panel.add(new JScrollPane(resultArea));
|
|
|
|
|
|
|
|
|
|
add(panel);
|
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void performConversion() {
|
|
|
|
|
String input = inputField.getText();
|
|
|
|
|
int fromBase = Integer.parseInt((String) Objects.requireNonNull(fromBaseComboBox.getSelectedItem()));
|
|
|
|
|
int toBase = Integer.parseInt((String) Objects.requireNonNull(toBaseComboBox.getSelectedItem()));
|
|
|
|
|
int fromBase = Integer.parseInt((String) Objects.requireNonNull(fromBaseBox.getSelectedItem()));
|
|
|
|
|
int toBase = Integer.parseInt((String) Objects.requireNonNull(toBaseBox.getSelectedItem()));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int number = Integer.parseInt(input, fromBase);
|
|
|
|
|
String result = Integer.toString(number, toBase);
|
|
|
|
|
resultField.setText(result);
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "输入错误,请检查输入格式。", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
resultArea.setText("转换结果: " + result);
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
resultArea.setText("输入的数字在指定的进制中无效。");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SwingUtilities.invokeLater(Transform2158::new);
|
|
|
|
|
EventQueue.invokeLater(() -> {
|
|
|
|
|
Transform2158 ex = new Transform2158();
|
|
|
|
|
ex.setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|