望版主采纳,代码包括界面功能,可以实现2,8,10,16进制之间任意转换
- package test.version01;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.Panel;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- /**
- * 程序功能:实现2进制,8进制,10进制,16进制之间的自由转换
- *
- * @author duanyitao
- *
- */
- public class Main extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- private Font font = null;
- private JLabel label1 = null;
- private JLabel label2 = null;
- private static JTextField textField1 = null;
- private static JTextField textField2 = null;
- private JPanel panel = null;
- private JComboBox comboBox01 = null;
- private JLabel label02 = null;
- private JComboBox comboBox02 = null;
- Object two = null;
- Object eight = null;
- Object ten = null;
- Object sixteenth = null;
- private JButton button = null;
- public Main() {
- font = new Font("微软雅黑", Font.BOLD, 16);
- label1 = new JLabel("需要转换的数:");
- label1.setHorizontalAlignment(0);
- label1.setFont(font);
- label2 = new JLabel("转换后的结果:");
- label2.setHorizontalAlignment(0);
- label2.setFont(font);
- textField1 = new JTextField();
- textField2 = new JTextField();
- comboBox01 = new JComboBox();
- comboBox01.setFont(font);
- label02 = new JLabel("转");
- label02.setFont(font);
- comboBox02 = new JComboBox();
- comboBox02.setFont(font);
- two = makeObj("二进制");
- eight = makeObj("八进制");
- ten = makeObj("十进制");
- sixteenth = makeObj("十六进制");
- comboBox01.addItem(two);
- comboBox01.addItem(eight);
- comboBox01.addItem(ten);
- comboBox01.addItem(sixteenth);
- comboBox02.addItem(two);
- comboBox02.addItem(eight);
- comboBox02.addItem(ten);
- comboBox02.addItem(sixteenth);
- panel = new JPanel();
- panel.setLayout(new FlowLayout());
- panel.add(comboBox01);
- panel.add(label02);
- panel.add(comboBox02);
- button = new JButton("转换");
- button.addActionListener(this);
- button.setFont(font);
- this.setLayout(new GridLayout(3, 3));
- this.add(label1);
- this.add(textField1);
- this.add(panel);
- this.add(new Panel());
- this.add(new Panel());
- this.add(button);
- this.add(label2);
- this.add(textField2);
- this.setTitle("进制转换");
- this.setSize(675, 130);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- }
- private Object makeObj(final String item) {
- return new Object() {
- public String toString() {
- return item;
- }
- };
- }
- public static void main(String[] args) {
- new Main().setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource().equals(button)) {
- try {
- caculate();
- } catch (Exception e2) {
- JOptionPane.showMessageDialog(this, "数据格式错误,请重新输入!");
- textField1.setText("");
- textField2.setText("");
- }
- }
- }
- private void caculate() {
- String str01 = comboBox01.getSelectedItem() + "";
- if (str01.equals("二进制")) {
- caculateTo(2);
- } else if (str01.equals("八进制")) {
- caculateTo(8);
- } else if (str01.equals("十进制")) {
- caculateTo(10);
- } else if (str01.equals("十六进制")) {
- caculateTo(16);
- }
- }
- private void caculateTo(int n) {
- String str02 = comboBox02.getSelectedItem() + "";
- if (str02.equals("二进制")) {
- changeAndSet(n, 2);
- } else if (str02.equals("八进制")) {
- changeAndSet(n, 8);
- } else if (str02.equals("十进制")) {
- changeAndSet(n, 10);
- } else if (str02.equals("十六进制")) {
- changeAndSet(n, 16);
- }
- }
- public static void changeAndSet(int src, int dest) {
- textField2.setText(Integer.toString(
- Integer.parseInt(textField1.getText(), src), dest));
- }
- }
复制代码 |