1

I am trying to make a simple text adventure game in Java (with a GUI) by following this tutorial: https://www.youtube.com/watchv=RcvABhflOkI&list=PL_QPQmz5C6WUMB0xEMZosWbyQo_Kil0Fb&index=2

This is my code thus far (for reference):

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Game {
    JFrame window;
    JPanel titleNamePanel, startButtonPanel;
    JLabel titleNameLabel, startButtonLabel;
    JButton startButton;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
    Font normalFont = new Font("Times New Roman", Font.PLAIN, 30);
    
    public static void main(String[] args) {
        new Game();
    }
    
    public Game() {

        window = new JFrame();
        window.setSize(900,600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(Color.black);
        window.setLayout(null);

        titleNamePanel = new JPanel();
        titleNamePanel.setBounds(100,100,700,150);
        titleNamePanel.setBackground(Color.black);
        titleNameLabel = new JLabel("Town of Stratham");
        titleNameLabel.setForeground(Color.white);
        titleNameLabel.setFont(titleFont);
        
        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(400,400,100,50);
        startButtonPanel.setBackground(Color.black);
        
        startButton = new JButton("START");
        startButton.setBackground(Color.black);
        startButton.setForeground(Color.white);
        startButton.setFont(normalFont);
        
        titleNamePanel.add(titleNameLabel);
        startButtonPanel.add(startButton);
        
        window.add(titleNamePanel);
        window.add(startButtonPanel);
        
        window.setVisible(true);
        
    }

}

However, I come across some issues when I tried to make a Jbutton. I have a MacBook air and when I run the program above my button doesn't even appear. I want my button to look black and the text to be white like in the tutorial video (timestep- 22:10). I asked one of my friends who had windows to run the code and it looks exactly like what I want it to look like.

It runs this on my Max OS: Button not showing up:

https://i.stack.imgur.com/LqT5t.png

This is what runs on my friend's window OS (exact same code): Button shows up fine:

https://i.stack.imgur.com/FXHxX.png

So my question is how can I fix this? Is there a fix for this & why does this happen?

Mostafa Ghorbani
  • 397
  • 1
  • 3
  • 15
  • 1
    This information might be relevant to your case: https://stackoverflow.com/questions/1065691/how-to-set-the-background-color-of-a-jbutton-on-the-mac-os – Tim Hunter Dec 17 '20 at 15:51
  • Looks to me like the button does show up but maybe the background of the button isn't correct. Try changing the foreground color to something other than white. – camickr Dec 17 '20 at 15:51
  • Not too familiar with Swing, but it looks like it makes some assumptions on look and feel depending on the OS preferences. You're probably running into this issue and need to set it to use a cross-platform look and feel, via the information in the link I gave above. – Tim Hunter Dec 17 '20 at 15:55
  • Thank you, everyone! I'll try all of your suggestions :) – Hasin Raihan Dec 17 '20 at 16:30
  • 1
    1) `window.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Dec 17 '20 at 20:30

0 Answers0