看了一圈,终于知道怎么调用这个paintComponent()
方法了。我现在遇到的问题是这条线g.drawRect(100, 100, 100, 6);
实际上并没有绘制矩形。我System.out.println
在这行代码的上面和下面放了两个,看看是否真的到达了那一行,它确实到达了。
这是该类的构造函数。最后调用paintComponent() 方法
View(Model m)
{
this.m = m;
setTitle("Word Guesssing");
super.setSize( new Dimension (475, 500) );
drawPanel = new JPanel ( new MigLayout() );
guessPanel = new JPanel ( new MigLayout() );
wordPanel = new JPanel ( new MigLayout() );
guessedLettersPanel = new JPanel ( new MigLayout() );
setLetterTextFields(); //Adds the letter JTextFields to an ArrayList in the Model class.
addToGuessPanel(); //Method where components are added to the guessPanel.
addToWordPanel(); //Method where components are added to the wordPanel.
addToGuessedLettersPanel(); //Method where components are added to the guessedLettersPanel.
//Sets a boarder for each panel.
wordPanel.setBorder (new LineBorder(new Color(0, 0, 0)));
drawPanel.setBorder (new LineBorder(new Color(0, 0, 0)));
guessPanel.setBorder (new LineBorder(new Color(0, 0, 0)));
guessedLettersPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
//Sets the sizes for each panel
drawPanel.setPreferredSize(new Dimension (475, 500));
wordPanel.setPreferredSize ( new Dimension(450, 50) );
guessPanel.setPreferredSize( new Dimension(350, 100) );
guessedLettersPanel.setPreferredSize( new Dimension(125, 600) );
setRadioButtonGroup();
addToPane();
setVisible(true);
setResizable(false);
paintComponent(drawPanel.getGraphics());
}
这是paintComponent()方法
public void paintComponent( Graphics g )
{
System.out.println("here 1");
g.drawRect(100, 100, 100, 6);
System.out.println("here 2");
}