import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mywindow {
public static void main(String[] args) {
CenteredFrame frame = new CenteredFrame();
GPanelWithText GPanel = new GPanelWithText();
frame.getContentPane().add(GPanel, BorderLayout.CENTER);
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 JTextArea {
public GPanelWithText () {
super("This is a test",5,30);
setLineWrap(true);
setWrapStyleWord(true);
this.setPreferredSize(new Dimension(250,250));
setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("My
Panel"),BorderFactory.createEmptyBorder(5,5,5,5)),this.getBorder()));
} //end constructor
} //ends GPanelWithText