import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Color; import java.awt.Graphics; public class GraphicalObjectsWithPainting extends JFrame { private static JButton button1; public static void main(String[] args) { new GraphicalObjectsWithPainting(); } // Constructor public GraphicalObjectsWithPainting() { // Set up the JFrame; note that it is NOT necessary to explicitly // create a 'JFrame' object here, since extending the 'JFrame' // class does that automatically setLayout(null); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); setTitle("Combining Graphical Objects & Painting on a JFrame"); getContentPane().setBackground(Color.LIGHT_GRAY); setLocationRelativeTo(null); // Create and set up a button; note that other graphical objects // can always be created here as well button1 = new JButton(); button1.setBounds(40, 90, 315, 41); button1.setText("I am a JButton. Above is an oval. Below is a line."); button1.setFocusable(false); button1.setVisible(true); // Add the JButton to the JFrame, and then make the JFrame (and // everything on it) visible add(button1); setVisible(true); } public void paint(Graphics g) { // This line is necessary to clear/refresh the background super.paintComponents(g); // Desired shapes/lines can be drawn here g.setColor(Color.RED); g.fillOval(175, 36, 50, 50); g.setColor(Color.BLUE); g.drawLine(20, 180, 380, 180); } }