'Java Graphics, Graphics panel and Buttons Panel
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);
Solution 1:[1]
Just override getPreferedSize()
method of your DoNotEnterSign
class instead of using setSize(100,100);
. Because according to docs it works only without layout manager:
Set the size of the component measured in pixels. The two int arguments specify width and height, in that order. Use these methods to size a component when you are not using a layout manager.
Add next to your DoNotEnterSign
:
@Override
public Dimension getPreferredSize() {
return new Dimension(100,100);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | alex2410 |