2013-09-28 55 views
3

我想在我的JFrame的內容窗格中繪製一個網格,但是當我這樣做時,一切都關閉了。網格開始太寬和偏移(大概設置窗口邊界與內容窗格重疊的位置),並且它不會正確繪製整個網格。當我使用frame.setUndecorated(true);一切正常。重新定位JFrame的ContentPane

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.*; 

@SuppressWarnings("serial") 
class Creator extends JFrame{ 

    JFrame frame; 
    int[][] Grid = new int[18][27]; 
    public Creator(){ 
     frame = this; 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     for(int i = 0; i < 18; i++) { 
      for(int j = 0; j < 27; j++) { 
       Grid[i][j] = 0; 
      } 
     } 
     setSize(864, 544); 

     getContentPane().addMouseListener(new MouseAdapter() {    
      @Override 
      public void mouseClicked(MouseEvent e) { 
       int yLocation = e.getX()/32; 
       int xLocation = e.getY()/32; 
       System.out.println(xLocation + " " + yLocation); 

       if(e.getButton() == 1){ 
        if(xLocation < 1 || yLocation < 1){ 
         Grid[xLocation][yLocation] = 2; 
         System.out.println("Enemy Added"); 
        }else if(xLocation > 16 || yLocation > 25){ 
         Grid[xLocation][yLocation] = 2; 
         System.out.println("Enemy Added"); 
        }else { 
         Grid[xLocation][yLocation] = 1; 
         System.out.println("Obstacle Added"); 
        } 
       }else { 
        Grid[xLocation][yLocation] = 0; 
        System.out.println("Item Removed"); 
       } 
       frame.invalidate(); 
       frame.validate(); 
       frame.repaint(); 
      } 
     }); 

    } 

    public void paint(Graphics g) { 
     g.clearRect(0, 0, 864, 544); 
     Graphics2D g2 = (Graphics2D) g; 
     for(int i =0; i < 18; i++) { 
      for(int j =0; j < 27; j++) { 
       if(Grid[i][j] != 0){ 
        if(i < 1 || j < 1 || i > 26 || j > 17) { 
         g2.setColor(Color.RED); 
         g2.fillRect(j*32, i*32, 32, 32); 
        }else { 
         g2.setColor(Color.BLUE); 
         g2.fillRect(j*32, i*32, 32, 32); 
        } 
       } 
       //System.out.print(Grid[i][j]); 
      } 
      //System.out.println(); 
     } 
     for(int i = 0; i < 27; i++) { 
      Line2D lin = new Line2D.Float(i*32, 0, i*32, 544); 
      g2.draw(lin); 
     } 
     for(int i = 0; i < 18; i++) { 
      Line2D lin = new Line2D.Float(0, i*32, 864, i*32); 
      g2.draw(lin); 
     } 

    } 

    public static void main(String []args){ 
     Creator s=new Creator(); 
     s.setVisible(true); 
    } 
} 

回答

3

不設置幀的大小。

取而代之,創建JPanel或JComponent的子類,覆蓋paintComponent()以繪製網格,並覆蓋getPreferredSize()以返回其應具有的大小(864 x 544)。將此面板添加到您的框架中,並致電pack()以確保框架尺寸自行調整到以其首選尺寸顯示其組件所需的尺寸。

編輯:例如:

public class GridPanel extends JPanel { 
    @Override 
    public void paintComponent(Graphics g) { 
     // paint the grid here 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(864, 544); 
    } 
} 

,並在JFrame的構造函數:

GridPanel gridPanel = new GridPanel(); 
this.add(gridPanel); 
this.pack(); 
+0

你能舉個例子嗎?我對JFrames非常陌生 – Chris

+0

完成。看到我編輯的答案,並閱讀搖擺教程。 –

+0

謝謝:)它總是有趣的探索區域,你從來沒有在 – Chris

1

JB Nizet,爲您提供了出色的解決方案。更改您的代碼如下:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.*; 

@SuppressWarnings("serial") 
class Creator extends JPanel{ 


    static int[][] Grid = new int[18][27]; 
    public Creator(){   
     setSize(getPreferredSize()); 
    } 

    public void paint(Graphics g) { 
     g.clearRect(0, 0, getWidth(), getHeight()); 
     Graphics2D g2 = (Graphics2D) g; 
     for(int i =0; i < 18; i++) { 
      for(int j =0; j < 27; j++) { 
       if(Grid[i][j] != 0){ 
        if(i < 1 || j < 1 || i > 26 || j > 17) { 
         g2.setColor(Color.RED); 
         g2.fillRect(j*32, i*32, 32, 32); 
        }else { 
         g2.setColor(Color.BLUE); 
         g2.fillRect(j*32, i*32, 32, 32); 
        } 
       } 
       //System.out.print(Grid[i][j]); 
      } 
      //System.out.println(); 
     } 
     for(int i = 0; i < 27; i++) { 
      Line2D lin = new Line2D.Float(i*32, 0, i*32, getHeight()); 
      g2.draw(lin); 
     } 
     for(int i = 0; i < 18; i++) { 
      Line2D lin = new Line2D.Float(0, i*32, getWidth(), i*32); 
      g2.draw(lin); 
     } 

    } 

    public static void main(String []args){ 
     JFrame frame=new JFrame(); 
     Creator s=new Creator(); 
     frame.setSize(864, 544); 
     frame.add(s); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().addMouseListener(new MouseAdapter() {    
      @Override 
      public void mouseClicked(MouseEvent e) { 
       int yLocation = e.getX()/32; 
       int xLocation = e.getY()/32; 
       System.out.println(xLocation + " " + yLocation); 

       if(e.getButton() == 1){ 
        if(xLocation < 1 || yLocation < 1){ 
         Grid[xLocation][yLocation] = 2; 
         System.out.println("Enemy Added"); 
        }else if(xLocation > 16 || yLocation > 25){ 
         Grid[xLocation][yLocation] = 2; 
         System.out.println("Enemy Added"); 
        }else { 
         Grid[xLocation][yLocation] = 1; 
         System.out.println("Obstacle Added"); 
        } 
       }else { 
        Grid[xLocation][yLocation] = 0; 
        System.out.println("Item Removed"); 
       } 
//    frame.invalidate(); 
//    frame.validate(); 
//    frame.repaint(); 
      } 
     }); 
//  s.setUndecorated(true); 
    } 
} 
+0

'frame.setSize(864,544);'這是次優和不必要的。更仔細地看待更好方法的可接受答案。 –