Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Java AWT Form

883128Aug 18 2011 — edited Aug 18 2011
Hi everybody,
I'm developing the first Applets, I made a window extending Frame class and I used KeyListener to write on it from keyboard.
I'd like to have a method that resize the text creating a new paragraph everytime the text exceed the width of the window, such
a common Text Editor.

Do you know such a method or how I can realize one to accomplish this task?

Thanks in advance,
Paolo F.C.

P.S. here is my code :
*******************************************
package com.testapplet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Window extends Frame
{
	
	public String testo = "";
	public Ascoltatore asc;
	public AscoltaTastiera asc_tast;
	
	public static void main(String argv[])
	{
		Window wind = new Window();
	}
	
	Window()
	{
		setBounds(0,0,800,600);
		setTitle(getClass().getName());
		setVisible(true);
	    setBackground(Color.white);
	    setForeground(Color.black);
		asc = new Ascoltatore();
		asc_tast = new AscoltaTastiera(this);
		addWindowListener(asc);
		addKeyListener(asc_tast);
	}
	
	public void paint(Graphics g)
	{
		testo = asc_tast.testo;
		g.drawString(testo, 10, 22+14);
	}
}

*******************************************
package com.testapplet;

import java.awt.event.*;

public class Ascoltatore extends WindowAdapter
{
	public void windowClosed (WindowEvent evt)
	{
		System.out.println(evt);
		System.exit(0);
	}
	public void windowClosing (WindowEvent evt)
	{
		evt.getWindow().dispose();
		System.out.println(evt);
	}
	
}

*********************************************
package com.testapplet;

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

public class AscoltaTastiera implements KeyListener
{
	public String testo = "";
	public Frame parent;
	public AscoltaTastiera(Frame frame)
	{
		this.parent = frame;
	}
			public void keyPressed(KeyEvent e)
			{}
			public void keyReleased(KeyEvent e)
			{}
			public void keyTyped(KeyEvent e)
			{
				testo += e.getKeyChar();
				if (testo != null)
			    parent.repaint();
			}
		}
Edited by: 880125 on 18-ago-2011 5.49

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 15 2011
Added on Aug 18 2011
2 comments
1,965 views