当我使用WindowBuilder预览功能时,代码会按预期显示,但是当我按下"运行"

时间:2016-11-07 19:41:29

标签: java eclipse windowbuilder

当我使用WindowBuilder预览时,代码显示应该如此,但是当我运行程序时,它只会显示一个空白帧。它在预览中看起来很好,但是一旦我运行它,它就会显示一个空白帧。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;

public class Frame extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel teamSelect;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JFrame frame = new JFrame();
                frame.setForeground(Color.WHITE);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocation(new Point(100, 50));
                frame.setSize(1800, 900);
                frame.setTitle("Hockey");

                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Frame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    teamSelect = new JPanel();
    teamSelect.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(teamSelect);
    teamSelect.setLayout(new CardLayout(0, 0));

    JPanel divisionGrid = new JPanel();
    teamSelect.add(divisionGrid, "name_3885325667274");
    divisionGrid.setLayout(new GridLayout(2, 2, 0, 0));

    JPanel metroDivision = new JPanel();
    divisionGrid.add(metroDivision);
    metroDivision.setLayout(new BorderLayout(0, 0));

    JLabel lblMetropolitanDivision = new JLabel("Metropolitan Division");
    lblMetropolitanDivision.setHorizontalAlignment(SwingConstants.CENTER);
    lblMetropolitanDivision.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
    metroDivision.add(lblMetropolitanDivision, BorderLayout.NORTH);

    JPanel atlanticDivision = new JPanel();
    divisionGrid.add(atlanticDivision);
    atlanticDivision.setLayout(new BorderLayout(0, 0));

    JLabel lblAtlanticDivision = new JLabel("Atlantic Division");
    lblAtlanticDivision.setHorizontalAlignment(SwingConstants.CENTER);
    lblAtlanticDivision.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
    atlanticDivision.add(lblAtlanticDivision, BorderLayout.NORTH);

    JPanel centralDivision = new JPanel();
    divisionGrid.add(centralDivision);
    centralDivision.setLayout(new BorderLayout(0, 0));

    JLabel lblNewLabel = new JLabel("Central Division");
    lblNewLabel.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    centralDivision.add(lblNewLabel, BorderLayout.NORTH);

    JPanel pacificDivision = new JPanel();
    divisionGrid.add(pacificDivision);
    pacificDivision.setLayout(new BorderLayout(0, 0));

    JLabel lblNewLabel_1 = new JLabel("Pacific Division");
    lblNewLabel_1.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
    pacificDivision.add(lblNewLabel_1, BorderLayout.NORTH);
}

1 个答案:

答案 0 :(得分:-1)

替换

new JFrame();

通过

new Frame();

pack()之前致电setVisible(true)

请注意,只需添加System.out.println()或使用调试器来逐步执行代码就可以让您找到这个简单的错误。调试器是一个很棒的工具。用它。