我是新手,所以下面的代码可能会有很多问题 它应该从文本字段中获取用户书面文本并为其绘制摩尔斯电码。它获取文本,将CharArray形式的String传输给此类,但paint方法不绘制任何内容(在paint方法中无法识别变量!!)。
输入:构造函数获取另一个类的链接(或其中的内容),字符串中的char以及字符串中的位置编号 输出:只绘制最后3行,不识别外部绘制方法的其他变量!
以下是不起作用的课程:
public class Procesare extends Canvas{
Morse fereastra;
String lit="",cod="";
char car;
int nr;
public Procesare(Morse parinte,char c,int i){
fereastra=parinte;
car=c; // with or without "this" doesn't seem to transmit values to the class
nr=i;
//this.lit=String.valueOf(c);
switch (c){
//this part is just testing that values were transmited, it works
case 'a':
fereastra.canv.setBackground(Color.orange);
System.out.println(c+" .-");
break;
case 'b':
fereastra.canv.setBackground(Color.blue);
System.out.println(c+" -...");
break;
case 'c':
fereastra.canv.setBackground(Color.red);
System.out.println(c+" -.-.");
break;
case 'd':
fereastra.canv.setBackground(Color.green);
System.out.println(c+" -..");
break;
default: break;
}
}
public void paint(Graphics g){
lit=String.valueOf(car);
switch (lit){
case "a": cod=".-"; break;
case "b": cod="-..."; break;
default: break;
}
g.drawString(lit,5,10+nr*10);
g.drawString(cod,15,10+nr*10);
g.drawString(lit,5,10); // these aren't drawing anything !?!?
g.fillOval(15, 3, 6, 6); // just testing
g.drawString("z",5,20);
g.fillOval(15, 13, 6, 6);
}
}
主要类,有效:
public class Morse extends Frame {
xText camp;
Procesare proc,canv;
String s1, str;
char[] cuv;
int n;
public Morse(String titlu){
super(titlu);
init();
}
public static void main (String args[]){
Morse app=new Morse("Morse much?");
app.setSize(250,250);
app.show();
}
public boolean handleEvent(Event evt) {
if (evt.id==Event.WINDOW_DESTROY)
{System.exit(0);}
return super.handleEvent(evt);
}
public void init(){
setLayout(new GridLayout(3,1,10,20));
setBackground(Color.gray);
canv=new Procesare(this,' ',0);
canv.setBackground(Color.orange);
camp=new xText(this,"Introduceti textul aici:");
add(camp);
add(canv);
}
public Insets getInsets(){
return new Insets(30,30,30,30);
}
void update(xText input){
s1=input.cText1.getText();
cuv = s1.toCharArray();
for(n=0;n<s1.length();n++){
proc=new Procesare(this,cuv[n],n);
}
}
}