?? code.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class code {
public static void main(String[] args)
{
codeFrame frame=new codeFrame();
frame.setTitle("二進制編碼器");
frame.setBounds(200, 100,400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class codeFrame extends JFrame
{
public codeFrame()
{
codePanel panel=new codePanel();
Container content=getContentPane();
content.add(panel);
}
}
class codePanel extends JPanel implements ActionListener
{
String str1=new String("雙向碼:");
String str2=new String("密勒碼:");
String str3=new String(" CMI碼:");
String str4=new String("AMI碼:");
String str5=new String("HDB3碼");
String str6=new String("差分碼:");
JLabel label=new JLabel("請輸入:");
JLabel codeLabel1=new JLabel(str1);
JLabel codeLabel2=new JLabel(str2);
JLabel codeLabel3=new JLabel(str3);
JLabel codeLabel4=new JLabel(str4);
JLabel codeLabel5=new JLabel(str5);
JLabel codeLabel6=new JLabel(str6);
TextField number=new TextField(33);
TextField toWrite1=new TextField(34);
TextField toWrite2=new TextField(34);
TextField toWrite3=new TextField(34);
TextField toWrite4=new TextField(34);
TextField toWrite5=new TextField(33);
TextField toWrite6=new TextField(33);
JButton button=new JButton("ALL");
JButton button1=new JButton("轉換");
JButton button2=new JButton("轉換");
JButton button3=new JButton("轉換");
JButton button4=new JButton("轉換");
JButton button5=new JButton("轉換");
JButton button6=new JButton("轉換");
private static String codeInput="";
public codePanel()
{
setLayout(new FlowLayout());
add(label);
add(number);
add(button);button.addActionListener(this);
add(codeLabel1);
add(toWrite1);toWrite1.setEditable(false);
add(button1);button1.addActionListener(this);
add(codeLabel2);
add(toWrite2);toWrite2.setEditable(false);
add(button2);button2.addActionListener(this);
add(codeLabel3);
add(toWrite3);toWrite3.setEditable(false);
add(button3);button3.addActionListener(this);
add(codeLabel4);
add(toWrite4);toWrite4.setEditable(false);
add(button4);button4.addActionListener(this);
add(codeLabel5);
add(toWrite5);toWrite5.setEditable(false);
add(button5);button5.addActionListener(this);
add(codeLabel6);
add(toWrite6);toWrite6.setEditable(false);
add(button6);button6.addActionListener(this);
add(new Label("聲明:版權所有,侵權必究!————舉報電話:13969651126"));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
String code=getManchester();
toWrite1.setText(code);
code=getMiller();
toWrite2.setText(code);
code=getCMI();
toWrite3.setText(code);
code=getAMI();
toWrite4.setText(code);
code=getHDB3();
toWrite5.setText(code);
code=getDivide();
toWrite6.setText(code);
}
if(e.getSource()==button1)
{
String code=getManchester();
toWrite1.setText(code);
}
if(e.getSource()==button2)
{
String code=getMiller();
toWrite2.setText(code);
}
if(e.getSource()==button3)
{
String code=getCMI();
toWrite3.setText(code);
}
if(e.getSource()==button4)
{
String code=getAMI();
toWrite4.setText(code);
}
if(e.getSource()==button5)
{
String code=getHDB3();
toWrite5.setText(code);
}
if(e.getSource()==button6)
{
String code=getDivide();
toWrite6.setText(code);
}
}
public String getManchester()
{
String codeManchester="";
String toAppend="";
codeInput=number.getText();
byte [] toDeal=codeInput.getBytes();
for(int i=0;i<toDeal.length;i++)
{
int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
if(str==1)
toAppend=" ̄_";
else if(str==0)
toAppend="_ ̄";
else
{
codeManchester="請輸入二進制代碼!!!";
break;
}
codeManchester+=toAppend;
}
return codeManchester;
}
public String getMiller()
{
String codeMiller="";
String toAppend="";
codeInput=number.getText();
boolean blowCheck=false;
boolean singleCheck=false;
byte [] toDeal=codeInput.getBytes();
for(int i=0;i<toDeal.length;i++)
{
int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
if(str==1)
{
if(blowCheck==false)
{
toAppend=" ̄_";
blowCheck=true;
}
else
{
toAppend="_ ̄";
blowCheck=false;
}
singleCheck=true;
}
else if(str==0)
{
if(singleCheck==true)
{
if(blowCheck==true)
toAppend="__";
else
toAppend=" ̄ ̄";
singleCheck=false;
}
else
{
if(blowCheck==true)
{
toAppend=" ̄ ̄";
blowCheck=false;
}
else
{
toAppend="__";
blowCheck=true;
}
}
}
else
{
codeMiller="請輸入二進制代碼!!!";
break;
}
codeMiller+=toAppend;
}
return codeMiller;
}
public String getCMI()
{
String codeCMI="";
String toAppend="";
codeInput=number.getText();
boolean oneCheck=false;
byte [] toDeal=codeInput.getBytes();
for(int i=0;i<toDeal.length;i++)
{
int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
if(str==1)
{
if(oneCheck==false)
{
toAppend=" ̄ ̄";
oneCheck=true;
}
else
{
toAppend="__";
oneCheck=false;
}
}
else if(str==0)
toAppend="_ ̄";
else
{
codeCMI="請輸入二進制代碼!!!";
break;
}
codeCMI+=toAppend;
}
return codeCMI;
}
public String getAMI()
{
String codeAMI="";
String toAppend="";
codeInput=number.getText();
boolean oneCheck=false;
byte [] toDeal=codeInput.getBytes();
for(int i=0;i<toDeal.length;i++)
{
int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
if(str==1)
{
if(oneCheck==false)
{
toAppend="_";
oneCheck=true;
}
else
{
toAppend=" ̄";
oneCheck=false;
}
}
else if(str==0)
{
toAppend="―";
}
else
{
codeAMI="請輸入二進制代碼!!!";
break;
}
codeAMI+=toAppend;
}
return codeAMI;
}
public String getHDB3()
{
String codeHDB3="";
codeInput=number.getText();
boolean oneCheck=true;
boolean vCheck=true;
boolean zeroMultiCheck=false;
int bCheck=0;
int zeroCount=0;
byte [] toDeal=codeInput.getBytes();
for(int i=0;i<toDeal.length;i++)
{
int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
if(str==1)
{
if(oneCheck==true)
{
codeHDB3+="_";
oneCheck=false;
}
else
{
codeHDB3+=" ̄";
oneCheck=true;
}
zeroMultiCheck=false;
zeroCount=0;
}
else if(str==0)
{
if(zeroCount!=3)
{
codeHDB3+="―";
zeroCount+=1;
}
else
{
codeHDB3=codeHDB3.substring(0,codeHDB3.length()-3);
if(zeroMultiCheck==false)
{
if(oneCheck==false&&vCheck==false)
{
codeHDB3+=" ̄―― ̄";
bCheck=1;vCheck=true;
}
else if(oneCheck==false&&vCheck==true)
{
codeHDB3+="―――_";
bCheck=0;vCheck=false;
}
else if(oneCheck==true&&vCheck==true)
{
codeHDB3+="_――_";
bCheck=-1;vCheck=false;
}
else
{
codeHDB3+="――― ̄";
bCheck=0;vCheck=true;
}
zeroMultiCheck=true;
}
else
{
if((bCheck==1&&vCheck==true)|(bCheck==0&&vCheck==true))
{
codeHDB3+="_――_";
bCheck=-1;vCheck=false;
}
if((bCheck==0&&vCheck==false)|(bCheck==-1&&vCheck==false))
{
codeHDB3+=" ̄―― ̄";
bCheck=1;vCheck=true;
}
}
zeroCount=0;
}
}
else
{
codeHDB3="請輸入二進制代碼!!!";
break;
}
}
return codeHDB3;
}
public String getDivide()
{
String codeDivide="";
String toAppend="";
boolean blowCheck=false;//默認協議初始碼“_ ̄”
codeInput=number.getText();
byte [] toDeal=codeInput.getBytes();
for(int i=0;i<toDeal.length;i++)
{
int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
if(str==1)
{
if(blowCheck==true)
toAppend=" ̄_";
else
toAppend="_ ̄";
}
else if(str==0)
{
if(blowCheck==true)
{
toAppend="_ ̄";
blowCheck=false;
}
else
{
toAppend=" ̄_";
blowCheck=true;
}
}
else
{
codeDivide="請輸入二進制代碼!!!";
break;
}
codeDivide+=toAppend;
}
return codeDivide;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -