📄 hffgui.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class MyCanvas extends Canvas
{
MyCanvas()
{
setSize(50,500);
}
public void paint(Graphics g)
{
/*
input "a good wood"
encoding:
a 100 1
g 101 1
w 110 1
d 111 2
o 0 4
*/
g.setColor(Color.pink);
String s1="100q";
String s2="11s";
String s3="1010w";
String s4="1011u";
for(int j=0;j<=3;j++)
{
int y=70;
String s="";
if(j==0)s=s1;
else if(j==1)s=s2;
else if(j==2)s=s3;
else if(j==3)s=s4;
int index=0;
int length=s.length();
int x[]=new int[length+1];
x[0]=270;
g.fillOval(x[0],y,30,30);
g.drawString("",265,82);
x[index+1]=x[index];
for(index=0;index<=length-2;index++)
{char c=s.charAt(index);
if(c=='1')
{
x[index+1]=x[index]+60;
g.drawLine(x[index]+15,y+=15,x[index+1]+15,y+55);
g.setColor(Color.black);
g.drawString("1",(x[index+1]+15+x[index]+15)/2,y+35);
g.setColor(Color.magenta);
g.fillOval(x[index+1],y+=40,30,30);
}
else if(c=='0')
{
x[index+1]=x[index]-50;
g.drawLine(x[index]+15,y+=15,x[index+1]+15,y+55);
g.setColor(Color.black);
g.drawString("0",(x[index+1]+15+x[index]+15)/2,y+35);
g.setColor(Color.BLUE);
g.fillOval(x[index+1],y+=40,30,30);
}
if(index==length-2){g.setColor(Color.black);
if(j==0){g.drawString("q3",x[index+1]+15,y+15);}
else if(j==1){g.drawString("s8",x[index+1]+15,y+15);}
else if(j==2){g.drawString("w1",x[index+1]+15,y+15);}
else if(j==3){g.drawString("u3",x[index+1]+15,y+15);}
}
}
}
g.setFont(new Font("txt",Font.BOLD+Font.ITALIC,28));
}
}
public class hffgui extends Frame
{
Frame f=new Frame();
Button btn11=new Button("编码读入:");
Button btn12=new Button("译码读入:");
Button btn2=new Button("编码结果");
Button btn3=new Button("译码结果");
Button btn4=new Button("退出");
TextArea txt1=new TextArea();
TextArea txt11=new TextArea();
TextArea txt2=new TextArea();
TextArea txt21=new TextArea();
ButtonListener btnlistener=new ButtonListener();
public hffgui()
{
MyCanvas c=new MyCanvas();
c.setBounds(0,20,800,600);
c.setBackground(Color.cyan);
f.setLayout(null);
f.add(btn11);
f.add(btn12);
f.add(btn2);
f.add(btn3);
f.add(btn4);
f.add(txt1);
f.add(txt11);
f.add(txt2);
f.add(txt21);
f.add(c);
btn11.setBounds(30,30,100,40);//设置大小
btn12.setBounds(30,380,100,40);//设置大小
btn2.setBounds(30,450,100,40);
btn3.setBounds(30,550,100,40);
btn4.setBounds(30,650,100,40);
txt1.setBounds(150,30,450,50);
txt2.setBounds(150,380,450,50);
txt11.setBounds(150,450,450,50);
txt21.setBounds(150,550,450,50);
btn11.addActionListener(btnlistener);
btn12.addActionListener(btnlistener);//监听
btn2.addActionListener(btnlistener);
btn3.addActionListener(btnlistener);
btn4.addActionListener(btnlistener);
btn11.setForeground(Color.DARK_GRAY);
btn12.setForeground(Color.DARK_GRAY);//设置颜色
btn2.setForeground(Color.DARK_GRAY);
btn3.setForeground(Color.DARK_GRAY);
btn4.setForeground(Color.DARK_GRAY);
btn11.setBackground(Color.GREEN);
btn12.setBackground(Color.GREEN);
btn2.setBackground(Color.GREEN);
btn3.setBackground(Color.GREEN);
btn4.setBackground(Color.GREEN);
setBackground(Color.YELLOW);//设置背景颜色(已被覆盖,转变为边框颜色)
f.setSize(700,750);
f.setTitle("HuffmanTree Demo");
f.setVisible(true);
f.setResizable(false);
//使窗口能正常关闭
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("编码读入:"))
{
}
else if(e.getActionCommand().equals("编码结果")){
}
else if(e.getActionCommand().equals("译码读入:"))
{
}
else if(e.getActionCommand().equals("译码结果"))
{
}
else if(e.getActionCommand().equals("退出"))
{
System.exit(1);
}
}
}
public static void main(String args[])
{
hffgui x=new hffgui();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -