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.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);
}}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!
pls tell me whats wrong with the code as follow.
If someone could help me fix it, i will be really appreciated it.
thx.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);
}}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!
//Tool for running Swing demos from the
//console, both applets and JFrames.
package net.mindview.util;
import javax.swing.*;public class SwingConsole {
public static void
run(final JFrame f, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
} ///:~
JFrame jf=new ex05();
run(jf,400, 200);
i have tried, but it still does not work.....i dont think this is a type matching problem because the code section1 public class Button1 extends JFrame{}is able to be compiled.help me guy!
你的错误是由于没有在ex05中定义run方法,或许你的意思是想使用SwingConsole中的run方法,那么应该把那句话改成这样
SwingConsole.run(new ex05(),400,200);
另外,Button1不知道你那为什么能编译通过,我这里也是报undefined错误,原因跟ex05一样
PS:既然继承的是JFrame,最好不要起名叫Button,程序规模大了之后很容易混淆
Hey dude.i know the reason, that is i should quote the package net.mindview.xxxxx.xxx by static. haha, anyway thx for ur help!