Hi, I'm trying to do a java project, in which I try to create some buttons on top of an image, using a paintComponent on a JPanel, but when I add the buttons to the JPanel and run the program, the buttons do not appear, (in a first time) but when re-dimensioning the window, if they appear, but for some reason every time you re-size the window, the number of buttons is doubled, as if in some way, there was a WindowsListener that every time you -Dimension the window, duplicate the frames of the JPanel, my question is, how do I prevent the buttons from duplicating, and that my buttons appear at the moment when the JPanel is opened? here I leave the code of my program.
public class Numero_1 {
public static void main(String[] args) {
ventana Ventana = new ventana("ffff");
Ventana.setVisible(true);
Ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ventana extends JFrame {
private static final long serialVersionUID = 1L;
ventana(String clase) {
super();
Toolkit ventana = Toolkit.getDefaultToolkit();
Dimension miventana = ventana.getScreenSize();
int altura = miventana.height;
int anchura = miventana.width;
if (clase == "encriptador") {
setBounds(anchura / 3, altura / 4, 1000, 667);
encriptador Encriptador = new encriptador();
add(Encriptador);
} else if (clase == "desencriptador") {
setBounds(anchura / 3, altura / 4, 1000, 600);
desencriptador Desencriptador = new desencriptador();
add(Desencriptador);
} else {
setBounds(anchura / 5, altura / 7, 1010, 580);
inicio Inicio = new inicio();
add(Inicio);
}
}
}
class inicio extends JPanel {
private static final long serialVersionUID = 1L;
private Image fondo;
private JButton encriptador;
private JButton desencriptador;
public void paintComponent(Graphics g) {
super.paintComponent(g);
File direccion = new File("lock_tech1010,580.jpg");
try {
BufferedImage boton = ImageIO.read(new
File("botonvacioover350x100.png"));
fondo = ImageIO.read(direccion);
encriptador = new JButton("encriptador");
desencriptador = new JButton("desencriptador");
} catch (IOException e) {
e.printStackTrace();
}
setLayout(new GridLayout(1, 2));
JPanel pantalla_izquierda = new JPanel();
JPanel pantalla_derecha3 = new JPanel();
JPanel pantalla_derecha3_1 = new JPanel();
JPanel pantalla_derecha3_2 = new JPanel();
JPanel pantalla_derecha3_3 = new JPanel();
pantalla_derecha3_2.setLayout(new FlowLayout());
pantalla_derecha3_3.setLayout(new FlowLayout());
pantalla_derecha3.setLayout(new GridLayout(3, 1));
add(pantalla_izquierda);
pantalla_derecha3.add(pantalla_derecha3_1);
pantalla_derecha3.add(pantalla_derecha3_2);
pantalla_derecha3.add(pantalla_derecha3_3);
pantalla_derecha3_2.add(encriptador);
pantalla_derecha3_3.add(desencriptador);
pantalla_izquierda.setOpaque(false);
pantalla_derecha3.setOpaque(false);
pantalla_derecha3_1.setOpaque(false);
pantalla_derecha3_2.setOpaque(false);
pantalla_derecha3_3.setOpaque(false);
g.drawImage(fondo, 0, 0, 1010, 580, null);
add(pantalla_derecha3);
}