2011-10-19 152 views
-1

我正在寫一個國際象棋程序,而我正處於推動一個棋子的地步。當一個棋子到達最後一級時,應該彈出一個新的jframe,允許用戶選擇他希望棋子被提升到什麼樣的位置。問題是,當我從我的主要方法創建這個框架時,它顯示得很好,但是當我在我的mouselistener代碼中創建它時,它在屏幕上顯示它背後的確切內容,並且我不知道爲什麼。這裏是我的相關代碼:JFrame顯示不正確的內容

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class Example extends JFrame { 

private SquarePanel[][] squares; 
private Colors colors; 

public Example() { 
    super(); 
    colors = new Colors(); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container con = this.getContentPane(); 
    setSize(800,800); 
    setLocation(0,0); 
    con.setLayout(new GridLayout(8,8)); 
    SquarePanel pane = null; 
    SquarePanel lastpanel = null; 
    squares = new SquarePanel[8][8]; 
    int color = colors.WHITE; 

    for(int i = 7; i >= 0; --i) { 

     if(color == colors.WHITE) 
      color = colors.BLACK; 
     else 
      color = colors.WHITE; 

     for(int j = 0; j <= 7; ++j) { 

      if(color == colors.WHITE) 
       color = colors.BLACK; 
      else 
       color = colors.WHITE; 

      lastpanel = new SquarePanel(color,j,i); 
      pane = lastpanel;    
      con.add(pane); 
      squares[j][i] = pane; 
     } 
    } 

    for(int i = 0; i < 8; ++i) { 
     for(int j = 0; j < 8; ++j) { 
      squares[j][i].setResident("pawn"); 
      //System.out.println(squares[j][i].getResident()); 
      //squares[j][i].repaint(); 
     } 
    } 

    MouseListener listener = new MoveListener(this, squares); 
    for(int i = 0; i < 8; ++i) 
     for(int j = 0; j < 8; ++j) 
      squares[i][j].addMouseListener(listener); 
    setVisible(true); 
    PromotionFrame pf = new PromotionFrame(this, 1); 
    String p = null; 

    while(p == null) { 
     p = pf.getPiece(); 
    } 

    pf.dispose(); 
} 

public class SquarePanel extends JPanel { 
    private ImageIcon content; 
    private int x; 
    private int y; 
    private int color; 
    private String resident; 
    private Colors colors; 

    public SquarePanel(int color, int x, int y) { 
     content = null; 
     resident = null; 
     this.color = color; 
        colors = new Colors(); 
     this.x = x; 
     this.y = y; 
     Border blackline = BorderFactory.createLineBorder(Color.black); 
     setBorder(blackline); 
     this.setLayout(new GridBagLayout()); 
    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     int width = getWidth(); 
     int height = getHeight(); 

     if(color == colors.WHITE) 
      g.setColor(Color.white); 
     else 
      g.setColor(Color.green); 

     g.fillRect(0, 0, width, height); 
     g.setColor(Color.black); 
     //System.out.println("here"); 
    } 

    public String getResident() { 
     return resident; 
    } 

    public void setResident(String p) { 
     resident = p; 

     if(p == null) 
      removeAll(); 
     else 
      add(new JLabel(p)); 
    } 

    public int getXCoordinate() { 
     return x; 
    } 

    public int getYCoordinate() { 
     return y; 
    } 
} 

private class Colors { 
    public int BLACK = 0; 
    public int WHITE = 1; 
} 

public class MoveListener implements MouseListener { 
    private int first_click; 
    private SquarePanel first_source; 
    private JFrame frame; 
    private int turn; 
    private SquarePanel[][] squares; 

    public MoveListener(JFrame f, SquarePanel[][] s) { 
     first_click = 0; 
     this.frame = f; 
     turn = colors.WHITE; 
     squares = s; 
    } 


    /* Empty method definition. */ 
    public void mousePressed(MouseEvent e) { 
     SquarePanel p = (SquarePanel)e.getSource(); 
     ImageIcon content = null; 

     if(first_click == 0) { 
      first_source = p; 
      ++first_click; 
      p.setBorder(BorderFactory.createLineBorder(Color.cyan,4)); 
     } 
     else { 
      first_click = 0; 
      first_source.setBorder(BorderFactory.createLineBorder(Color.black)); 
      //System.out.println(turn == colors.WHITE ? "White" : "Black"); 

      if(p.getResident().equals("pawn") && (p.getYCoordinate() == 7 || p.getYCoordinate() == 0)) { 
       PromotionFrame pframe = new PromotionFrame(frame, colors.WHITE); 
       String piece; 

       do { 
        piece = pframe.getPiece(); 
       } 
       while(piece == null); 

       System.out.println(piece); 
       pframe.dispose(); 
       p.setResident(piece); 
      } 

     } 

    } 

    /* Empty method definition. */ 
    public void mouseReleased(MouseEvent e) { 
    } 

    /* Empty method definition. */ 
    public void mouseEntered(MouseEvent e) { 
    } 

    /* Empty method definition. */ 
    public void mouseExited(MouseEvent e) { 
    } 

    public void mouseClicked(MouseEvent e) { 

    } 

} 

public class PromotionFrame extends JFrame implements MouseListener { 
    private JFrame master; 
    private String piece; 

    public PromotionFrame(JFrame master, int color) { 
     super(); 
     this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     Container con = this.getContentPane(); 
     setSize(400,200); 
     this.master = master; 
     setLocation(master.getX()+master.getWidth()/2-200,master.getY()+master.getHeight()/2-100); 
     setLocation(0,0); 
     con.setLayout(new GridLayout(1,4)); 
     ImageIcon imgicon1; 
     Image img1; 
     Image newimg; 
     ImageIcon img; 
     ImageLabel label; 
     piece = null; 

     label = new ImageLabel("queen"); 
     label.addMouseListener(this); 
     con.add(label); 
     label = new ImageLabel("rook"); 
     label.addMouseListener(this); 
     con.add(label); 
     label = new ImageLabel("bishop"); 
     label.addMouseListener(this); 
     con.add(label); 
     label = new ImageLabel("knight"); 
     label.addMouseListener(this); 
     con.add(label); 

     this.show(); 
    } 

    public String getPiece() { 
     return piece; 
    } 

    /* Empty method definition. */ 
    public void mousePressed(MouseEvent e) { 
     ImageLabel label = (ImageLabel)e.getSource(); 
     piece = label.getPiece(); 
    } 

    /* Empty method definition. */ 
    public void mouseReleased(MouseEvent e) { 
    } 

    /* Empty method definition. */ 
    public void mouseEntered(MouseEvent e) { 
    } 

    /* Empty method definition. */ 
    public void mouseExited(MouseEvent e) { 
    } 

    public void mouseClicked(MouseEvent e) { 

    } 

    private class ImageLabel extends JLabel { 
     private String piece; 

     ImageLabel(String piece) { 
      super(piece); 
      this.piece = piece; 
     } 

     public String getPiece() { 
      return piece; 
     } 
    } 
} 

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

} 
} 
+1

