I have been making a “Cookie Clicker” sort of game, where you click cake and it gets you cakes. However, this program has been having some bugs, like when I make the game class, the text variable doesn’t show up. Can anyone help?
public class Game extends JPanel {
private double numClicks = 0;
public JLabel text;
public JLabel hoverText;
ImageIcon cakeImage = new ImageIcon("cake image location");
public JButton cakeButton;
public JPanel counterPanel;
public Game(Window frame //my jframe (look below)) {
counterPanel = new JPanel();
counterPanel.setBounds(700, 100, 400, 200);
counterPanel.setLayout(new BorderLayout());
counterPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 10));
//make it easier to move the parts around
setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(1000, 750));
cakeButton = new JButton(cakeImage);
cakeButton.setBounds(100, 100,300,300);
cakeButton.setBackground(Color.decode("#FFFFFF"));
cakeButton.setBorder(BorderFactory.createLineBorder(Color.black, 5));
cakeButton.setFocusable(false);
cakeButton.setOpaque(false);
cakeButton.setContentAreaFilled(false);
//cakeButton.setBorderPainted(false);
cakeButton.setVisible(true);
add(cakeButton);
//make the cake counter
text = new JLabel("Cakes: 0 ");
text.setVerticalAlignment(SwingConstants.TOP);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setFont(new Font("Courier", Font.BOLD,75));
text.setText("Cakes: 0 ");
text.setSize(text.getPreferredSize());
//text.setLocation(500/*50 + (frameLength * 1/2)*/, 100);
counterPanel.add(text);
text.setVisible(true);
//hover text for the cake button
hoverText = new JLabel("Cakes: 0 ");
hoverText.setVerticalAlignment(SwingConstants.BOTTOM);
hoverText.setHorizontalAlignment(SwingConstants.CENTER);
hoverText.setFont(new Font("Courier", Font.BOLD,75));
hoverText.setText("Cakes: 0 ");
hoverText.setSize(hoverText.getPreferredSize());
counterPanel.add(hoverText);
hoverText.setVisible(false);
frame.add(counterPanel);
//add the action listener to the cake button
cakeButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
numClicks++;
int convClicks = (int) Math.round(numClicks);
String texty = "Cakes: " + convClicks + " ";
text.setText(texty);
}
}
);
text.addMouseListener( new MouseListener()
{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
hoverText.setVisible(true);
hoverText.setText("Text");
System.out.println("hi");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
);
}
}
Here is my Jframe:
public class Window extends JFrame implements KeyListener, WindowListener, MouseListener {
JFrame frame;
JLabel textLabel;
Game game;
MainMenu mainMenu;
public Window(String title) {
setTitle(title);
//Create and set up the window.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addKeyListener(this);
addMouseListener(this);
//Display the window.
setSize(1000, 750);
setLocationRelativeTo(null);
pack();
//frame.setLayout(null);
setVisible(true);
mainMenu = new MainMenu(this);
add(mainMenu);
setSize(1000, 750);
//game = new Game(this);
}
public Game mainMessage(int type) {
if (type == 0) {
mainMenu.removeMenu();
game = new Game(this);
add(game);
game.setVisible(true);
game.text.setVisible(true);
SwingUtilities.updateComponentTreeUI(this);
return game;
}
return null;
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
SwingUtilities.updateComponentTreeUI(this);
Main.receiveKey(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
main.Main.receiveMouse(e);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
I tried making a new panel and using that, but it didn’t work and just glitched out
New contributor
thing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.