Below is the code where I am trying to draw some graphics on a JFrame. I am trying to install a JFrame layout that gives an error. But if I do not configure the layout, the code works fine, but it will not be what I want. I can’t understand what the problem is. Please help! =)
import java.awt.*;
import javax.swing.*;
class GuiForJFrame extends JFrame {
private FlowLayout layout;
private Container container;
public GuiForJFrame() {
super("Drawing Graphics");
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) {
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
jFrame.setSize(500, 200);
jFrame.setVisible(true);
}
}
source
share