import java.awt.*;
import java.awt.event.*;
import java.io.*;
class AFrame extends Frame
{
final Frame f;
AFrame(String str)
{
f=new Frame(str);
f.setSize(600, 400);
f.setLocation(200,200);
f.setBackground(Color.ORANGE);
f.setLayout(new GridLayout(3,3)); //似乎只能指定行数,不能指定列数
f.addWindowListener(new WindowListener1());
}
public void show()
{
f.setVisible(true);
}
void intializeABorderLayout()
{
Button b1=new Button("我点");
b1.addActionListener(new MyActionListener1()); //注册事件监听器
Button b2=new Button("我点");
b2.addActionListener(new MyActionListener1());
Button b3=new Button("我点");
b3.addActionListener(new MyActionListener1());
Button b4=new Button("我点");
b4.addActionListener(new MyActionListener1());
Button b5=new Button("我点");
b5.addActionListener(new MyActionListener1());
Panel p1=new Panel(new BorderLayout(1,1));
p1.add(b1,BorderLayout.NORTH);
p1.add(b2,BorderLayout.EAST);
p1.add(b3,BorderLayout.WEST);
p1.add(b5,BorderLayout.SOUTH);
p1.add(b4,BorderLayout.CENTER);
f.add(p1);
}
void intializeAFlowLayout() //顺序排列
{
Button b1=new Button("我点");
b1.addActionListener(new MyActionListener1());
Button b2=new Button("我点");
b2.addActionListener(new MyActionListener1());
Button b3=new Button("我点");
b3.addActionListener(new MyActionListener1());
Button b4=new Button("我点");
b4.addActionListener(new MyActionListener1());
Button b5=new Button("我点");
b5.addActionListener(new MyActionListener1());
Panel p2=new Panel(new FlowLayout(FlowLayout.CENTER));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
f.add(p2);
}
void intializeAGridLayout() //网格状
{
Button b1=new Button("我点");
b1.addActionListener(new MyActionListener1());
Button b2=new Button("我点");
b2.addActionListener(new MyActionListener1());
Button b3=new Button("我点");
b3.addActionListener(new MyActionListener1());
Button b4=new Button("我点");
b4.addActionListener(new MyActionListener1());
Button b5=new Button("我点");
b5.addActionListener(new MyActionListener1());
Button b6=new Button("我点");
b6.addActionListener(new MyActionListener1());
Panel p3=new Panel(new GridLayout(3,2,1,1));
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
f.add(p3);
}
void intializeACardLayout() //卡片装,可实现翻牌效果
{
final CardLayout c=new CardLayout();
final Panel p4=new Panel(c); //满足匿名内部类访问其的条件,final
ActionListener a1=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.next(p4);
}
};
Button b1=new Button("我点1");
b1.addActionListener(a1);
Button b2=new Button("我点2");
b2.addActionListener(a1);
Button b3=new Button("我点3");
b3.addActionListener(a1);
Button b4=new Button("我点4");
b4.addActionListener(a1);
Button b5=new Button("我点5");
b5.addActionListener(a1);
Button b6=new Button("我点6");
b6.addActionListener(a1);
p4.add(b1,"1");
p4.add(b2,"2");
p4.add(b3,"3");
p4.add(b4,"4");
p4.add(b5,"5");
p4.add(b6,"6");
f.add(p4);
}
void intializeAMenuBar() //加入菜单栏
{
MenuBar mb=new MenuBar();
Menu mn1=new Menu("我喜欢");
Menu mn2=new Menu("点秋香");
MenuItem mi1=new MenuItem("第一个玩家");
MenuItem mi2=new MenuItem("第二个玩家");
MenuItem mi3=new MenuItem("退出");
mi3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
MenuItem mi4=new MenuItem("打开");
mi4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(f,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String strFile=fd.getDirectory()+fd.getFile();
try
{
byte[] buf=new byte[1024*10];
FileInputStream fis=new FileInputStream(strFile);
int length=fis.read(buf);
final Frame f1=new Frame();
f1.setSize(400,600);
f1.setLocation(400,400);
TextArea ta=new TextArea();
f1.add(ta);
ta.append(new String(buf,0,length));
f1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f1.dispose();
}
});
f1.setVisible(true);
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(0);
}
}
});
mn1.add(mi1);
mn1.add(mi2);
mn1.add(mi4);
mn2.add(mi3);
mb.add(mn1);
mb.add(mn2);
f.setMenuBar(mb);
}
void intializeAChoice()
{
Choice cho=new Choice();
cho.add("我点");
cho.add("我点");
cho.add("我点点");
f.add(cho);
}
void intializeACheckbox()
{
Panel p5=new Panel();
p5.setLayout(new GridLayout(3,1));
p5.add(new Checkbox("我点",false));
p5.add(new Checkbox("我点",false));
p5.add(new Checkbox("我点点",false));
f.add(p5);
}
}
class WindowListener1 extends WindowAdapter //事件监听器
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class MyActionListener1 implements ActionListener //点击它改变按钮标签
{
int i=0;
public void actionPerformed(ActionEvent e)
{
if(i%2==0)
((Button)e.getSource()).setLabel("已点");
else
((Button)e.getSource()).setLabel("再点");
i++;
}
}
public class MyFrame { public static void main(String[] args) { AFrame af=new AFrame("唐伯虎点秋香"); af.intializeABorderLayout(); af.intializeAFlowLayout(); af.intializeAGridLayout(); af.intializeACardLayout(); af.intializeAMenuBar(); af.intializeAChoice(); af.intializeACheckbox(); af.show(); } }