netbeans不运行javaswing应用程序

时间:2015-11-22 10:01:18

标签: java netbeans netbeans-8.1

JavaSwing应用程序在netbeans中没有显示错误。当我尝试运行它时,它需要永远,没有任何事情发生(JDK 8 + Netbeans 8.1)。日食中的同样问题。一个简单的hello world程序可行。该课程分为4个班级。抱歉,长代码。

package test2;

import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class MainWindow extends JFrame {

private Navigator navigator = new Navigator(this);
private Field spielfeld = new Field();

public MainWindow() {

    super("GameTest");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.add(spielfeld);
    this.setResizable(false);
    this.pack();

}

public static void main(String[] args) {
    new MainWindow();
}
}


package test2;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

public class Field extends JPanel {

private static int x = 10;
private static int y = 10;
private JLabel[][] fields = new JLabel[10][10];

public Field() {
    Dimension dim = new Dimension();
    dim.setSize(50, 50);

    this.setLayout(new GridLayout(x, y));
    for(int i = 0; i < y; i++){
        for(int j = 0; j < x; j++){
            fields[i][j] = new JLabel();
            fields[i][j].setPreferredSize(dim);
            fields[i][j].setOpaque(true);
            if((i+j)%2 == 0){
                fields[i][j].setBackground(Color.BLACK);
            }
            else {
                fields[i][j].setBackground(Color.WHITE);
            }
            this.add(fields[i][j]);
        }
    }
}
}


package test2;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;

public class Navigator extends JWindow {

public Navigator(JFrame parent) {
    super(parent);

    JPanel frame = new JPanel();
    frame.setLayout(new BorderLayout());
    frame.setBorder(BorderFactory.createLineBorder(Color.RED));
    frame.add(new Keypad(), BorderLayout.NORTH);

    this.getContentPane().add(frame);
            this.updateLocation();
            this.pack();
}

public void updateLocation()
{
        this.setLocation((int)this.getParent().getLocation().getX() + this.getParent().getWidth() + 550, 
                                 (int)this.getParent().getLocation().getY());
}

public MainWindow getMainWindow()
{
        return (MainWindow)this.getParent();
}
}


package test2;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Keypad extends JPanel {

public Keypad() {
    Dimension dim = new Dimension(60, 30);
    this.setLayout(new GridLayout(3, 3));
    this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    for(int i = 9; 0 < i; i--) {
        JButton button = new JButton(String.valueOf(i));
        button.setPreferredSize(dim);
        button.setVisible(true);
        if(i != 5) {

            this.add(button);


            this.add(button);
        }
    }
    this.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:0)

您在setVisible(true)构造函数中缺少MainWindow

代码将&#34;永远运行&#34;有或没有这条线。唯一的区别是窗口将会显示。