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

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