Java Gui内容不会出现

时间:2017-08-19 18:24:09

标签: java user-interface

我有这个代码,但是当我运行它时,只显示黑色窗口背景,但其他一切都没有,

这是我运行时出现的内容。:

This is what appears when I run it.

当我运行它时,我的IDE也在控制台中给我这条消息,但这些行没有任何问题:

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);
    }
}

0 个答案:

没有答案