My apologies I went back and tried what you said and it worked this
is what I did
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mywindow {
public static void main(String[] args) {
CenteredFrame frame = new CenteredFrame();
JTextArea text = new JTextArea("This is a test",5,30);
text.setLineWrap(true);
text.setWrapStyleWord(true);
GPanelWithText GPanel = new GPanelWithText(text);
frame.getContentPane().add(GPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class CenteredFrame extends JFrame
{
public CenteredFrame()
{
//get screen dimensions
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
// center frame in screen
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
// set frame icon and title
Image img = kit.getImage("icon.gif");
setIconImage(img);
setTitle("CenteredFrame");
}
}
class GPanelWithText extends JScrollPane {
public GPanelWithText (JTextArea t) {
this.setViewportView(t);
this.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setPreferredSize(new Dimension(250,250));
this.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("My
Panel"),
BorderFactory.createEmptyBorder
(5,5,5,5)),
this.getBorder()));
} //end constructor
} //ends GPanelWithText