I have a game program and I want to add a title to my main menu. I added the graphics and added a string of text to be displayed but it won't show up. I added a comment were the problem may be.
My main code:
package Main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame("Tennis Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Menu graphics = new Menu();
frame.add(graphics);
frame.setLayout(null);
final JButton b = new JButton("Play");
b.setFocusPainted(false);
b.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
b.setBounds(110, 100, 80, 40);
b.setForeground(Color.BLACK);
b.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseEntered(MouseEvent evt) {
b.setForeground(Color.RED);
}
public void mouseExited(MouseEvent evt) {
b.setForeground(Color.BLACK);
}
});
frame.add(b);
frame.setSize(300,400);
frame.setVisible(true);
}
}
My menu code with the title:
package Main;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import javax.swing.*;
public class Menu extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.yellow);
g.setFont(new Font("Arial", Font.BOLD, 30));
g.setColor(Color.BLACK);
g.drawString("TENNIS GAME", 40, 60);
}
}