Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java text input question...why won't anything come up?

So I wrote this program. I want to get a text field that a person can write text into. If you notice at the very end, I gave it a shot and I've tried a few other things. I can't even get a text box to appear. Help! Give me something I can go off of...

Thank you so much!

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Toolkit;

import java.awt.geom.Ellipse2D;

import java.text.NumberFormat;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JTextArea;

public class windowName {

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable(){

public void run(){

SimpleFrame frame = new SimpleFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

});

}

}

class SimpleFrame extends JFrame {

public SimpleFrame(){

//The following four lines create the screenWidth and screenHeight variables

Toolkit kit = Toolkit.getDefaultToolkit();

Dimension screenSize = kit.getScreenSize();

int screenWidth = screenSize.width;

int screenHeight = screenSize.height;

//The following lines are the properties of the frame itself

setSize(screenWidth/3, screenHeight/2);

setTitle("We're going to take your name!");

setLocation(screenWidth/3,screenHeight/4); //These numbers center the window on the screen

getContentPane().setBackground(Color.black);

getContentPane().setForeground(Color.white);

//The following 2 lines create a new panel (which will be used to draw a circle)

DrawComponent component = new DrawComponent();

add(component);

}

}

class DrawComponent extends JComponent{

JTextArea text = new JTextArea("This is a test.",20,20); //Why isn't this creating a text field??

}

}

4 Answers

Relevance
  • Anonymous
    1 decade ago
    Favourite answer

    The code is all wrong

    Source(s): mtfbwy
  • 1 decade ago

    There are two things you have to do to get the DrawComponent to show.

    //The following 2 lines create a new panel (which will be used to draw a circle)

    DrawComponent component = new DrawComponent();

    add(component);

    Change these lines to:

    //The following 2 lines create a new panel (which will be used to draw a circle)

    DrawComponent component = new DrawComponent();

    getContentPane().add(component);

    class DrawComponent extends JComponent{

    JTextArea text = new JTextArea("This is a test.",20,20); //Why isn't this creating a text field??

    Change this to:

    class DrawComponent extends JComponent{

    JTextArea text = new JTextArea("This is a test.",20,20); //Why isn't this creating a text field??

    add(text);

    That should show it.

    Reasons being:

    1. You are not adding the DrawComponent to the contentPane of the SimpleFrame.

    2. You are not adding the text to the DrawComponent.

  • 1 decade ago

    In DrawComponent you are just subclassing JComponent and adding a field called text. That won't affect the drawing of the component. Check out the paint() method.

    That said, you extremely rarely have to subclass JComponent. Instead of add(component). Just do getContentPane().add(new JTextField()). And you probably want a pack() at the end.

  • Anonymous
    4 years ago

    there is the Java Speech API. link below. Any of the stt (speech-to-textual content cloth) frameworks require huge computing components to be ninety seven% precise. i comprehend a number of the militia hardware makes use of embedded 128-bit micro-controllers to learn get entry to to the operator's flight controls. And, the US government makes use of technologies unavailable to us to study digital communications. What you observed in the Bollywood movie is 'hollywood' -- computing device generated to seem that way. I definitely have used stt and any history noise in any respect, the noise that the human ear does no longer sign up, disrupts the acceptance potential of stt. good good fortune, however.

Still have questions? Get answers by asking now.