2015-11-22 65 views
0

JavaSwing應用程序在netbeans中不顯示錯誤。當我嘗試運行它時,它需要永遠無用(JDK 8 + Netbeans 8.1)。日蝕中的相同問題。一個簡單的hello世界程序的作品。該計劃分爲4個班級。對不起,長碼。netbeans不運行javaswing應用程序

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

回答

0

您在MainWindow構造函數中缺少setVisible(true)

該代碼將「永遠運行」有或沒有這條線。唯一的區別是窗口將被顯示。

+0

當窗口顯示時,代碼不會永遠運行。由於'setDefaultCloseOperation(EXIT_ON_CLOSE)'關閉窗口,程序將終止' –

+0

@a_horse_with_no_name我同意,但我認爲當OP說「永遠運行」時,它不涉及關閉操作,只是應用程序如果你什麼都不做,「繼續運行」。 – Tunaki

相關問題