我正在尝试在线遵循尽可能多的在线教程,但是我很生气,因为几乎每个人都咳嗽了某种错误。在此示例中,我找到了一个程序,该程序创建了一个基本的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,我可以静态地绘制每个符号plz help。
drawLine
是java.awt.Graphics
不是JPanel
或其任何超级类的实例方法
g.drawLine(1, 2, 3, 4);