简体   繁体   English

直到您选择它,JTextArea才会显示

[英]JTextArea does not show up until you select it

My code creates a basic Sign Up screen, but the JTextArea objTE1 does not show up until you select where the text would be. 我的代码创建了一个基本的Sign Up屏幕,但是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);
    }
}
  1. Don't use a "null layout". 不要使用“空布局”。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。
  2. Use new JTextArea(5, 30); 使用new JTextArea(5, 30); when creating a text area. 创建文本区域时。 That is specify the rows/columns for the text area. 那就是指定文本区域的行/列。
  3. Don't use AWT components in a Swing application. 不要在Swing应用程序中使用AWT组件。 Swing components start with a "J", JButton, JTextField, JFrame. 摆动组件以“ J”,JButton,JTextField和JFrame开头。
  4. Don't use show() . 不要使用show() The proper method is setVisible(true) 正确的方法是setVisible(true)
  5. Create your GUI components on the Event Dispatch Thread (EDT) . Event Dispatch Thread (EDT)上创建GUI组件。
  6. The setVisible(true) needs to be done AFTER all the components have been added to the frame. 在将所有组件添加到框架之后,需要完成setVisible(true)

I suggest you read the section from the Swing tutorial on Using Layout Managers for some examples of using layout managers and on to use the EDT . 我建议您阅读Swing教程中有关使用布局管理器的部分,以获取一些使用布局管理器以及使用EDT示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM