I am trying to create a Frame that has a graphics panel on the top portion (through a Layout) and a buttons / labels panel underneath it in the same frame. So far I seemed to have been able to get both of them onto the same frame but the Graphics panel is very small compared to the buttons / labels panel... I cant post photos but its almost as if the size was (400,10) for the Graphics panel and (400,290) for the Buttons / Labels panel.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DoNotEnterSign extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(0,0,50,50);
}}
public static void main(String args[])
{
JFrame frame2 = new JFrame();
JPanel panel = new DoNotEnterSign();
panel.setBackground(Color.GRAY);
panel.setSize(100,100);
JPanel panel2 = new JPanel();
JButton test = new JButton("Testing");
panel2.add(test);
frame2.getContentPane().add(panel, BorderLayout.NORTH);
frame2.getContentPane().add(panel2, BorderLayout.SOUTH);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(400,300);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
Just override
getPreferedSize()
method of yourDoNotEnterSign
class instead of usingsetSize(100,100);
. Because according to docs it works only without layout manager:Add next to your
DoNotEnterSign
: