master2
郑磊 10 months ago
parent 8b648f4af1
commit 53f37a60ce

@ -1,68 +1,99 @@
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Transform2158 extends JFrame {
public class Transform2158{
private JTextField inputField;
private JComboBox<String> fromBaseBox;
private JComboBox<String> toBaseBox;
private JTextArea resultArea;
public static String convertBase(String input, int fromBase, int toBase) {
try {
int number = Integer.parseInt(input, fromBase);
return Integer.toString(number, toBase);
} catch (NumberFormatException e) {
return null;
}
}
public Transform2158() {
setTitle("进制转换器");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // 居中显示
public static boolean isValidBase(int base) {
return base == 2 || base == 8 || base == 10 || base == 16;
}
initUI();
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("cli")) {
runCLI();
} else {
SwingUtilities.invokeLater(() -> new Transform2158().createAndShowGUI());
}
}
private void initUI() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
private static void runCLI() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入输入数字: ");
String input = scanner.nextLine();
panel.add(new JLabel("输入数字: "));
inputField = new JTextField();
panel.add(inputField);
System.out.print("请输入从进制 (2, 8, 10, 16): ");
int fromBase = scanner.nextInt();
panel.add(new JLabel("从进制: "));
fromBaseBox = new JComboBox<>(new String[]{"2", "8", "10", "16"});
panel.add(fromBaseBox);
System.out.print("请输入到进制 (2, 8, 10, 16): ");
int toBase = scanner.nextInt();
if (isValidBase(fromBase) && isValidBase(toBase)) {
String result = convertBase(input, fromBase, toBase);
if (result != null) {
System.out.println("转换结果: " + result);
} else {
System.out.println("输入的数字在指定的进制中无效。");
}
} else {
System.out.println("无效的进制,请输入 (2, 8, 10, 16)。");
}
panel.add(new JLabel("到进制: "));
toBaseBox = new JComboBox<>(new String[]{"2", "8", "10", "16"});
panel.add(toBaseBox);
scanner.close();
}
JButton convertButton = new JButton("转换");
convertButton.addActionListener(e -> performConversion());
panel.add(convertButton);
private void createAndShowGUI() {
JFrame frame = new JFrame("进制转换器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(null);
resultArea = new JTextArea(5, 20);
resultArea.setEditable(false);
panel.add(new JScrollPane(resultArea));
JButton button = new JButton("开始转换");
button.setBounds(80, 70, 140, 30);
button.addActionListener(new ConvertAction());
frame.add(button);
add(panel);
frame.setVisible(true);
}
private void performConversion() {
String input = inputField.getText();
int fromBase = Integer.parseInt((String) Objects.requireNonNull(fromBaseBox.getSelectedItem()));
int toBase = Integer.parseInt((String) Objects.requireNonNull(toBaseBox.getSelectedItem()));
private static class ConvertAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String input = JOptionPane.showInputDialog("请输入输入数字:");
if (input == null) return;
try {
int number = Integer.parseInt(input, fromBase);
String result = Integer.toString(number, toBase);
resultArea.setText("转换结果: " + result);
} catch (NumberFormatException e) {
resultArea.setText("输入的数字在指定的进制中无效。");
}
}
String fromBaseStr = JOptionPane.showInputDialog("请输入从进制 (2, 8, 10, 16):");
if (fromBaseStr == null) return;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Transform2158 ex = new Transform2158();
ex.setVisible(true);
});
String toBaseStr = JOptionPane.showInputDialog("请输入到进制 (2, 8, 10, 16):");
if (toBaseStr == null) return;
try {
int fromBase = Integer.parseInt(fromBaseStr);
int toBase = Integer.parseInt(toBaseStr);
if (isValidBase(fromBase) && isValidBase(toBase)) {
String result = convertBase(input, fromBase, toBase);
if (result != null) {
JOptionPane.showMessageDialog(null, "转换结果: " + result);
} else {
JOptionPane.showMessageDialog(null, "输入的数字在指定的进制中无效。");
}
} else {
JOptionPane.showMessageDialog(null, "无效的进制,请输入 (2, 8, 10, 16)。");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "输入的数字在指定的进制中无效。");
}
}
}
}

@ -24,4 +24,4 @@ public class Transform4381 {
System.out.println("输入的数字在指定的进制中无效。");
}
}
}
}
Loading…
Cancel
Save