用java实现一个能够进行各种进位计数制的转换应用程序事件处理
时间: 2024-05-09 08:14:17 浏览: 107
好的,以下是Java实现进制转换的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberSystemConverter extends JFrame implements ActionListener {
private JTextField inputField;
private JTextField outputField;
private JComboBox<String> fromComboBox;
private JComboBox<String> toComboBox;
private JButton convertButton;
public NumberSystemConverter() {
setTitle("Number System Converter");
setSize(400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1;
inputField = new JTextField(10);
outputField = new JTextField(10);
outputField.setEditable(false);
fromComboBox = new JComboBox<String>(new String[]{"Decimal", "Binary", "Octal", "Hexadecimal"});
toComboBox = new JComboBox<String>(new String[]{"Decimal", "Binary", "Octal", "Hexadecimal"});
convertButton = new JButton("Convert");
convertButton.addActionListener(this);
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(inputField, constraints);
constraints.gridx = 1;
panel.add(fromComboBox, constraints);
constraints.gridx = 2;
panel.add(toComboBox, constraints);
constraints.gridx = 3;
panel.add(convertButton, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(outputField, constraints);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
String input = inputField.getText();
int from = fromComboBox.getSelectedIndex();
int to = toComboBox.getSelectedIndex();
String output = "";
switch (from) {
case 0: // Decimal
switch (to) {
case 0: output = input; break;
case 1: output = Integer.toBinaryString(Integer.parseInt(input)); break;
case 2: output = Integer.toOctalString(Integer.parseInt(input)); break;
case 3: output = Integer.toHexString(Integer.parseInt(input)); break;
}
break;
case 1: // Binary
switch (to) {
case 0: output = Integer.toString(Integer.parseInt(input, 2)); break;
case 1: output = input; break;
case 2: output = Integer.toOctalString(Integer.parseInt(input, 2)); break;
case 3: output = Integer.toHexString(Integer.parseInt(input, 2)); break;
}
break;
case 2: // Octal
switch (to) {
case 0: output = Integer.toString(Integer.parseInt(input, 8)); break;
case 1: output = Integer.toBinaryString(Integer.parseInt(input, 8)); break;
case 2: output = input; break;
case 3: output = Integer.toHexString(Integer.parseInt(input, 8)); break;
}
break;
case 3: // Hexadecimal
switch (to) {
case 0: output = Integer.toString(Integer.parseInt(input, 16)); break;
case 1: output = Integer.toBinaryString(Integer.parseInt(input, 16)); break;
case 2: output = Integer.toOctalString(Integer.parseInt(input, 16)); break;
case 3: output = input; break;
}
break;
}
outputField.setText(output);
} catch (NumberFormatException ex) {
outputField.setText("Invalid input");
}
}
public static void main(String[] args) {
new NumberSystemConverter();
}
}
```
该应用程序界面包含两个文本框,分别用于输入和输出转换的数字,以及两个下拉框,用于选择输入和输出的进制类型。当用户单击“Convert”按钮时,程序将根据选择的进制类型将输入的数字转换为所需的进制,并在输出文本框中显示结果。此示例支持四种进制类型:十进制、二进制、八进制和十六进制。
阅读全文