I am learning java swing programming and have a problem.
I want to create simple Bouncing ball application using threading and swing..
So, I created a JFrame and placed 2 buttons, start and stop, and placed a simple ball using fillOval().
Here is the code...
package bouncingball;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Ball extends JPanel
{
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.red);
g.fillOval(20, 20, 100, 100);
}
}
public class BouncingBall {
JFrame f;
JButton start, stop;
BouncingBall()
{
f = new JFrame("BouncingBall");
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700, 700);
f.getContentPane().add(new Ball());
start = new JButton("START");
start.setBounds(100, 450, 150, 20);
f.getContentPane().add(start);
stop = new JButton("STOP");
stop.setBounds(100, 500, 150, 20);
f.getContentPane().add(stop);
f.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
BouncingBall b = new BouncingBall();
}
}
I set the layout to null. On running this code, buttons are visible, but ball is not.
I know that in layout, we can add only one thing to each region. But, since setLayout(null), what is wrong with it?