2015-06-25 86 views
0

所以我有這樣的代碼:Java的繪畫工作不

package tictactoe; 

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class TicTacToe extends JFrame { 

public static void main(String[] args) { 
    JFrame masterFrame = new JFrame("TicTacToe"); 
    JPanel drawingPanel = new JPanel(); 
    GameBoard theGame = new GameBoard(); 

    masterFrame.add(drawingPanel); 
    masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    masterFrame.setSize(504, 504); 
    masterFrame.setResizable(false); 
    masterFrame.setLocationRelativeTo(null); 
    masterFrame.setVisible(true); 
} //End of Main method 


@Override 
public void paint (Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    //Drawing the GridLines 
    g2.drawLine(168, 0, 168, 500); 
    g2.drawLine(336, 0, 336, 500); 
    g2.drawLine(0, 168, 500, 168); 
    g2.drawLine(0, 336, 500, 336); 
} //End of Paint Method 
} //End of TicTacToe Class  

我想它做的是畫4條線到我的井字時尚的JFrame,但JFrame中仍爲空白。爲什麼是這樣?我的代碼有什麼問題,我應該如何解決它?

+0

您的'drawingPane'繪製了使用'paint'方法繪製的任何東西 – MadProgrammer

+1

您的代碼永遠不會創建類TicTacToe的實例,所以類的paint方法從不被調用。 – VGR

+0

@MadProgrammer,我試着評論我添加繪圖面板的部分,但它仍然不起作用。 –

回答

1

假如你實際創建的TicTacToe一個實例,而不是JFrame,你DrawingPane可能會被畫了/覆蓋什麼都你的框架是畫通過它paint方法。

// Create an instance of TicTacToe instead of JFrame... 
//JFrame masterFrame = new JFrame("TicTacToe"); 
TicTacToe masterFrame = new TicTacToe(); 

子組件可以在沒有通知或要求它被塗父容器,這意味着子組件實際上會掩蓋什麼先前已經由幀畫來畫,這可能會產生一些非常奇怪的結果,因爲不同部分得到更新

A JFrame包含許多子組件,包括JRootPanecontentPane,您將自己的組件放在其上。

RootPane

在你的情況下更好的解決辦法可能是使用你的DrawingPane和定製它的paintComponent呈現網格

更多細節

例如見Painting in AWT and SwingPerforming Custom Painting ...

TicTacToe

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TicTacToe { 

    public static void main(String[] args) { 
     new TicTacToe(); 
    } //End of Main method 

    public TicTacToe() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Tic Tac Toe"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TicTacToePane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TicTacToePane extends JPanel { 

     public TicTacToePane() { 
     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      //Drawing the GridLines 
      g2d.drawLine(168, 0, 168, 500); 
      g2d.drawLine(336, 0, 336, 500); 
      g2d.drawLine(0, 168, 500, 168); 
      g2d.drawLine(0, 336, 500, 336); 
      g2d.dispose(); 
     } 

    } 

} //End of TicTacToe Class  

作爲一般規則,建議不要直接從JFrame開始擴展,除了自定義繪畫問題之外,您不會向類中添加任何功能,而是將自己鎖定到單個用例中(很難再次使用該類)

+0

怎麼了試試catch塊? –

+0

另外,你會如何去添加第二個面板旁邊的這個? –

+0

@ UviCaspe圍繞'UIManager.setLookAndFeel'?我對Meta有一種病態的仇恨(默認的外觀和感覺),所以我更喜歡使用系統的外觀和感覺(屏幕截圖中的Windows),但是API可以讓你加載來自不同來源的外觀和感覺,所以它引發了一堆例外情況,一般來說,我不在乎(在這種情況下)。查看[修改外觀和感覺](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html)以獲取更多詳細信息 – MadProgrammer