Java窗体问题。这个程序错在哪了?

来源:百度知道 编辑:UC知道 时间:2024/06/24 06:48:50
import java.awt.*;
import java.awt.event.*;
public class wzuoye1 extends Frame implements ActionListener
{
Label label1;
Label label2;
TextField input;
String s;
public wzuoye1()
{
super("test");
input=new TextField();
label1=new Label("请输入姓名!");
label2=new Label(" ");
setLayout(new BorderLayout(2,2));
add(label1,BorderLayout.CENTER);
add(input,BorderLayout.CENTER);
add(label2,BorderLayout.CENTER);
input.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
s=input.getText();
label2.setText("您好, "+s+" 欢迎使用Java编程!");
}

class closeWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window w=e.getWindow();
w.dispose();
}
}
public static void main(String args[])
{

改正后的代码如下,我编译过。完全正确。
你的代码的错误点:1,FRAME要继承于SWING的JFRAME。
2. add(label1,BorderLayout.CENTER);
add(input,BorderLayout.SOUTH);
add(label2,BorderLayout.NORTH);
如果都是BorderLayout.CENTER,是把三个组件都放在FRAME的中间位置,结果只能显示最后一个。
3,为什么要写嵌套类?我把两个类给分开了。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class wzuoye1 extends JFrame implements ActionListener
{
Label label1;
Label label2;
TextField input;
String s;
public wzuoye1()
{
super("test");
input=new TextField();
label1=new Label("ÇëÊäÈëÐÕÃû!");
label2=new Label(" ");
setLayout(new BorderLayout(2,2));
add(label1,BorderLayout.CENTER);
add(input,BorderLayout.SOUTH);
add(label2,BorderLayout.NORTH);
input.addActionListener(this);
}
public void actionPerformed(Ac