master2
郑磊 10 months ago
parent 8d06a64bec
commit 8b648f4af1

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

Loading…
Cancel
Save