0

I have this code but when I run it only the black window background appears, but everything else does not,

This is what appears when I run it.:

This is what appears when I run it.

My IDE also gives me this message in the console when i run it but there is nothing wrong with those lines:

My IDE also gives me this message in the console when i run it but there is nothing wrong with those lines

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Main{
    JFrame window;
    Container container;
    JPanel titleNamePanel;
    JPanel startButtonPanel;
    JLabel titleNameLabel;
    Font titleFont = new Font("Times New Roman", Font.BOLD, 90);
    Font normalFont = new Font("Times New Roman", Font.PLAIN, 40);
    JButton startButton;

    public static void main(String [] args){
        //Variables
        Scanner myScanner = new Scanner(System.in);

        new Main();

    }
    public Main(){
        window = new JFrame();
        window.setSize(1500, 1500);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(Color.BLACK);
        window.setLayout(null);
        window.setVisible(true);
        window.setResizable(true);
        container = window.getContentPane();

        titleNamePanel = new JPanel();
        titleNamePanel.setBounds(100, 100, 1000, 150);
        titleNamePanel.setBackground(Color.BLACK);
        titleNameLabel = new JLabel("ADVENTURE CAVE");
        titleNameLabel.setForeground(Color.white);
        titleNameLabel.setFont(titleFont);


        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(500, 400, 200, 100);
        startButtonPanel.setBackground(Color.BLUE);
        startButton.setFont(normalFont);

        startButton = new JButton("START");
        startButton.setBackground(Color.BLACK);
        startButton.setForeground(Color.RED);

        container.add(titleNamePanel);
        titleNamePanel.add(titleNameLabel);
        container.add(startButtonPanel);
        startButtonPanel.add(startButton);
    }
}
curious
  • 1,504
  • 5
  • 18
  • 32
Koji Ohara
  • 11
  • 5
  • 2
    Call `setVisible(true)` on the JFrame **after** you've added all components to the JFrame. Yours is a common problem. – DontKnowMuchBut Getting Better Aug 19 '17 at 18:25
  • 1
    Also a side issue, you want to avoid `window.setLayout(null);` and `setBounds(...)` as it leads to creation of very inflexible GUI's that may look OK on one platform but usually look terrible on all others. – DontKnowMuchBut Getting Better Aug 19 '17 at 18:28
  • 1
    You are calling `startButton.setFont(normalFont);` but you are initializing `startButton` line below. Before that `startButton` is `null` which is causing NPE. – Pshemo Aug 19 '17 at 18:28
  • Oops,.... I missed that! Better read up on [NullPointerExceptions](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) because if you continue coding, you **will** run into these again, and again, and again,... – DontKnowMuchBut Getting Better Aug 19 '17 at 18:36
  • 1
    And the real answer : getting swing uis correct is complicated. You easily get things wrong. So trial and error is not an effective approach. Read and follow tutorials instead of hoping that self written code will work. It will not. – GhostCat Aug 19 '17 at 18:36
  • Also, you don't really want to mix a `new Scanner(System.in)` with a Swing GUI as you risk freezing the GUI if you try to get input from the Scanner, and also it's a somewhat schizophrenic way to create a user interface, one that is neither fish nor foul. – Hovercraft Full Of Eels Aug 19 '17 at 18:49

0 Answers0