import java.awt.*; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class FrameWindowDemo extends Frame implements ActionListener { TextField txtCenter; Button btnNorth, btnSouth, btnEast, btnWest; FrameWindowDemo(String title) { super(title); setLayout(new BorderLayout()); txtCenter = new TextField(20); btnNorth = new Button("North"); btnSouth = new Button("South"); btnEast = new Button("East"); btnWest = new Button("West"); add("North", btnNorth); add("South", btnSouth); add("East", btnEast); add("West", btnWest); add("Center", txtCenter); btnNorth.addActionListener(this); btnSouth.addActionListener(this); btnEast.addActionListener(this); btnWest.addActionListener(this); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub txtCenter.setText(((Button)arg0.getSource()).getLabel() + "is pressed!"); } }
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class BorderlayoutDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub FrameWindowDemo fw = new FrameWindowDemo("BorderLayoutDemo"); fw.setSize(200,250); fw.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); fw.setVisible(true); } }
import java.awt.Button; import java.awt.Frame; import java.awt.GridLayout; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class FrameWindowDemo extends Frame implements ActionListener { final int n = 7, m = 3; String s = ""; Button btn; TextField txtMessage; FrameWindowDemo(String title) { super(title); setLayout(new GridLayout(4, 3)); for(int i=1; i<=n; i+=3) { for(int j=0; j<m; ++j) { int k = i + j; btn = new Button("" + k); add(btn); btn.addActionListener(this); } } btn = new Button("0"); add(btn); btn.addActionListener(this); txtMessage = new TextField(5); add(txtMessage); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub s = s + ((Button)arg0.getSource()).getLabel(); txtMessage.setText(s); } }
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class GridLayoutDemo { public static void main(String[] args) { FrameWindowDemo fw = new FrameWindowDemo("GridLayoutDemo"); fw.setSize(300, 200); fw.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); fw.setVisible(true); } }