so I'm just learning Java GUI (JFrame
, JPanel
etc) and I ran into this issue where when I repaint my panel and then continue to add buttons to the same panel, the buttons sort of stutter and only half appear when I hover my mouse over the button.
I will split my code up into the different classes below so hopefully someone with more experience may be able to spot what the issue is.
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
new Test();
}
}
Above is the first class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Test extends JFrame
{
private menuPanel MENUPANEL;
public Test()
{
super("My Game");
setLayout(new BorderLayout());
MENUPANEL = new menuPanel();
add(MENUPANEL, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(660,360);
setResizable(false);
setVisible(true);
}
}
Here is my class which creates the initial frame.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class menuPanel extends JPanel
{
private JButton btn1, btn2, btn3;
private JPanel menuPanel;
private ImageIcon mypic;
public menuPanel()
{
JButton btn1 = new JButton("START GAME");
btn2 = new JButton("EXIT GAME");
btn3 = new JButton("OPTIONS");
setLayout(new FlowLayout());
add(btn2);
add(btn1);
add(btn3);
}
@Override
public void paint (Graphics g)
{
super.paintComponents(g);
ImageIcon i = new ImageIcon("background.gif");
i.paintIcon(this, g, 0,0);
}
}
Finally here is my panel class.
Please excuse me if I've used any wrong terminology etc. I've been searching for a fix for this problem for a few hours now and only have a handful of hours experience with Java GUI.