我正在按照stanford.edu下学习java的编程方法课程,我尝试了一个程序,根据JTextField中使用的字体更改标签的字体,它响应输入键事件但它是不在JTextField中打印任何内容,而且我面临的一个问题是,当我展开窗口时,一切都是空白的,只显示窗口。我该怎样解决这个问题?
import acm.program.*;
import acm.graphics.*;
import acm.gui.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
public class EnterText extends GraphicsProgram {
public void init(){
//System.out.println("hello");
fontfield=new JTextField(MAX_FONT_NAME);
fontfield.setText("Enter your question here.");
this.fontfield.addActionListener(this);
fontfield.selectAll();
add(fontfield,SOUTH);
add(new JLabel("FONT"),SOUTH);
lasty=0;
lastlabel=new GLabel(str);
addGLabel(lastlabel);
addActionListeners();
addMouseListeners();
}
public void actionPerformed(ActionEvent e){
//System.out.println("hello");
if(e.getSource()==fontfield){
// println("hello" + fontfield.getText());
GLabel label=new GLabel("Hello");
label.setFont(lastlabel.getFont());
label.setFont(fontfield.getText());
addGLabel(label);
lastlabel=label;
}
}
private void addGLabel(GLabel label){
//System.out.println("hello");
lasty +=label.getHeight();
lasty +=lastlabel.getDescent()-label.getDescent();
add(label,LEFT_MARGIN,lasty);
}
private static final int MAX_FONT_NAME=150;
private static final int LEFT_MARGIN=30;
private static final String str="Hello Java";
private JTextField fontfield;
private GLabel lastlabel;
private double lasty;
}