所以我有這樣的代碼: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中仍爲空白。爲什麼是這樣?我的代碼有什麼問題,我應該如何解決它?
您的'drawingPane'繪製了使用'paint'方法繪製的任何東西 – MadProgrammer
您的代碼永遠不會創建類TicTacToe的實例,所以類的paint方法從不被調用。 – VGR
@MadProgrammer,我試着評論我添加繪圖面板的部分,但它仍然不起作用。 –