我制作了一个数独游戏程序,但我有一个问题我无法解决。我总共有4个按钮但只有一个按钮出现,除非我将鼠标悬停在其他按钮上。我知道这与setLayout选项有关,但我不知道如何修复它而不会弄乱我的显示。我通常不会要求一个完整的解决方案,但如果有人能给我一个大手,那么我将非常感谢它。对不起,代码很大......我在这个项目的最后,我真的迷失了这个最后的问题。这是按钮发生的主要部分:
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
}
}