流式布局FlowLayout
package net.zyz; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; public class TestFlowLayout { public static void main(String[] args) { Frame frame=new Frame("Flow Layout"); //设置窗口的布局方式为FlowLayout,元素在窗口中左对齐,元素水平间距为10,垂直间距为20 frame.setLayout(new FlowLayout(FlowLayout.LEFT,10,20)); for(int i=1;i<=10;i++){ frame.add(new Button("按纽"+i)); } frame.pack(); frame.setVisible(true); } }
边界布局BorderLayout
package net.zyz; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; public class TestBorderLayout { public static void main(String[] args) { Frame frame=new Frame("Border Layout"); //边界布局是frame默认的布局方式 Button button1=new Button("东"); frame.add(button1, BorderLayout.EAST); Button button2=new Button("西"); frame.add(button2, BorderLayout.WEST); Button button3=new Button("南"); frame.add(button3, BorderLayout.SOUTH); Button button4=new Button("北"); frame.add(button4, BorderLayout.NORTH); Button button5=new Button("中"); frame.add(button5, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }
表格布局GridLayout
package net.zyz; import java.awt.Button; import java.awt.Frame; import java.awt.GridLayout; public class TestGridLayout { public static void main(String[] args) { Frame frame=new Frame("Grid Layout"); //frame默认的布局为BorderLayout frame.setLayout(new GridLayout(2, 3)); for(int i=1;i<=5;i++){ frame.add(new Button("按纽"+i)); } //setSize只指定窗口的大小,位置在原点(0,0) //setBound既可指定位置,又可指定大小 // frame.setSize(300,200); //pack会将窗口中的组件正好包裹起来,即大小自适应 frame.pack(); frame.setVisible(true); } }
综合布局
package net.zyz; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; public class TestMutiLayout { public static void main(String[] args) { Frame frame=new Frame("MutiLayout"); //窗口分成2行1列,分上下两个panel frame.setLayout(new GridLayout(2, 1)); //第1个panel采用BorderLayout Panel p1=new Panel(); p1.setLayout(new BorderLayout()); Button b11=new Button("ButtonWest"); Button b12=new Button("ButtonEast"); //CENTER部分再使用panel并采用GridLayout Panel p11=new Panel(); p11.setLayout(new GridLayout(2, 1)); Button b13=new Button("ButtonUp"); Button b14=new Button("ButtonDown"); p11.add(b13); p11.add(b14); p1.add(b11, BorderLayout.EAST); p1.add(b12, BorderLayout.WEST); p1.add(p11, BorderLayout.CENTER); //第2个panel采用BorderLayout Panel p2=new Panel(); p2.setLayout(new BorderLayout()); Button b21=new Button("ButtonWest"); Button b22=new Button("ButtonEast"); //CENTER部分再使用panel并采用GridLayout Panel p21=new Panel(); p21.setLayout(new GridLayout(2, 2)); for(int i=1;i<=4;i++){ p21.add(new Button("Button")); } p2.add(b21, BorderLayout.EAST); p2.add(b22, BorderLayout.WEST); p2.add(p21, BorderLayout.CENTER); frame.add(p1); frame.add(p2); frame.setSize(300,200);; frame.setVisible(true); } }
绘图Graphics
package net.zyz; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; public class TestGraphics { public static void main(String[] args) { new FrameDraw(); } } class FrameDraw extends Frame{ /** * */ private static final long serialVersionUID = 1L; public FrameDraw() { setSize(400,300); setVisible(true); } @Override /** * Object->Component->Container->Window->Frame * paint是Container类的方法,在继承Frame类中要重写,重写后系统就会自动调用 */ public void paint(Graphics g) { Color color=g.getColor(); g.setColor(Color.RED); g.drawOval(50, 50, 200, 150); g.setColor(new Color(0, 255, 0)); g.fillRect(100, 100, 200, 50); g.setColor(color); } }
动作事件监听java.awt.event.ActionListener
Button
package net.zyz; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestActionListener { public static void main(String[] args) { new Frame2(); } } class Frame2 extends Frame { /** * */ private static final long serialVersionUID = 1L; public Frame2() { super("Event Listener"); Button button1 = new Button("button"); Button button2 = new Button("button"); button1.setActionCommand("button1"); button2.setActionCommand("button2"); //button1和button2加入同一个监听器,如何区分? button1.addActionListener(new monitor()); button2.addActionListener(new monitor()); add(button1, BorderLayout.NORTH); add(button2, BorderLayout.SOUTH); pack(); setVisible(true); } } class monitor implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Button button=(Button)e.getSource(); //首先要得到事件源对象,因getSource()返回的是Object,需强制转换为Button String actionCommand=button.getActionCommand(); /** * Button的getLabel()方法得到是按纽上的文字,有可能两个按纽的文本会一样,如本例 * Button的getName()得到的是按纽的name,这个name值是由系统命名的,第1个Button的name是button0 * 最好使用getActionCommand()方法进行区分处理 */ if(actionCommand.equals("button1")){ System.out.println("你按下了button1按纽"); }else if(actionCommand.equals("button2")){ System.out.println("你按下了button2按纽"); } } }
TextField
package net.zyz; import java.awt.Frame; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestTextField { public static void main(String[] args) { new TFFrame(); } } class TFFrame extends Frame{ /** * */ private static final long serialVersionUID = 1L; public TFFrame() { TextField txt1=new TextField(10); //对于Button、TextField等标准组件,一般都采用ActionEvent txt1.addActionListener(new monitorTF()); add(txt1); pack(); setVisible(true); } } class monitorTF implements ActionListener{ @Override /** * 对于按纽来说默认的动作事件是单击,文本框默认事件的是按回车键, * 这些事件执行时,系统回自动调用actionPerformed方法 * 所以需要在监听器里要重写该方法,ActionListener接口里也只有这一个方法 */ public void actionPerformed(ActionEvent e) { TextField tf1=(TextField)(e.getSource()); System.out.println(tf1.getText()); tf1.setText(""); } }
Window事件监听WindowListener
package net.zyz; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestWindowClose { public static void main(String[] args) { new FrameWindowCloseTest(); } } class FrameWindowCloseTest extends Frame { private static final long serialVersionUID = 1L; public FrameWindowCloseTest() { setSize(300, 200); setVisible(true); /* * addWindowListener方法的参数为继承了WindowAdapter的匿名类 * 该匿名类直接写在方法的内部 * 一般情况下很少使用,只有代码简单且无需变动时才临时使用,推荐使用内部类 */ this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } }); /* * addWindowListener()方法的参数为WindowListener对象 * 而WindowListener是一个接口,里面有6、7个类似windowClosing()的方法 * 如果要来实现接口的话,则需要实现接口里的每一个方法 * jdk提供了实现WinowListener接口的抽象类WindowAdapter, * 其实WindowApdater类中实现的方法并无实际代码 * 对于类中的方法,子类是不需要全都实现。本例中只要重写windowClosing()方法即可。 * monitor extends WindowAdapter implements WindowListener * WindowAdapter只是一个中间件,过渡一下 */ } }
鼠标事件监听器MouseListener
package net.zyz; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Iterator; public class TestMouseListener { public static void main(String[] args) { new FramePoints(); } } class FramePoints extends Frame{ private static final long serialVersionUID = 1L; ArrayList<Point> points; public FramePoints() { points=new ArrayList<Point>(); this.addMouseListener(new MonitorPoint()); this.addWindowListener(new WindowClose(this)); setSize(500,400); setVisible(true); } @Override public void paint(Graphics g) { g.setColor(Color.GREEN); Iterator<Point> iterator=points.iterator(); while (iterator.hasNext()) { Point point = (Point) iterator.next(); g.fillOval(point.x, point.y, 10, 10); } } //监听鼠标事件:MouseAdapter implements MouseListener class MonitorPoint extends MouseAdapter{ @Override public void mousePressed(MouseEvent e) { FramePoints f=(FramePoints)e.getSource(); points.add(new Point(e.getX(), e.getY())); f.repaint(); } } //监听窗口事件:WindowAdapter implements WindowListener // class MonitorWindow extends WindowAdapter{ // public void windowClosing(WindowEvent e) { // setVisible(false); // System.exit(0); // } // } }
按键事件监听器KeyListener
package net.zyz; import java.awt.Frame; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestKey { public static void main(String[] args) { new FrameKey().lanch(); } } class FrameKey extends Frame { private static final long serialVersionUID = 1L; void lanch() { setSize(300, 200); this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); /** * getKeyCode()方法得到是按键的虚拟键码, * 如按A键无论大小写都是65(美式键盘布局),而且跟键盘布局也有关。 * 如果按组合键的话,会激活多个keyPressed事件, * 如shift+A组合键getKeyCode()方法就会得到两个键值,但getKeyChar()方法只会得到一个字符('a') * 所以keyPressed事件最好和getKeyCode()方法结合使用 * 虚拟键码 用于报告按下了键盘上的哪个键,而不是一次或多次键击组合生成的字符(如 "A" 是由 shift + "a" 生成的)。 * 例如,按下 Shift 键会生成 keyCode 为 VK_SHIFT 的 KEY_PRESSED 事件, * 而按下 'a' 键将生成 keyCode 为 VK_A 的 KEY_PRESSED 事件。 * 释放 'a' 键后,会激发 keyCode 为 VK_A 的 KEY_RELEASED 事件。 * 另外,还会生成一个 keyChar 值为 'A' 的 KEY_TYPED 事件。 */ System.out.println(keyCode); System.out.println(e.getKeyChar()); switch (keyCode) { case KeyEvent.VK_UP: System.out.println("up"); break; case KeyEvent.VK_DOWN: System.out.println("down"); break; case KeyEvent.VK_LEFT: System.out.println("left"); break; case KeyEvent.VK_RIGHT: System.out.println("right"); break; default: break; } } }); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } }); setVisible(true); } }