0

我的代码创建了一个基本的注册屏幕,但在JTextArea objTE1您选择文本所在的位置之前不会显示。

import java.awt.*;
import javax.swing.*;
public class SignUpScreen extends Frame
{
    SignUpScreen()
    {
        super("No Layout Manager");
        setLayout(null);
        setTitle("Sign Up");
        setSize(400,700);
        show();
    }
    public static void main(String[] args)
    {
        Frame objFrame;
        JTextArea objTE1;

        JCheckBox objCheckMail;
        JCheckBox objCheckEmail;
        JCheckBox objCheckPhone;

        Button objButtonFinish;
        Button objButtonCancel;

        TextField objEnterEmail;
        TextField objEnterAdress;
        TextField objEnterPhone;
        TextField objEnterUsername;
        TextField objEnterPassword;
        TextField objConfirmPassword;


        objFrame = new SignUpScreen();
        objTE1 = new JTextArea();
        objCheckMail = new JCheckBox("Mail Notifications");
        objCheckEmail = new JCheckBox("Email Notifications");
        objCheckPhone = new JCheckBox("Text Notifications");

        objButtonFinish = new Button("Sign Up");
        objButtonCancel = new Button("Cancel");

        objEnterEmail = new TextField("Enter Email",0);
        objEnterAdress = new TextField("Enter Adress",0);
        objEnterPhone = new TextField("Enter Phone Number",0);
        objEnterUsername = new TextField("Enter Username",0);
        objEnterPassword = new TextField("Enter Password",0);
        objConfirmPassword = new TextField("Confirm Password",0);

        Font pagehead = new Font("Verdana", Font.BOLD, 24);
        objTE1.setFont(pagehead);
        objTE1.setForeground(Color.BLUE);

        objTE1.setText("     Sign up for the\nWatermelone Mail List");
        objTE1.setEditable(false);
            objTE1.setVisible(true);


        objTE1.setBounds(50, 75, 300, 70);

        objCheckMail.setBounds(100,450,200,50);
        objCheckEmail.setBounds(100,500,200,50);
        objCheckPhone.setBounds(100,550,200,50);

        objEnterEmail.setBounds(100,150,150,20);
        objEnterAdress.setBounds(100,200,200,20);
        objEnterPhone.setBounds(100,250,200,20);
        objEnterUsername.setBounds(100,300,200,20);
        objEnterPassword.setBounds(100,350,200,20);
        objConfirmPassword.setBounds(100,400,200,20);

        objButtonFinish.setBounds(50,600,120,60);
        objButtonCancel.setBounds(220,600,120,60);


        objFrame.add(objTE1);

        objFrame.add(objEnterEmail);

        objFrame.add(objButtonFinish);
        objFrame.add(objButtonCancel);

        objFrame.add(objEnterUsername);
        objFrame.add(objEnterPassword);
        objFrame.add(objConfirmPassword);
        objFrame.add(objEnterAdress);
        objFrame.add(objEnterPhone);

        objFrame.add(objCheckMail);
        objFrame.add(objCheckPhone);
        objFrame.add(objCheckEmail);
    }
}
4

1 回答 1

5
  1. 不要使用“空布局”。Swing 旨在与布局管理器一起使用。
  2. new JTextArea(5, 30);创建文本区域时使用。即指定文本区域的行/列。
  3. 不要在 Swing 应用程序中使用 AWT 组件。Swing 组件以“J”、JButton、JTextField、JFrame 开头。
  4. 不要使用show(). 正确的方法是setVisible(true)
  5. Event Dispatch Thread (EDT).
  6. 需要在setVisible(true)所有组件都添加到框架后完成。

我建议您阅读 Swing 教程中关于使用布局管理器的部分,了解一些使用布局管理器的示例,以及使用EDT.

于 2014-04-25T15:15:48.887 回答