Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
netbeans不运行javaswing应用程序_Java_Netbeans_Netbeans 8.1 - Fatal编程技术网

netbeans不运行javaswing应用程序

netbeans不运行javaswing应用程序,java,netbeans,netbeans-8.1,Java,Netbeans,Netbeans 8.1,JavaSwing应用程序在netbeans中没有显示错误。当我尝试运行它时,它会花费很长时间,而且不会发生任何事情(JDK 8+Netbeans 8.1)。eclipse中也存在同样的问题。一个简单的hello world程序可以工作。该课程分为4个班。抱歉,代码太长了 package test2; import javax.swing.JFrame; import static javax.swing.JFrame.EXIT_ON_CLOSE; public class MainWind

JavaSwing应用程序在netbeans中没有显示错误。当我尝试运行它时,它会花费很长时间,而且不会发生任何事情(JDK 8+Netbeans 8.1)。eclipse中也存在同样的问题。一个简单的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);
}
}
包测试2;
导入javax.swing.JFrame;
在关闭时导入静态javax.swing.JFrame.EXIT_;
公共类主窗口扩展JFrame{
私有导航器=新导航器(此);
私有字段spielfeld=新字段();
公共主窗口(){
超级(“游戏测试”);
此.setDefaultCloseOperation(关闭时退出);
添加(斯皮尔菲尔德);
此参数为.setresizeable(false);
这个包();
}
公共静态void main(字符串[]args){
新建主窗口();
}
}
包装测试2;
导入javax.swing.JPanel;
导入javax.swing.JLabel;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.GridLayout;
公共类字段扩展了JPanel{
私有静态int x=10;
私有静态int y=10;
私有JLabel[][]字段=新JLabel[10][10];
公共领域(){
尺寸标注=新尺寸标注();
尺寸设置尺寸(50,50);
这个.setLayout(新的GridLayout(x,y));
for(int i=0;i
您的
主窗口中缺少
设置可见(true)


不管有没有这一行,代码都将“永远运行”。唯一的区别是将显示窗口。

显示窗口时,代码不会“永远”运行。由于
setDefaultCloseOperation(关闭时退出)
@a_horse_,没有名字,程序将在窗口关闭后终止。我同意,但我认为当OP说“永远运行”时,它不涉及关闭操作,只是如果你不做任何事情,应用程序“继续运行”。