1

I've made a Sudoku program but I have 1 issue I can't fix. I have 4 buttons overall but only one appears unless I hover over the others. I know this has something to do with the setLayout option but I don't know how to fix it without making a mess of my display. I don't usually ask for a full solution but if someone can give me a huge hand then I'll be very thankful for it. Sorry that the code is huge... I'm at the end of this project and I'm really lost with this last problem. Here is the main part where the buttons happen:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class Test extends JFrame implements ActionListener {

public static JPanel Container1;
public static JPanel Container2;
public static JPanel containermain;
public static JLabel FOND_ECRAN;
public static JButton verify = new JButton("Verify");
public static JButton solve = new JButton("Solve");
public static JButton reset = new JButton("Reset");
public static JButton submit = new JButton("Submit");
public static JTextField[][] cases = new JTextField[9][9];
public static int[][] NOMBRES_DEBUT;

public static void main(String[] args) {
    Test t = new Test();
}

public Test(){
    NOMBRES_DEBUT = new int[9][9];
    //Permet d'avoir une fenêtre en plein-écran
    //this.setUndecorated(true); 
    this.setAlwaysOnTop(true);
    this.setResizable(false);

    Toolkit tk = Toolkit.getDefaultToolkit();
    int xsize = (int) tk.getScreenSize().getWidth();
    int ysize = (int) tk.getScreenSize().getHeight();

    this.setSize(xsize, ysize);

    Container1 = new JPanel();  //Initialisation de la grille de Sudoku en JTextField
    Container1.setLayout(null);
    Container1.setBounds((this.getWidth()/2)-(9*60/2),(this.getHeight()/2)-(9*60/2),9*60,9*60);
    Container1.setBackground(Color.white);

    for (int li = 0; li < 9; li++) {    //Rempli les JTextField avec les nombres dans NOMBRES_DEBUT
        for (int col = 0; col < 9; col++){
            //Things here
        }
    }

    // Création des 4 boutons
    int xButton = 170;
    int yButton = (int) ((xButton*3)/4);

    verify.setBounds((xsize/4)-(xButton/2), (ysize/4)-(yButton/2), xButton, 3*yButton/4);
    verify.addActionListener(this);
    verify.setBackground(new Color(50, 150, 251));
    verify.setOpaque(true);
    verify.setBorderPainted(true);

    solve.setBounds((xsize/4)-(xButton/2), (ysize/2)-(yButton/2), xButton, 3*yButton/4);
    solve.addActionListener(this);
    solve.setBackground(new Color(50, 150, 251));
    solve.setOpaque(true);
    solve.setBorderPainted(true);

    reset.setBounds((xsize/4)-(xButton/2), (3*ysize/4)-(yButton/2), xButton, 3*yButton/4);
    reset.addActionListener(this);
    reset.setBackground(new Color(50, 150, 251));
    reset.setOpaque(true);
    reset.setBorderPainted(true);

    submit.setBounds((xsize/2)-(xButton/2), (7*ysize/8)-(yButton/2), xButton, 3*yButton/4);
    submit.addActionListener(this);
    submit.setBackground(new Color(50, 150, 251));
    submit.setOpaque(true);
    submit.setBorderPainted(true);

    containermain = new JPanel();
    containermain.setBounds(0,0, xsize, ysize);
    containermain.setOpaque(false);
    containermain.setLayout(null);

    // Crée et ajoute le fond d'écran au JLabel
    FOND_ECRAN = new JLabel();
    FOND_ECRAN.setBounds(0, 0, 1920, 1080);
    containermain.add(FOND_ECRAN);
    containermain.add(verify);
    containermain.add(solve);
    containermain.add(reset);
    containermain.add(submit);

    // Récupère l'image de l'ordinateur
    ImageIcon temp = new ImageIcon("C:\\Users\\Maxime\\Documents\\Perso\\Post-Bac\\INSA\\Projet Info\\nature-1520704451453-7117.jpg");
    Image img = temp.getImage();
    ImageIcon imageIcon = new ImageIcon(img);
    FOND_ECRAN.setIcon(imageIcon); // Donne l'image comme fond d'écran

    setContentPane(containermain);

    containermain.add(Container1);

    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
    // Things happen

}

}

alexrnov
  • 2,346
  • 3
  • 18
  • 34
Max Michel
  • 575
  • 5
  • 20
  • 1
    1. Simplify the code to one that *we* can compile and run without modification, a [mcve]. 2. Always better to use appropriate layout managers and not use null layouts and `setBounds(...)`. 3. You're not overriding `paint` or some other painting method somewhere? – DontKnowMuchBut Getting Better Apr 01 '18 at 17:14
  • No, I'm not using paint at all, the only thing I'm using is JFrame. i'll try to modify the code snippet a bit – Max Michel Apr 01 '18 at 17:15
  • 2
    Your code should only contain that which is needed for it to compile and run for us and demonstrate your problem and nothing more. It should be a complete self-contained class with imports, etc... – DontKnowMuchBut Getting Better Apr 01 '18 at 17:19
  • @DontKnowMuchButGettingBetter I hope the changes help you, sorry about the bulky code, still getting used to the site... – Max Michel Apr 01 '18 at 17:20
  • I still cannot copy and paste it into my IDE and have it compile and run unmodified. Please keep at it. – DontKnowMuchBut Getting Better Apr 01 '18 at 17:22
  • 2
    And if you're making a Sudoku grid why not use a JPanel that uses a simple GridLayout? You look to be making things unnecessarily complicated for yourself. – DontKnowMuchBut Getting Better Apr 01 '18 at 17:22
  • @DontKnowMuchButGettingBetter This should now work and the problem seems to be the JLabel FOND_ECRAN (you can probably use any image in your computer to see what happens) – Max Michel Apr 01 '18 at 17:39
  • I suggest you create a new file and write just enough code to recreate the problem you are asking about. Your current code example appears to have lots of stuff that is irrelevant to your quesiton. – Code-Apprentice Apr 01 '18 at 17:40
  • @Code-Apprentice This is the bare minimum I could take out and still have my problem appear, if I take other parts out, I have no more problem but that's not what I need – Max Michel Apr 01 '18 at 17:43
  • That's not how you create a background image for your GUI and there's your problem as the JLabel can cover over your components. Instead look at [this duplicate](https://stackoverflow.com/questions/18777893/jframe-background-image) and other similar ones. – Hovercraft Full Of Eels Apr 01 '18 at 17:45
  • I manage to find the problem thanks to you two @DontKnowMuchButGettingBetter, the order of operations in java is so important ! – Max Michel Apr 01 '18 at 17:46
  • @HovercraftFullOfEels Thanks, I'll check that out, can't be worse than my way ! – Max Michel Apr 01 '18 at 17:47
  • It's not just order. You need to either use a JLabel as the contentPane and add things to it, or you need to use a JPanel as your contentPane, override its paintComponent method and draw the image within it. Again this has all been well-described in [many similar questions](https://www.google.com/search?&q=site%3Ahttps%3A%2F%2Fstackoverflow.com%2F+java+background+image+for+jframe) – Hovercraft Full Of Eels Apr 01 '18 at 17:47
  • And I agree with the other comments about layouts. While null layouts and `setBounds()` might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. – Hovercraft Full Of Eels Apr 01 '18 at 17:48
  • @MaxMichel Do you really need 4 JButton's to illustrate the problem? Isn't one button sufficient? Why do you need 3 JPanel's to duplicate the problem? I see lots of code that still seems unnecessary to ask your question. – Code-Apprentice Apr 01 '18 at 18:53

0 Answers0