Java试用(3)awt,ui.
程序 (注意要点: control, layout, 事件处理)
代码
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame{
private TextField tf;
public static void main(String args[ ]){
MyFrame fr = new MyFrame("Hello Out There!");
fr.InitUI();
}
public MyFrame (String str){
super(str); //调用父类的构造方法
}
public void InitUI()
{
//Frame
//setSize(200,200);
setBackground(Color.red);
//setLayout(null);
//setLayout(new FlowLayout());
//setLayout(new BorderLayout()); //BAD
setLayout(new GridLayout(2,3));
//setLayout(new CardLayout()); //crash
//setLayout(new GridBagLayout());
//menus
MenuBar mb = new MenuBar();
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
Menu m3 = new Menu("Help");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
setMenuBar(mb);
// controls
Panel pan=new Panel();
pan.setSize(100,100);
pan.setBackground(Color.yellow);
Button b1 = new Button("Button1");
b1.addActionListener(new ButtonHandler());
Button b2 = new Button("Button2");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Action 2 occurred");
tf.setText("Action 2 occurred");
}
});
tf=new TextField(30);
// frames
add(pan);
add(b1);
add(b2);
add(tf);
pack();
addMouseMotionListener(new MouseMotionAdapter(){ //匿名类开始
public void mouseDragged(MouseEvent e){
String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
tf.setText(s); }
} ); //匿名类结束
setVisible(true);
}
}
class ButtonHandler implements ActionListener {
//实现接口ActionListener才能做事件ActionEvent的处理者
public void actionPerformed(ActionEvent e)
//系统产生的ActionEvent事件对象被当作参数传递给该方法
{
System.out.println("Action occurred");
//本接口只有一个方法,因此事件发生时,系统会自动调用本方法,需要做的操作就把代码写在则个方法里。
}
}
import java.awt.event.*;
public class MyFrame extends Frame{
private TextField tf;
public static void main(String args[ ]){
MyFrame fr = new MyFrame("Hello Out There!");
fr.InitUI();
}
public MyFrame (String str){
super(str); //调用父类的构造方法
}
public void InitUI()
{
//Frame
//setSize(200,200);
setBackground(Color.red);
//setLayout(null);
//setLayout(new FlowLayout());
//setLayout(new BorderLayout()); //BAD
setLayout(new GridLayout(2,3));
//setLayout(new CardLayout()); //crash
//setLayout(new GridBagLayout());
//menus
MenuBar mb = new MenuBar();
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
Menu m3 = new Menu("Help");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
setMenuBar(mb);
// controls
Panel pan=new Panel();
pan.setSize(100,100);
pan.setBackground(Color.yellow);
Button b1 = new Button("Button1");
b1.addActionListener(new ButtonHandler());
Button b2 = new Button("Button2");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Action 2 occurred");
tf.setText("Action 2 occurred");
}
});
tf=new TextField(30);
// frames
add(pan);
add(b1);
add(b2);
add(tf);
pack();
addMouseMotionListener(new MouseMotionAdapter(){ //匿名类开始
public void mouseDragged(MouseEvent e){
String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
tf.setText(s); }
} ); //匿名类结束
setVisible(true);
}
}
class ButtonHandler implements ActionListener {
//实现接口ActionListener才能做事件ActionEvent的处理者
public void actionPerformed(ActionEvent e)
//系统产生的ActionEvent事件对象被当作参数传递给该方法
{
System.out.println("Action occurred");
//本接口只有一个方法,因此事件发生时,系统会自动调用本方法,需要做的操作就把代码写在则个方法里。
}
}
编译运行
javac MyFrame.java
java MyFrame