//这个是自定义的一个类 建立SmellLeft类
import java.awt.*;
import javax.swing.*;
public class SmellLeft extends JPanel{
public void paintComponent(Graphics g){
int a=0;
int b=120;
int num=(int)(Math.random()*10+1);
if(num<=4){
for(int i=1;i<=num;i++){
Image smell = new ImageIcon("smiley.png").getImage();
g.drawImage(smell,a,0,this);
a=a+b;
}
}
if(num>=5&&num<=8){
for(int i=1;i<=4;i++){
Image smell = new ImageIcon("smiley.png").getImage();
g.drawImage(smell,a,0,this);
a=a+b;
}
for(int i=5;i<=num;i++){
a=0;
Image smell = new ImageIcon("smeily.png").getImage();
g.drawImage(smell,a,b,this);
a=a+b;
}
}
if(num>=9){
for(int i=1;i<=4;i++){
Image smell = new ImageIcon("smiley.png").getImage();
g.drawImage(smell,a,0,this);
a=a+b;
}
for(int i=5;i<=8;i++){
a=0;
Image smell = new ImageIcon("smeily.png").getImage();
g.drawImage(smell,a,b,this);
a=a+b;
}
for(int i=9;i<=num;i++){
a=0;
Image smell = new ImageIcon("smeily.png").getImage();
g.drawImage(smell,a,2*b,this);
a=a+b;
}
}
}
}
//这个是运行程序 带有main函数的。建立了SmellLeft对象 在对象建立那里老是有错误 说找不到符号。。
package org.component;
import java.awt.Frame;
import javax.swing.*;
public class ExecuteClass extends Frame{
public ExecuteClass(){
this.setTitle("Welcome to Smelly Count!");
}
public static void main(String[] args){
JFrame myFrame = new JFrame();
myFrame.setSize(1100,1100);
myFrame.setVisible(true);
myFrame.getContentPane().add(new SmellLeft());
myFrame.getContentPane().add(new SmellRight());
}
}
还是不可以········反而多了两个错误······
原来你在SmellLeft类里面没写构造方法。你在这个类里面加个public SmellLeft(){}。
追问加上了就成了这个样子······
我构造函数随便写的,你照搬啊?我只是举个例子,构造函数都不会么,看书。
追问得写什么形式的构造函数啊?需要用super()什么的吗?现在再学就来不及了····要交啦 ~~帮我看看呗···············
public void paintComponent(Graphics g)在这个上面写 public SmellLeft(){},在另外一个SmellRight类里面写public SmellRight(){}