Java图形界面编程
AWT
AWT(Abstract Window Toolkit),抽象窗口工具包,SUN公司提供的用于图形界面编程(GUI)的类库。基本的AWT库处理用户界面元素的方法是把这些元素的创建和行为委托给每个目标平台上(Windows、Unix、Macintosh等)的本地GUI工具进行处理。例如:如果我们使用AWT在一个Java窗口中放置一个按钮,那么实际上使用的是一个具有本地外观和感觉的按钮。这样,从理论上来说,我们所编写的图形界面程序能运行在任何平台上,做到了图形界面程序的跨平台运行
布局管理器
容器里组件的位置和大小是由布局管理器来决定的。容器对布局管理器的特定实例保持一个引用。当容器需要定位一个组件时,它将调用布局管理器来完成。当决定一个组件的大小时,也是如此。
在AWT中,给我们提供了五种布局管理器:
BorderLayout
FlowLayout
GridLayout
CardLayout
GridBagLayout
/** * @(#)MyFrame.java * * * @author * @version 1.00 2012/5/14 */ import java.awt.*; import java.awt.event.*; public class MyFrame { public static void main (String[] args) { Frame f=new Frame("mybole"); f.setSize(600, 400); f.setLocation(100, 100); f.setBackground(Color.blue); Button btn1=new Button("hehe"); Button btn2=new Button("South"); Button btn3=new Button("West"); Button btn4=new Button("East"); Button btn5=new Button("Center"); //f.setLayout(new BorderLayout(10, 10)); //f.setLayout(new FlowLayout(FlowLayout.LEFT)); f.setLayout(new GridLayout(2, 3, 10, 10)); f.add(btn1, "North"); f.add(btn2, "South"); f.add(btn3, "West"); f.add(btn4, "East"); f.add(btn5, "Center"); //f.addWindowListener(new MyWindowListener()); //f.addWindowListener(new HisWindowListener()); /*f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } });*/ f.show(); } } class MyWindowListener implements WindowListener{ public void windowOpened(WindowEvent e){ } public void windowClosing(WindowEvent e){ System.exit(0); } public void windowClosed(WindowEvent e){ } public void windowIconified(WindowEvent e){ } public void windowDeiconified(WindowEvent e){ } public void windowActivated(WindowEvent e){ } public void windowDeactivated(WindowEvent e){ } } class HisWindowListener extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } }
/** * @(#)YourFrame.java * * * @author * @version 1.00 2012/5/14 */ import java.awt.*; import java.awt.event.*; public class YourFrame extends Frame{ private Panel borderPanel; private Panel flowPanel; private Panel gridPanel; private Panel cardPanel; public void setBorderLayoutPanel(){ borderPanel=new Panel(); borderPanel.setLayout(new BorderLayout()); Button btn1=new Button("North"); Button btn2=new Button("South"); Button btn3=new Button("West"); Button btn4=new Button("East"); Button btn5=new Button("Center"); borderPanel.add(btn1, BorderLayout.NORTH); borderPanel.add(btn2, BorderLayout.SOUTH); borderPanel.add(btn3, BorderLayout.WEST); borderPanel.add(btn4, BorderLayout.EAST); borderPanel.add(btn5, BorderLayout.CENTER); } public void setFlowLayoutPanel(){ flowPanel=new Panel(); Button btn1=new Button("mybole"); btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ ((Button)e.getSource()).setLabel("winxin"); } }); Button btn2=new Button("winsun"); flowPanel.add(btn1); flowPanel.add(btn2); } public void setGridLayoutPanel(){ gridPanel=new Panel(); gridPanel.setLayout(new GridLayout(2,2, 5,5)); Button btn1=new Button("Button1"); Button btn2=new Button("Button2"); Button btn3=new Button("Button3"); Button btn4=new Button("Button4"); gridPanel.add(btn1); gridPanel.add(btn2); gridPanel.add(btn3); gridPanel.add(btn4); } public void setCardLayoutPanel(){ final CardLayout cl=new CardLayout(); cardPanel=new Panel(); cardPanel.setLayout(cl); Button btn1=new Button("black A"); Button btn2=new Button("red K"); Button btn3=new Button("green N"); ActionListener al=new ActionListener(){ public void actionPerformed(ActionEvent e){ cl.next(cardPanel); } }; cardPanel.add(btn1, "1"); cardPanel.add(btn2, "2"); cardPanel.add(btn3, "3"); btn1.addActionListener(al); btn2.addActionListener(al); btn3.addActionListener(al); } public YourFrame(String title){ super(title); setSize(600, 400); setLocation(100, 100); setBorderLayoutPanel(); setFlowLayoutPanel(); setGridLayoutPanel(); setCardLayoutPanel(); setLayout(new GridLayout(2, 2)); add(borderPanel); add(flowPanel); add(gridPanel); add(cardPanel); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public static void main (String[] args) { YourFrame yf=new YourFrame("http://www.baidu.com"); yf.show(); } }
/** * @(#)HisFrame.java * * * @author * @version 1.00 2012/5/14 */ import java.awt.*; import java.awt.event.*; import java.io.*; public class HisFrame { public static void main (String[] args) { final Frame f=new Frame("http://www.baidu.com"); f.setSize(600, 400); f.setLocation(100, 100); //TextField tf=new TextField(20); //f.add(tf, "North"); final TextArea ta=new TextArea(); f.add(ta); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); MenuBar mb=new MenuBar(); Menu m1=new Menu("File"); Menu m2=new Menu("Edit"); MenuItem mi1=new MenuItem("New"); MenuItem mi2=new MenuItem("Open"); mi2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ FileDialog fd=new FileDialog(f, "Open File Dialog", FileDialog.LOAD); fd.show(); String strFile=fd.getDirectory() + fd.getFile(); if(strFile != null){ try{ FileInputStream fis=new FileInputStream(strFile); byte[] buf=new byte[10*1024]; int len=fis.read(buf); fis.close(); ta.append(new String(buf, 0, len)); }catch(Exception ex){ ex.printStackTrace(); } } } }); MenuItem mi3=new MenuItem("Save"); MenuItem mi4=new MenuItem("Exit"); MenuItem mi5=new MenuItem("Copy"); MenuItem mi6=new MenuItem("Paste"); m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi4); m2.add(mi5); m2.add(mi6); mb.add(m1); mb.add(m2); f.setMenuBar(mb); f.show(); } }
/** * @(#)SwingTest.java * * * @author * @version 1.00 2012/5/15 */ import javax.swing.*; public class SwingTest{ public static void main (String[] args) { JFrame jf=new JFrame("mybole"); jf.setSize(600, 400); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton btn=new JButton("nihao"); jf.getContentPane().add(btn, "West"); jf.show(); } }