diff --git a/BaseConverter.java b/BaseConverter.java index e793c7f..41826fc 100644 --- a/BaseConverter.java +++ b/BaseConverter.java @@ -2,11 +2,11 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.Scanner; +import java.util.Arrays; +import java.util.List; public class BaseConverter { - // 字符数组,用于表示大于9的数字 private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; /** @@ -27,6 +27,7 @@ public class BaseConverter { } return result; } + /** * 将十进制整数转换为目标进制的字符串 * @@ -47,6 +48,7 @@ public class BaseConverter { return result.toString(); } + /** * 将任意进制的数转换为另一个任意进制的数 * @@ -73,8 +75,8 @@ public class BaseConverter { public static class BaseConverterGUI extends JFrame { private JTextField inputField; - private JTextField sourceBaseField; - private JTextField targetBaseField; + private JComboBox sourceBaseBox; + private JComboBox targetBaseBox; private JButton convertButton; private JLabel resultLabel; @@ -83,32 +85,76 @@ public class BaseConverter { setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // 居中显示 - setLayout(new GridLayout(5, 2)); + setLayout(new GridBagLayout()); + + GridBagConstraints gbc = new GridBagConstraints(); // 输入框 + gbc.gridx = 0; + gbc.gridy = 0; + gbc.anchor = GridBagConstraints.EAST; + gbc.insets = new Insets(5, 5, 5, 5); + add(new JLabel("输入数据(例如 A1):"), gbc); + + gbc.gridx = 1; + gbc.gridy = 0; + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.weightx = 1; + gbc.anchor = GridBagConstraints.WEST; 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); + add(inputField, gbc); + + // 源进制下拉菜单 + gbc.gridx = 0; + gbc.gridy = 1; + gbc.fill = GridBagConstraints.NONE; + gbc.weightx = 0; + gbc.anchor = GridBagConstraints.EAST; + add(new JLabel("源进制数(例如 16):"), gbc); + + gbc.gridx = 1; + gbc.gridy = 1; + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.weightx = 1; + gbc.anchor = GridBagConstraints.WEST; + List bases = Arrays.asList(2, 4, 8, 10, 16, 32); + sourceBaseBox = new JComboBox<>(bases.toArray(new Integer[0])); + add(sourceBaseBox, gbc); + + // 目标进制下拉菜单 + gbc.gridx = 0; + gbc.gridy = 2; + gbc.fill = GridBagConstraints.NONE; + gbc.weightx = 0; + gbc.anchor = GridBagConstraints.EAST; + add(new JLabel("目标进制数(例如 2):"), gbc); + + gbc.gridx = 1; + gbc.gridy = 2; + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.weightx = 1; + gbc.anchor = GridBagConstraints.WEST; + targetBaseBox = new JComboBox<>(bases.toArray(new Integer[0])); + add(targetBaseBox, gbc); // 转换按钮 + gbc.gridx = 1; + gbc.gridy = 3; + gbc.fill = GridBagConstraints.NONE; + gbc.weightx = 0; + gbc.anchor = GridBagConstraints.CENTER; convertButton = new JButton("转换"); - add(new JLabel()); - add(convertButton); + add(convertButton, gbc); // 结果标签 + gbc.gridx = 0; + gbc.gridy = 4; + gbc.gridwidth = 2; + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.weightx = 1; + gbc.anchor = GridBagConstraints.LINE_START; resultLabel = new JLabel("转换结果:"); - add(new JLabel()); - add(resultLabel); + add(resultLabel, gbc); // 添加监听器到按钮 convertButton.addActionListener(new ActionListener() { @@ -116,8 +162,8 @@ public class BaseConverter { public void actionPerformed(ActionEvent e) { try { String input = inputField.getText().toUpperCase(); - int sourceBase = Integer.parseInt(sourceBaseField.getText()); - int targetBase = Integer.parseInt(targetBaseField.getText()); + int sourceBase = (Integer) sourceBaseBox.getSelectedItem(); + int targetBase = (Integer) targetBaseBox.getSelectedItem(); String result = convertBase(input, sourceBase, targetBase); resultLabel.setText("转换结果:" + result); } catch (Exception ex) { @@ -126,8 +172,8 @@ public class BaseConverter { } }); + pack(); // 自动调整大小以适应所有组件 setVisible(true); } } - } \ No newline at end of file