使用java编写一个有界面的进制转换应用程序在windows上使用
时间: 2023-05-30 20:04:39 浏览: 177
以下是一个简单的Java Swing应用程序,可以将十进制数转换为二进制、八进制和十六进制数。您可以将此代码复制并粘贴到您的IDE中,然后在Windows上运行它。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BinaryConverter implements ActionListener {
private JFrame frame;
private JPanel panel;
private JLabel label1, label2, label3, label4;
private JTextField inputField, binaryField, octalField, hexField;
private JButton convertButton;
public BinaryConverter() {
frame = new JFrame("Binary Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputField = new JTextField(10);
binaryField = new JTextField(10);
octalField = new JTextField(10);
hexField = new JTextField(10);
binaryField.setEditable(false);
octalField.setEditable(false);
hexField.setEditable(false);
label1 = new JLabel("Decimal:");
label2 = new JLabel("Binary:");
label3 = new JLabel("Octal:");
label4 = new JLabel("Hex:");
convertButton = new JButton("Convert");
convertButton.addActionListener(this);
panel = new JPanel(new GridLayout(5, 2, 5, 5));
panel.add(label1);
panel.add(inputField);
panel.add(label2);
panel.add(binaryField);
panel.add(label3);
panel.add(octalField);
panel.add(label4);
panel.add(hexField);
panel.add(new JLabel(""));
panel.add(convertButton);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
int decimal = Integer.parseInt(inputField.getText());
String binary = Integer.toBinaryString(decimal);
String octal = Integer.toOctalString(decimal);
String hex = Integer.toHexString(decimal);
binaryField.setText(binary);
octalField.setText(octal);
hexField.setText(hex);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter a valid decimal number.");
}
}
public static void main(String[] args) {
new BinaryConverter();
}
}
```
阅读全文