0

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

1 回答 1

0

setVisible(true)您在MainWindow构造函数中丢失了。

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

于 2015-11-22T11:24:11.283 回答