// DrawPanelTest.java
// Application to display a DrawPanel.
import javax.swing.JFrame;
public class DrawPanelTest {
public static void main( String[] args ) {
// create a panel that contians our drawing
DrawPanel panel = new DrawPanel();
// create a new frame to hold the panel
JFrame application = new JFrame();
// set the frame to exit when it is close
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
application.add( panel ); // add the panel to the frame
application.setSize( 250, 250 ); // set tje size of the frame
application.setVisible( true ); // make the frame visible
} // end main\
} // end class DrawPanelTest
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
//int for whole class
private int N;
// constructor
public DrawPanel(int numLines)
{
N = numLines;
} // end constructor
// draws an numLines from the corners of the panel
public void paintComponent(Graphics g)
{
// call paintComponent to ensurethe angle displays
super.paintComponent(g);
int width = getWidth(); // total width
int height = getHeight(); // total height
// draws (N + 1) lines with equal spacing
if (N > 0) {
for (int counter = 0; counter <= N; counter++) {
g.drawLine(0, height / 2, width, counter * height / N);
}
}
}
} 遇到错误有困难并且不确定该怎么办或如何解决该错误需要帮助,谢谢
Microsoft Windows [版本10.0.17134.1006] (c)2018年微软公司。保留所有权利。
C:\ Users \ MAC> cd桌面
C:\ Users \ MAC \ Desktop> javac DrawPanel.java
C:\ Users \ MAC \ Desktop> javac DrawPanelTest.java DrawPanelTest.java:11:错误:类DrawPanel中的构造函数DrawPanel无法应用于给定类型; DrawPanel面板= new DrawPanel(); ^ 要求:int 找到:没有参数 原因:实际和正式论点清单的长度不同 1个错误
C:\ Users \ MAC \ Desktop>
答案 0 :(得分:1)
要么创建一个无参数的构造函数,要么在调用类时传递参数,如下所示:-
public DrawPanel()
{
N = 1;// if you want to assign some default value
}
使用以下参数运行:-
javac Arguments.java
java Arguments arg0 arg1 arg2