Warum "schließt" sich das Fenster direkt wieder?

Immer, wenn ich die frame.pack() Methode verwende ist das Fenster nach den Ausführen sehr komisch, warum?

package com.company;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.Border;

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame(); // creates a frame

        JLabel label = new JLabel(); // creates a label
        Border border = BorderFactory.createLineBorder(Color.green);

        label.setText("Hello World"); // sets text of label

        label.setHorizontalTextPosition(JLabel.CENTER); //sets text left,center or right of image icon

        label.setVerticalTextPosition(JLabel.TOP); //sets text top,center or bottom of image icon

        label.setForeground(Color.WHITE); //sets color og label

        label.setFont(new Font("serif",Font.PLAIN,20)); // sets font of label

        frame.setTitle("Title"); // sets title of the frame

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exits and not hides the frame when close

        frame.setResizable(true);// can't resize the frame

        frame.setSize(420,420); //sets the x an y dimension of the frame

        frame.setVisible(true); //make frame visible

        label.setIconTextGap(100); //adds distance between label and icon

        label.setBackground(Color.black); //sets background-color of label

        label.setOpaque(true); //adds the background color !!!

        label.setBorder(border); //sets Border of label

        label.setVerticalAlignment(JLabel.CENTER); // Vertical Aligment of label

        label.setHorizontalAlignment(JLabel.CENTER); // Horizontal Aligment of label

        frame.add(label); //adds label !!!

        ImageIcon image = new ImageIcon("logo.jpg"); //creates image icon

        label.setIcon(image); // sets icon of label

        frame.setIconImage(image.getImage()); // change icon of frame

        label.setBounds(100, 100, 250, 250); //sets x,y positon within frame well as dimensions

        frame.setLayout(null);

        frame.getContentPane().setBackground(new Color(123,50,250)); // change color of background

        frame.pack();



    }
}

Computer, Programmieren, Java

Was möchtest Du wissen?