为什么代码不打印2个文本框和一个按钮?

时间:2018-01-09 02:34:04

标签: java

这是代码。它是一个基本的事件处理程序。我得到的输出非常荒谬。第一个文本框看起来很大,而第二个文本框看起来很小。虽然" Ok"按钮完全不可见。

 import java.awt.*;
 import java.awt.event.*;

 class alpha extends Frame implements ActionListener
 {
   TextField t1,t2;
   Button B;
   String msg;
   alpha()
 {
     t1=new TextField(10);
     t2=new TextField(10);
     B=new Button("OK");
     t1.setBounds(30,30,300,300);
     t2.setBounds(30,50,300,300);
     B.setBounds(30,80,300,300);
     add(t1);
     add(t2);
     add(B);
     setSize(500,500);
     setVisible(true);
     setLayout(null);
 }
  public void actionPerformed(ActionEvent x)
 {
     msg=x.getActionCommand();
     if(msg.equals("OK"));
     {
        t2.setText("Welcome");
     }
   }
  }
  public class frame
 {
    public static void main(String args[])
   {
     alpha f=new alpha();
   }
 }

1 个答案:

答案 0 :(得分:0)

我建议改用Swing。您可以尝试以下代码作为开始。 HTH。

import javax.swing.*;

public class alpha 
{
    JTextField t1;
    JTextField t2;
    JButton B;
    String msg;
    public alpha()
    {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10,10,600,600);
        panel.setLayout(null);

        t1=new JTextField();
        t2=new JTextField(10);
        B=new JButton("OK");
        t1.setBounds(5,30,100,20);
        t2.setBounds(5,50,100,20);
        B.setBounds(5,80,100,20);

        panel.add(t1);
        panel.add(t2);
        panel.add(B);

        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String args[])
    {
        new alpha();
    }

}