兩種經常使用的Container:java
Frame() Frame(String s) //建立標題欄爲字符串s的窗口
示例1:編程
import java.awt.*; public class test{ public static void main( String args[]) { Frame f = new Frame("My First Test"); f.setLocation(300, 300); f.setSize( 170,100); f.setBackground( Color.blue); f.setResizable(false); f.setVisible( true); } }
示例2:佈局
import java.awt.*; public class test { public static void main(String args[]) { MyFrame f1 = new MyFrame(100,100,200,200,Color.BLUE); MyFrame f2 = new MyFrame(300,100,200,200,Color.YELLOW); MyFrame f3 = new MyFrame(100,300,200,200,Color.GREEN); MyFrame f4 = new MyFrame(300,300,200,200,Color.MAGENTA); } } class MyFrame extends Frame{ static int id = 0; MyFrame(int x,int y,int w,int h,Color color){ super("MyFrame " + (++id)); setBackground(color); setLayout(null); setBounds(x,y,w,h); setVisible(true); } }
示例1:spa
import java.awt.*; public class TestPanel { public static void main(String args[]) { Frame f = new Frame("Java Frame with Panel"); Panel p = new Panel(null); f.setLayout(null); f.setBounds(300,300,500,500); f.setBackground(new Color(0,0,102)); p.setBounds(50,50,400,400); p.setBackground(new Color(204,204,255)); f.add(p); f.setVisible(true); } }
示例2:code
import java.awt.*; public class test{ public static void main( String args[]) { new MyFrame2("MyFrameWithPanel",300,300,400,300); } } class MyFrame2 extends Frame{ private Panel p1,p2,p3,p4; MyFrame2(String s,int x,int y,int w,int h){ super(s); setLayout(null); p1 = new Panel(null); p2 = new Panel(null); p3 = new Panel(null); p4 = new Panel(null); p1.setBounds(0,0,w/2,h/2); p2.setBounds(0,h/2,w/2,h/2); p3.setBounds(w/2,0,w/2,h/2); p4.setBounds(w/2,h/2,w/2,h/2); p1.setBackground(Color.BLUE); p2.setBackground(Color.GREEN); p3.setBackground(Color.YELLOW); p4.setBackground(Color.MAGENTA); add(p1);add(p2);add(p3);add(p4); setBounds(x,y,w,h); setVisible(true); } }
Button事件監聽orm
示例:對象
import java.awt.*; import java.awt.event.*; public class test { public static void main(String args[]) { Frame f = new Frame("Test"); Button b = new Button("Press Me!"); Monitor bh = new Monitor(); b.addActionListener(bh); f.add(b,BorderLayout.CENTER); f.pack(); //調整此窗口大小,以適應其組件的首先大小和佈局 f.setVisible(true); } } class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed"); } }
TextField類blog
TextField事件監聽繼承
import java.awt.*; import java.awt.event.*; public class TFPassword { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TFFrame2(); } } class TFFrame2 extends Frame { TFFrame2() { TextField tf = new TextField(); add(tf); tf.addActionListener(new TFActionListener2()); tf.setEchoChar('*'); pack(); setVisible(true); } } class TFActionListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { TextField tf = (TextField)e.getSource(); System.out.println(tf.getText()); tf.setText(""); } }
好處:接口
什麼時候使用:
示例:
import java.awt.*; public class TestPaint { public static void main(String[] args) { new PaintFrame().launchFrame(); } } class PaintFrame extends Frame { public void launchFrame() { setBounds(200,200,640,480); setVisible(true); } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.red); g.fillOval(50, 50, 30, 30); g.setColor(Color.green); g.fillRect(80,80,40,40); g.setColor(c); } }