gpt4 book ai didi

java - 尝试编译时出现错误找不到符号

转载 作者:行者123 更新时间:2023-12-01 23:13:47 35 4
gpt4 key购买 nike

我试图遵循尽可能多的 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 或其任何父类(super class)

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

关于java - 尝试编译时出现错误找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21533083/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com