爲了更好地幫助越早,發佈[SSCCE](http://pscode.org/sscce.html)。 –

+0

你確定**全部**這段代碼與問題嚴格相關嗎? – Vlad

+0

太多的代碼來看看和理解你在做什麼。一個評論是你不應該在你的GUI的主線上有一個do/while循環。 – camickr

回答

1

1)改變主

public static void main(String[] args) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      Example example = new Example(); 
     } 
    }); 
} 

2)

您的彈出PromotionFrame可能簡單的凍結,因爲裏面MouseListener您所創建的每個MouseEvents,仔細那MouseListener生成了不可信的數量Events,那麼同樣的數量PromotionFrame_s

一)

class PromotionFrame extends JDialog implements MouseListener { 

B)

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 



setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 

C)

你是怎麼與THI代碼塊一個的JLabel有四個不同的定義方式,那麼最後的有效期爲當前代碼

 label = new ImageLabel("queen"); 
     label.addMouseListener(this); 
     con.add(label); 
     label = new ImageLabel("rook"); 
     label.addMouseListener(this); 
     con.add(label); 
     label = new ImageLabel("bishop"); 
     label.addMouseListener(this); 
     con.add(label); 
     label = new ImageLabel("knight"); 
     label.addMouseListener(this); 
     con.add(label); 

d)

this.show(); 

this.setVisible(true); 

E)創建PromotionFrame代碼只有一次,再利用,對於其他動作或事件

3)請再次讀取如何LayoutManagers工作

+0

我不認爲你的答案可以解決問題。我的例子中有3個類,但它們都是內部類。如果你將整個東西拷貝到一個名爲Example.java的文件中,它應該編譯並運行。如果你運行它,你會正確地顯示提升框架,然後如果按照我的指示並單擊一個正方形,那麼最上面一行中的任何正方形,promotionframe將再次彈出,但不能正確顯示 – yemista

+0

,也就是其結構化的方式。每升一次鼠標點擊就應該創建一次升級框架,然後它應該輪詢一個答案,並在收到答案後離開 – yemista