我正在努力在线跟踪尽可能多的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,我可以静态地绘制每个像素打开,PLZ帮助。