簡體   English   中英

嘗試補全時找不到錯誤

[英]error cannot find symbol when trying to complie

我正在嘗試盡可能多地在線閱讀Java教程,但我很生氣,因為幾乎每個教程都出現某種錯誤。 在此示例中,我找到了一個程序,該程序創建一個基本的gui來包含java2d圖形,並通知您在何處插入函數。 這是我的代碼:

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

// Swing Program Template
@SuppressWarnings("serial")
public class SwingTemplateJPanel extends JPanel {
   // Name-constants
   public static final int CANVAS_WIDTH = 640;
   public static final int CANVAS_HEIGHT = 480;
   public static final String TITLE = "...Title...";
   // ......

   // private variables of GUI components
   // ......

   /** Constructor to setup the GUI components */
   public SwingTemplateJPanel() {
      setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
      // "this" JPanel container sets layout
      // setLayout(new ....Layout());

      // Allocate the UI components
      // .....

      // "this" JPanel adds components
      // add(....)

      // Source object adds listener
      // .....
  }

   /** Custom painting codes on this JPanel */
   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);  // paint background
      setBackground(Color.BLACK);
      drawLine(1, 2, 3, 4);

      // Your custom painting codes
      // ......
   }

   /** The entry main() method */
   public static void main(String[] args) {
      // Run GUI codes in the Event-Dispatching thread for thread safety
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame frame = new JFrame(TITLE);
            frame.setContentPane(new SwingTemplateJPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();             // "this" JFrame packs its components
            frame.setLocationRelativeTo(null); // center the application window
            frame.setVisible(true);            // show it
         }
      });
   }
}

在我認為合適的位置添加drawLine(1,2,3,4)后,它返回錯誤:嘗試編譯時找不到符號,我唯一需要的是一個簡單的GUI,可以在其中靜態繪制每個像素,請幫助。

drawLinejava.awt.Graphics的實例方法,不是JPanel或其任何JPanel

g.drawLine(1, 2, 3, 4);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM