新手的问题,请高手门看看
本文导语: /** * タイトル: * 説明: * 著作権: Copyright (c) * 会社名: * @author * @version 1.0 */ package test1; import java.awt.*; import java.awt.event.*; public class Ch7_1 extends Frame{ Label lbl1...
/**
* タイトル:
* 説明:
* 著作権: Copyright (c)
* 会社名:
* @author
* @version 1.0
*/
package test1;
import java.awt.*;
import java.awt.event.*;
public class Ch7_1 extends Frame{
Label lbl1 = new Label("count the click ");
Button button1 = new Button("count");
int count=0;
public static void main(String args[]) {
Ch7_1 frame1 = new Ch7_1();
frame1.setTitle("") ;
frame1.setBackground(Color.pink) ;
frame1.setLayout(null);
frame1.setSize(200,200) ;
lbl1.setBounds(5,20,150,25) ;
button1.setBounds(5,50,75,25) ;
frame1.setVisible(true) ;
}
public Ch7_1() {
add(lbl1);
add(button1);
button1.addActionListener(new ActionLis()) ;
}
class ActionLis implements ActionListener {
public void actionPerformed(ActionEvent e) {
count++;
lbl1.setText("click's count is :" + count) ;
}
}
}
我想问下为什么
lbl1.setBounds(5,20,150,25) ;
button1.setBounds(5,50,75,25) ;
这2句放在main中会报错,可以说详细点吗
|
是 static 的,所以只能调用 static 的函数;
而 setBounds 的定义是:
public void setBounds(int x,
int y,
int width,
int height)
懂了吗
其实 这些画界面的东东最好都放在构造函数里
|
import java.awt.*;
import java.awt.event.*;
public class Ch7_1 extends Frame{
Label lbl1 = new Label("count the click ");
Button button1 = new Button("count");
int count=0;
public static void main(String args[]) {
Ch7_1 frame1 = new Ch7_1();
frame1.setTitle("") ;
frame1.setBackground(Color.pink) ;
frame1.setLayout(null);
frame1.setSize(200,200) ;
frame1.lbl1.setBounds(5,20,150,25) ;
frame1.button1.setBounds(5,50,75,25) ;
frame1.setVisible(true) ;
}
public Ch7_1() {
add(lbl1);
add(button1);
button1.addActionListener(new ActionLis()) ;
}
class ActionLis implements ActionListener {
public void actionPerformed(ActionEvent e) {
count++;
lbl1.setText("click's count is :" + count) ;
}
}
}