Java Swing 编程实现曼彻斯特编码

曼彻斯特详解请看
博主只实现了标准版的曼彻斯特编码,本来想用图像输出的

主要思想是用监听获得输入框的内容,然后根据曼彻斯特编码输出到文本框中


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;

import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

public class Manchester extends JFrame {
    public static int TEXT_ROWS = 8;
    public static int TEXT_COLUMS = 20;
    final JTextArea textarea = new JTextArea(TEXT_ROWS, TEXT_COLUMS);
    private String str=null;

    public Manchester() {
        final JTextField textfield = new JTextField();
        textfield.getDocument().addDocumentListener(new textfielsLisener());

        JPanel northjpanel = new JPanel();
        northjpanel.setLayout(new GridLayout(1, 2));
        northjpanel.add(new JLabel("Manchester Code:", SwingConstants.RIGHT));
        northjpanel.add(textfield);
        add(northjpanel, BorderLayout.NORTH);

        JScrollPane scrollpane = new JScrollPane(textarea);
        add(scrollpane, BorderLayout.CENTER);

        pack();

    }

    //监听输入框的变化
    public class textfielsLisener implements DocumentListener {
        @Override
        public void removeUpdate(DocumentEvent e) {
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            try {
                //添加空格字符串 
                str = " "+e.getDocument().getText(
                        e.getDocument().getStartPosition().getOffset(),
                        e.getDocument().getLength());
            } catch (BadLocationException e1) {

                e1.printStackTrace();
            }
            for (int i = 0; i < str.length() ; i++) {
                //判断第一个
                if(i==1&&str.charAt(1) == '1'){
                    textarea.setText("");
                    textarea.append(" ̄|_");

                }else if(i==1&&str.charAt(1) == '0'){
                    textarea.setText("error");
                    textarea.append("_| ̄");
                }
                //判断前面的数字进行不同的输出
                else if (str.charAt(i) == '1'&&str.charAt(i-1) == '0') {
                    textarea.append(" ̄|_");
                }
                else if(str.charAt(i) == '0'&&str.charAt(i-1) == '1'){
                    textarea.append("_| ̄");

                }
                else if(str.charAt(i) == '1'&&str.charAt(i-1) == '1') {
                    textarea.append("| ̄|_");
                }
                else if(str.charAt(i) == '0'&&str.charAt(i-1) == '0') {

                    textarea.append("|_| ̄");
                }
                else{
                    textarea.setText("error");
                }
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }

    }

    public static void main(String[] args) throws InvocationTargetException,
            InterruptedException {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                Manchester manchester = new Manchester();
                manchester.setSize(600,300);
                manchester.setVisible(true);
            }
        });

    }

}

你可能感兴趣的:(java)