日期:2014-05-20  浏览次数:20767 次

菜鸟在学GUI 请问我的程序哪里错了
Its an exercise from thinking java. 
pls tell me whats wrong with the code as follow.
If someone could help me fix it, i will be really appreciated it.
thx.

Java code
import javax.swing.*;
import java.awt.*;
import static net.mindview.util.SwingConsole.*;
public class Button1 extends JFrame{

    /**
     * @param args
     */
    private JButton b1 = new JButton("Button 1"),
                    b2 = new JButton("Button 2");
    public Button1(){
        setLayout(new FlowLayout());
        add(b1);
        add(b2);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        run(new Button1(),400,600);
        
    }

}




Java code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.mindview.util.SwingConsole;
public class ex05 extends JFrame {

    /**
     * @param args
     */
    private JButton b1 = new JButton("Button 1"),
                    b2 = new JButton("Button 2"),
                    b3 = new JButton("Button 3");
    private JTextField txt = new JTextField(10);
    private ActionListener bl = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            String name = ((JButton)e.getSource()).getText();
            txt.setText(name);
        }
    };
    public ex05(){
        b1.addActionListener(bl);
        b2.addActionListener(bl);
        b3.addActionListener(bl);
        setLayout(new FlowLayout());
        add(b1);
        add(b2);
        add(b3);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        run(new ex05(), 400, 200);
    }

}


/* output 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method run(ex05, int, int) is undefined for the type ex05

at ex05.main(ex05.java:31)[that is {run(new ex05(), 400, 200);}]


thx guys!

------解决方案--------------------
很简单啊,你声明run方法的时候写的形式参数第一个是JFrame类,但是传参的时候实际参数是ex05类的,虽然它是JFrame的子类,但是肯定类型不匹配,你可以这样
Java code
JFrame jf=new ex05();
run(jf,400, 200);