Igor
Estou enviando um exemplo de código que utiliza um JTextArea embutido num JScrollPane. Para controlar a forma de apresentação do texto dentro do JTextArea utilizo o método setLineWrap. Se estiver true, o texto terá quebra de linha automática; se false, não será realizada a quebra de linha; Se o setLineWrap não for declarado, por default é considerado false.
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;
/**
* @author Éder
*/
public class Index2 extends javax.swing.JFrame{
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
public Index2() {
initComponents();
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
//* * * Preste atenção aqui * * *
// jTextArea1.setLineWrap(true);
jTextArea1.setText("BBBBBAAAAAAQualquer coisa deve ser escrita aqui, o importante \u00e9 que ocuope v\u00e1rias linhas entenderammmmmmmmmmmmmmmmmm");
// jTextArea1.setLineWrap(false);
//* * * Ative e desative os setLineWrap
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 360));
pack();
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new Index2().show();
}
}
Esse código foi escrito no NetBeans 3.6. Se não tiveres as classes importadas, elas estão logo abaixo.
package org.netbeans.lib.awtextra;
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
* Microsystems, Inc. All Rights Reserved.
*/
import java.awt.Dimension;
import java.awt.Point;
/** An object that encapsulates position and (optionally) size for
* Absolute positioning of components.
*
* @see AbsoluteLayout
* @version 1.01, Aug 19, 1998
*/
public class AbsoluteConstraints implements java.io.Serializable {
/** generated Serialized Version UID */
static final long serialVersionUID = 5261460716622152494L;
/** The X position of the component */
public int x;
/** The Y position of the component */
public int y;
/** The width of the component or -1 if the component's preferred width should be used */
public int width = -1;
/** The height of the component or -1 if the component's preferred height should be used */
public int height = -1;
/** Creates a new AbsoluteConstraints for specified position.
* @param pos The position to be represented by this AbsoluteConstraints
*/
public AbsoluteConstraints(Point pos) {
this (pos.x, pos.y);
}
/** Creates a new AbsoluteConstraints for specified position.
* @param x The X position to be represented by this AbsoluteConstraints
* @param y The Y position to be represented by this AbsoluteConstraints
*/
public AbsoluteConstraints(int x, int y) {
this.x = x;
this.y = y;
}
/** Creates a new AbsoluteConstraints for specified position and size.
* @param pos The position to be represented by this AbsoluteConstraints
* @param size The size to be represented by this AbsoluteConstraints or null
* if the component's preferred size should be used
*/
public AbsoluteConstraints(Point pos, Dimension size) {
this.x = pos.x;
this.y = pos.y;
if (size != null) {
this.width = size.width;
this.height = size.height;
}
}
/** Creates a new AbsoluteConstraints for specified position and size.
* @param x The X position to be represented by this AbsoluteConstraints
* @param y The Y position to be represented by this AbsoluteConstraints
* @param width The width to be represented by this AbsoluteConstraints or -1 if the
* component's preferred width should be used
* @param height The height to be represented by this AbsoluteConstraints or -1 if the
* component's preferred height should be used
*/
public AbsoluteConstraints(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/** @return The X position represented by this AbsoluteConstraints */
public int getX () {
return x;
}
/** @return The Y position represented by this AbsoluteConstraints */
public int getY () {
return y;
}
/** @return The width represented by this AbsoluteConstraints or -1 if the
* component's preferred width should be used
*/
public int getWidth () {
return width;
}
/** @return The height represented by this AbsoluteConstraints or -1 if the
* component's preferred height should be used
*/
public int getHeight () {
return height;
}
public String toString () {
return super.toString () +" [x="+x+", y="+y+", width="+width+", height="+height+"]";
}
}
package org.netbeans.lib.awtextra;
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
* Microsystems, Inc. All Rights Reserved.
*/
import java.awt.*;
/** AbsoluteLayout is a LayoutManager that works as a replacement for "null" layout to
* allow placement of components in absolute positions.
*
* @see AbsoluteConstraints
* @version 1.01, Aug 19, 1998
*/
public class AbsoluteLayout implements LayoutManager2, java.io.Serializable {
/** generated Serialized Version UID */
static final long serialVersionUID = -1919857869177070440L;
/** Adds the specified component with the specified name to
* the layout.
* @param name the component name
* @param comp the component to be added
*/
public void addLayoutComponent(String name, Component comp) {
throw new IllegalArgumentException();
}
/** Removes the specified component from the layout.
* @param comp the component to be removed
*/
public void removeLayoutComponent(Component comp) {
constraints.remove(comp);
}
/** Calculates the preferred dimension for the specified
* panel given the components in the specified parent container.
* @param parent the component to be laid out
*
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Container parent) {
int maxWidth = 0;
int maxHeight = 0;
for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
Component comp = (Component)e.nextElement();
AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
Dimension size = comp.getPreferredSize();
int width = ac.getWidth ();
if (width == -1) width = size.width;
int height = ac.getHeight ();
if (height == -1) height = size.height;
if (ac.x + width > maxWidth)
maxWidth = ac.x + width;
if (ac.y + height > maxHeight)
maxHeight = ac.y + height;
}
return new Dimension (maxWidth, maxHeight);
}
/** Calculates the minimum dimension for the specified
* panel given the components in the specified parent container.
* @param parent the component to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Container parent) {
int maxWidth = 0;
int maxHeight = 0;
for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
Component comp = (Component)e.nextElement();
AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
Dimension size = comp.getMinimumSize();
int width = ac.getWidth ();
if (width == -1) width = size.width;
int height = ac.getHeight ();
if (height == -1) height = size.height;
if (ac.x + width > maxWidth)
maxWidth = ac.x + width;
if (ac.y + height > maxHeight)
maxHeight = ac.y + height;
}
return new Dimension (maxWidth, maxHeight);
}
/** Lays out the container in the specified panel.
* @param parent the component which needs to be laid out
*/
public void layoutContainer(Container parent) {
for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
Component comp = (Component)e.nextElement();
AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
Dimension size = comp.getPreferredSize();
int width = ac.getWidth ();
if (width == -1) width = size.width;
int height = ac.getHeight ();
if (height == -1) height = size.height;
comp.setBounds(ac.x, ac.y, width, height);
}
}
/** Adds the specified component to the layout, using the specified
* constraint object.
* @param comp the component to be added
* @param constr where/how the component is added to the layout.
*/
public void addLayoutComponent(Component comp, Object constr) {
if (!(constr instanceof AbsoluteConstraints))
throw new IllegalArgumentException();
constraints.put(comp, constr);
}
/** Returns the maximum size of this component.
* @see java.awt.Component#getMinimumSize()
* @see java.awt.Component#getPreferredSize()
* @see LayoutManager
*/
public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
/** Returns the alignment along the x axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentX(Container target) {
return 0;
}
/** Returns the alignment along the y axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentY(Container target) {
return 0;
}
/** Invalidates the layout, indicating that if the layout manager
* has cached information it should be discarded.
*/
public void invalidateLayout(Container target) {
}
/** A mapping <Component, AbsoluteConstraints> */
protected java.util.Hashtable constraints = new java.util.Hashtable();
}
O que eu posso ajudar está aí.
Boa sorte…