Я знаю, что подобные вопросы задавались, но у всех разные коды, и решения их ошибок на самом деле не помогают мне найти решение.
//Libraries
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SliderGame implements ActionListener {
JLabel referenceLabel1;
JLabel instruc1;
JLabel preGame1;
JButton gotIt1;
JButton addPicture1;
JPanel instructionsPanel1;
JPanel gameboardFoundation1;
public SliderGame(){
//Image that shows user what their objective is
referenceLabel1 = new JLabel(new ImageIcon("Images/SliderEasy.jpg"));
referenceLabel1.setBounds(100,150,300,300);
referenceLabel1.setVisible(true);
easyBackground.add(referenceLabel1);
//"your objective" pregame = before the game starts
preGame1 = new JLabel("Your objective:");
preGame1.setBounds(500,150,300,100);
preGame1.setFont(new Font ("Courier New", Font.ITALIC, 28));
preGame1.setVisible(true);
easyBackground.add(preGame1);
//Tells you the instructions
instruc1 = new JLabel ("<html>Use the mouse & click to move piece into
the blank spot to get the numbers in order, as shown on the left -- As
fast as you can!<html>");
instruc1.setBounds(500, 190, 300, 200);
instruc1.setFont(new Font ("Courier New", Font.PLAIN, 20));
instruc1.setVisible(true);
easyBackground.add(instruc1);
//Press to play
gotIt1 = new JButton ("Got it!");
gotIt1.setBounds(500, 375, 100, 50);
gotIt1.setFont(new Font ("Courier New", Font.PLAIN, 12));
gotIt1.setVisible(true);
easyBackground.add(gotIt1);
gotIt1.addActionListener(this);
//Press to choose images from a library
addPicture1 = new JButton ("Use Picture");
addPicture1.setBounds(615, 375, 135, 50);
addPicture1.setFont(new Font ("Courier New", Font.PLAIN, 12));
addPicture1.setVisible(true);
easyBackground.add(addPicture1);
addPicture1.addActionListener(this);
//Panel that holds the gameboard
gameboardFoundation1 = new JPanel();
gameboardFoundation1.setLayout(new GridLayout(3,3,3,3));
gameboardFoundation1.setBounds(100, 150, 300, 300);
gameboardFoundation1.setOpaque(false);
gameboardFoundation1.setVisible(false);
easyBackground.add(gameboardFoundation1);
}
public void actionPerformed (ActionEvent e) {
if(e.getSource().equals(gotIt1)) {
preGame1.setVisible(false);
instruc1.setVisible(false);
gotIt1.setVisible(false);
addPicture1.setVisible(false);
referenceLabel1.setVisible(false);
gameboardFoundation1.setVisible(true);
}
}
Я не включил весь свой код, так что случаи, когда он говорит mainFrame.add (), например, mainFrame, существует, просто я не добавил его к вопросу.Код работает до тех пор, пока я не нажму на кнопку JButton gotIt1, и ничего не изменится.Это странно, потому что я отформатировал все остальные кнопки, которые я использовал в своей программе, и они отлично работают.
Я начинающий программист, особенно в GUI.Я знаю, что мой код, вероятно, не самый чистый, но это не главное.Почему моя кнопка не работает!?
Заранее спасибо.