Reputation: 236
Below is the code in which i'm trying to draw some graphics on JFrame. I'm trying to set the layout of the JFrame which is giving an error. But if I don't set the layout the code works fine but no the desired way. I can not figure out what is the problem. Please Help! =)
import java.awt.*;
import javax.swing.*;
class GuiForJFrame extends JFrame {
private FlowLayout layout;
private Container container;
public GuiForJFrame() {
super("Drawing Graphics");
// Setting the Layout
layout = new FlowLayout(FlowLayout.LEFT);
container = getContentPane();
setLayout(layout);
}
}
class GuiForDrawingGraphics extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.RED);
g.fillRect(25, 25, 150, 50);
g.setColor(new Color(156, 32, 111));
g.fillRect(25, 80, 150, 50);
g.setColor(Color.BLACK);
g.drawString("Drawing Graphics in JAVA", 25, 150);
}
}
public class Application {
public static void main(String[] args) {
// Creating the JFrame object
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding Graphics to JFrame
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
jFrame.setSize(500, 200);
jFrame.setVisible(true);
}
}
Upvotes: 4
Views: 83
Reputation: 168825
As mentioned in another answer, the custom drawn panel has no inherent size. It should return a preferred size suitable for the content. Then we just need to pack()
the frame after it is added.
import java.awt.*;
import javax.swing.*;
class GuiForJFrame extends JFrame {
private FlowLayout layout;
private Container container;
public GuiForJFrame() {
super("Drawing Graphics");
// Setting the Layout
layout = new FlowLayout(FlowLayout.LEFT);
container = getContentPane();
setLayout(layout);
}
public static void main(String[] args) {
// Creating the JFrame object
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding Graphics to JFrame
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
//jFrame.setSize(500, 200);
jFrame.pack();
jFrame.setVisible(true);
}
}
class GuiForDrawingGraphics extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.RED);
g.fillRect(25, 25, 150, 50);
g.setColor(new Color(156, 32, 111));
g.fillRect(25, 80, 150, 50);
g.setColor(Color.BLACK);
g.drawString("Drawing Graphics in JAVA", 25, 150);
}
public Dimension getPreferredSize() {
return new Dimension(300,200);
}
}
Upvotes: 2
Reputation: 4340
JFrame by default use BorderLayout, so when you not specify any layout it work because your panel will be added to the center of the BorderLayout.
but when you specify FlowLayout which I'm not adviced you to do because it will arrange all components on row and then exapend to multiple rows when needed. and the other thing FlowLayout cann't get your preferredSize, and you need to provide this size on your custom panel.
A flow layout lets each component assume its natural (preferred) size.
http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html
so either you specify your panel size, for example in your paintComponent of the panel:
setSize(200, 100); // values depend on your components
or use any other layout like the default one (BorderLayout) or other types like BoxLayout, it depend on what you want the frame to be look like
see this for more layouts types: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
Upvotes: 1
Reputation: 150
Add jFrame.pack();
and jFrame.validate();
. This sets all child components (i.e. components inside jFrame) as well as jFrame itself to their appropriate size and makes sure they can even be displayed and that no Exception
s will be thrown.
If you don't do either of these, nothing will (and nothing should) show up.
So, your code for the Application
class should look like:
public class Application {
public static void main(String[] args) {
// Creating the JFrame object
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding Graphics to JFrame
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
jFrame.setSize(500, 200);
// Displaying the JFrame properly.
jFrame.pack();
jFrame.validate();
jFrame.setVisible(true);
}
}
In addition to this, you'll have to use the setPreferredSize(Dimension dimension)
method to specify the size that you want each JComponent
to be.
Check out the JComponent
and JFrame
classes at http://docs.oracle.com/javase/7/docs/api/ for more information.
Upvotes: 1