public MyLayout (){
frame.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
panel.add(new GraphicsSurface(),BorderLayout.CENTER);
frame.add(panel,BorderLayout.NORTH);
frame.add(btn1,BorderLayout.SOUTH);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
I'm creating a graphic placing it inside a panel
panel.add(new GraphicsSurface(),BorderLayout.CENTER);
and placing this panel inside a JFrame
frame.add(panel,BorderLayout.NORTH);
Except the graphic is displaying outside the JFrame
In
GraphicsSurface
overridegetPreferredSize
and give it the size you want (I guess the size of the clockFor
panel
, don't set the layout. The defaultFlowLayout
will work perfectly (it also centers by default -yeeee!). What will happen is that theGraphicsSurface
will maintain its preferred size, asFlowLayout
will respect it. Then thepanel
will be stretched to the width of the frame (the height will remain theGraphicsSurface
's height), and theGraphicsSurface
will be centered in thepanel
With this you should be fine, adding the
panel
to the frame'sNORTH
. The center will be left for everything else.