Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java语言程序设计 马 皓 mah@pku.edu.cn.

Similar presentations


Presentation on theme: "Java语言程序设计 马 皓 mah@pku.edu.cn."— Presentation transcript:

1 Java语言程序设计 马 皓

2 第五章 图形用户界面设计 概述 事件处理 基本控制组件 布局设计 常用容器组件

3 概述 用户界面(User Interface) 用户与计算机系统(各种程序)交互的接口

4 User Interface Evolution
- Kai Fu Lee in 2003 Natural User Interface Natural Language Multimodal (speech, ink…) Personal Assistant Graphical User Interface 1995 Internet Hyperlinks Search Engines Digital Decade XML Web Services Smart devices 1990 GUI Multiple Windows Menus Command line 1985 PC

5 概述 Java GUI的发展 AWT (Java 1.0) Swing (Lightweight Components, Java 1.1)
AWT (Abstract Window Toolkit): 抽象窗口工具包 概念设计实现 (about 1 month) 字体设计(四种), 界面显示(二流水准) Swing (Lightweight Components, Java 1.1) "Swing" was the code name of the project that developed the new components Swing API (附加包, Add-on package) JFC (Java 2) JFC (Java Foundation Classes): Java基础类 JFC encompass a group of features to help people build graphical user interfaces (GUIs). JFC 是指包含在 Java 2 平台内的一整套图形和用户界面技术 JFC was first announced at the 1997 JavaOne developer conference

6 概述 JFC (Java Foundation Classes) AWT (Abstract Window Toolkit)
一些用户界面组件 (Component) 事件响应模型 (Event-handling model) 布局管理器 (Layout manager) 绘图和图形操作类, 如Shape、Font、Color类等 Swing Components (Swing组件, JFC的核心) a set of GUI components with a pluggable look and feel (包括已有的AWT组件(Button、Scrollbar、Label等)和更高层的组件 (如tree view、list box、tabbed panes等) The pluggable look and feel lets you design a single set of GUI components that can automatically have the look and feel of any OS platform (Microsoft Windows, Solaris, Macintosh). 基于Java 1.1 Lightweight UI Framework

7 概述 JFC (Java Foundation Classes)
Java 2D (advanced 2D graphics and imaging) Graphics? Imaging? Print Service 打印文档、图形、图像 设定打印属性和页面属性 发现打印机 (IPP, Internet Printing Protocol)

8 概述 JFC (Java Foundation Classes) Input Method Framework
text editing components to communicate with input methods and implement a well-integrated text input user interface 用Java语言开发输入法 Accessibility: 辅助功能,帮助伤残人士 screen readers, speech recognition systems, refreshable braille displays Drag & Drop Drag and Drop enables data transfer both across Java programming language and native applications, between Java programming language applications, and within a single Java programming language application.

9 图形用户界面的构成 什么是组件? 构成图形用户界面的元素,拿来即用
用图形表示(能在屏幕上显示,能和用户进行交互) Button、Checkbox、Scrollbar、Choice、Frame

10 图形用户界面的构成 一些特定的Java类 容器组件(Container): 可包含其他组件 非容器组件: 必须要包含在容器中
java.awt包 javax.swing包 容器组件(Container): 可包含其他组件 顶层容器: Applet, Dialog, Frame, Window 一般用途容器: Panel, ScrollPane 特定用途容器: InternalFrame 非容器组件: 必须要包含在容器中 Button, Checkbox, Scrollbar, Choice, Canvas

11 图形用户界面的构成 AWT组件  java.awt包 Component
Button, Canvas, Checkbox, Choice, Label, List, Scrollbar TextComponent TextArea TextField Container Panel Window ScrollPane Dialog Frame MenuComponent MenuBar MenuItem

12 图形用户界面的构成 Swing组件  javax.swing包 java.awt.Component
|-java.awt.Container |-java.awt.Window |-java.awt.Frame |-javax.swing.JFrame |-javax.swing.JComponent |-JComboBox, JFileChooser, JInternalFrame JLabel, JList, JMenuBar, JOptionPane, JPanel JPopupMenu, JProgressBar, JScrollBar JScrollPane, JSeparator, JSlider, JSpinner JSplitPane, JTabbedPane, JTable JTextComponent, JToolBar, JTree等

13 图形用户界面的实现 选取组件 设计布局 响应事件 应用原则 Swing比AWT提供更全面、更丰富的图形界面设计功能
Java 2平台支持AWT组件,但鼓励用Swing组件 主要讲述AWT和Swing的图形界面设计

14 图形用户界面的实现 简单实例 import java.awt.*; import java.awt.event.*;
public class HelloWorldAWT { public static void main(String[] args) { Frame f = new Frame("AWT1"); Label label = new Label("Hello!"); f.add(label); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(200, 200); f.setVisible(true); } 图形用户界面的实现 简单实例 import javax.swing.*; import java.awt.event.*; public class HelloWorldSwing { public static void main(String[] args) { JFrame f = new JFrame(“Swing1"); JLabel label = new JLabel("Hello!"); f.getContentPane().add(label); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(200, 200); f.setVisible(true); }

15 第五章 图形用户界面设计 概述 事件处理 基本控制组件 布局设计 常用容器组件

16 事件处理 界面设计 (静态) 界面动起来 ! 事件处理机制 如何实现 通过事件触发对象的响应机制 事件? 鼠标移动、鼠标点击、键盘键入等
事件源 事件对象 事件监听者 如何实现 实现(implements)事件监听接口(interface)产生一个监听器对象(Listener) 监听谁? 将该监听器对象注册到组件对象中 编写事件响应方法

17 事件处理 java.awt.event.ActionListener (interface)
public void actionPerformed(ActionEvent e) javax.swing.JButton (class) public void addActionListener(ActionListener l) 事件处理 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Beeper extends JApplet implements ActionListener { JButton button; public void init() { button = new JButton("Click Me"); getContentPane().add(button, BorderLayout.CENTER); button.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println(“Click me once”);

18 事件处理 事件分类 Act that results in the event Listener type
User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener

19 事件处理 事件分类 interface java.awt.event.ActionListener
public void actionPerformed(ActionEvent e) interface java.awt.event.WindowListener public void windowOpened(WindowEvent e) public void windowClosing(WindowEvent e) 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)

20 事件处理 事件分类 interface java.awt.event.MouseListener
public void mouseClicked(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) interface java.awt.event.MouseMotionListener public void mouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged public void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed

21 事件处理 鼠标事件 public class MouseEventDemo ... implements MouseListener {
//Register for mouse events on blankArea(TextArea) and applet blankArea.addMouseListener(this); } public void mousePressed(MouseEvent e) { saySomething("Mouse pressed; # of clicks: “ + e.getClickCount(), e); } public void mouseReleased(MouseEvent e) { saySomething("Mouse released; # of clicks: "+ e.getClickCount(), e); } public void mouseEntered(MouseEvent e) { saySomething("Mouse entered", e); } public void mouseExited(MouseEvent e) { saySomething("Mouse exited", e); } public void mouseClicked(MouseEvent e) { saySomething("Mouse clicked (# of clicks: “ + e.getClickCount() + ")", e); } void saySomething(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " detected on “ + e.getComponent().getClass().getName() + "." + newline); } 事件处理 鼠标事件

22 事件处理 多个监听器(Listener)多个组件
public class MultiListener ... implements ActionListener { ... button1.addActionListener(this); button2.addActionListener(this); button2.addActionListener(new Eavesdropper(bottomTextArea)); } public void actionPerformed(ActionEvent e) { topTextArea.append(e.getActionCommand() + newline); } class Eavesdropper implements ActionListener { { myTextArea.append(e.getActionCommand() + newline); }

23 第五章 图形用户界面设计 概述 事件处理 基本控制组件 布局设计 常用容器组件

24 AWT组件 (java.awt.*) Button Canvas Panel Applet Container ScrollPane
Frame Choice Window Dialog Component CheckBox FileDialog Label List TextField TextComponent TextArea Scrollbar

25 基本控制组件 使用步骤: Swing组件(javax.swing.*) 创建基本控制组件类的对象,指定对象属性;
将组件对象加入到制定容器的适当位置(布局设计); 创建事件对象的监听者。 Swing组件(javax.swing.*)

26 按钮和标签 按钮(Button) 创建按钮 常用方法 事件响应 public Button()
public Button(String label) 常用方法 public String getLabel() public void setLabel(String label) public void setActionCommand(String s) public String getActionCommand(String s) 事件响应 java.awt.event.ActionListener(接口) void actionPerformed(ActionEvent e)

27 按钮和标签 标签(Label) 创建标签 常用方法 事件响应 public Label() public Label(String s)
public Label(String s, int alignment) 常用方法 public String getText() public void setText(String s) public void setAlignment(int alignment) 事件响应 不引发事件

28 使用标签的例子 import java.awt.*; import java.applet.*;
public class Exam5_3 extends Applet { Label lab1, lab2; TextField text1, text2; public void init() { lab1 = new Label(“输入姓名”); lab2 = new Label(“输入年龄”); lab1.setBackground(Color.red); lab2.setBackground(Color.green); text1 = new TextField(10); text2 = new TextField(10); add(lab1); add(text1); add(lab2); add(text2); }

29 使用标签的例子 <Html> <Body>
<Applet code="Exam5_3.class" width=500 height=300> </Applet> </Body> </Html>

30 文本框和文本区 文本框(TextField) TextComponent类的子类 创建文本框 常用方法 事件响应
public TextField() public TextField(int size) public TextField(String s) public TextField(String s, int size) 常用方法 public void setText(String s) public String getText() public void setEchochar(char c) public void setEditable(boolean b) 事件响应 java.awt.event.TextListener(接口) java.awt.event.ActionListener(接口)

31 文本框和文本区 文本区(TextArea) TextComponent类的子类 创建文本区 常用方法 事件响应
public TextArea() public TextArea(String s) public TextArea(int rows, int columns) public TextArea(String s, int rows, int columns) public TextArea(String s, int rows, int columns, int scrollbars) SCROLLBARS_BOTH, SCROLLBARS_NONE SCROLLBARS_VERTICAL_ONLY SCROLLBARS_HORIZONTAL_ONLY 常用方法 public void append(String s) public void insert(String s, int index) pubilc void replaceRange(String s, int start, int end) 事件响应 java.awt.event.TextListener(接口) void textValueChanged(TextEvent e)

32 使用文本框的例子 import java.awt.*; import java.awt.event.*;
import java.applet.*; public class Exam5_4 extends Applet implements ActionListener{ Label lab1, lab2, lab3; TextField text1, text2, text3; String str; int i; float f; public void init() { lab1 = new Label(“输入整形数: ”); add(lab1); text1 = new TextField(“0”, 30); text1.addActionListener(this); add(text1); lab2 = new Label(“输入浮点数: ”); add(lab2); text2 = new TextField(“0.0”, 30); text2.addActionListener(this); add(text2); lab3 = new Label(“输入字符串: ”); add(lab3); text3 = new TextField(“0.0”, 30); text3.addActionListener(this); add(text3); }

33 使用文本框的例子 public void actionPerformed(ActionEvent e) {
i = Integer.parseInt(text1.getText()); f = (Float.valueOf(text2.getText())).floatValue(); str = text3.getText(); repaint(); } public void paint(Graphics g) { g.drawString(“整形数=” + i, 20, 120); g.drawString(“浮点数=” + f, 20, 150); g.drawString(“字符串=” + str, 20, 180); <Html> <Body> <Applet code="Exam5_4.class" width=400 height=300> </Applet> </Body> </Html>

34 单复选框和列表 复选框(Checkbox) 创建复选框 常用方法 事件响应 public Checkbox()
public Checkbox(String s) public TextField(String s, boolean state) 常用方法 public boolean getState() public void setState(boolean b) public void setLabel(String s) public String getLabel() 事件响应 java.awt.event.ItemListener(接口) void itemStateChanged(ItemEvent e)

35 单复选框和列表 单选按钮组(CheckboxGroup) 创建单选按钮组 常用方法 事件响应
public Checkbox(String label, boolean state, CheckboxGroup group) public Checkbox(String label, CheckboxGroup group, boolean state) 常用方法 与复选框相同 事件响应

36 单复选框和列表 列表(List) 创建列表 常用方法 事件响应 public List() public List(int n)
public List(int n, boolean b) 常用方法 public void add(String s) public void add(String s, int n) public void remove(int n) public void removeAll() public int getSelectedIndex() public String getSelectedItem() 事件响应 java.awt.event.ItemListener(接口) java.awt.event.ActionListener(接口)

37 下拉列表和滚动条 下拉列表(Choice) 创建下拉列表 常用方法 事件响应 public Choice()
public int getSelectedIndex() public String getSelectedItem() public void select(int index) public void select(String item) public void add(String s) public void add(String s, int index) public void remove(int index) public void remove(String item) public void removeAll() 事件响应 java.awt.event.ItemListener(接口)

38 下拉列表和滚动条 滚动条(Scrollbar) 创建滚动条 常用方法 事件响应
public Scrollbar(int orientation, int value, int visible, int minimum, int maximum) 常用方法 public void setUnitIncrement(int n) public void setBlockIncrement(int n) public int getUnitIncrement() public int getBlockIncrement() public int getValue() 事件响应 java.awt.event.AdjustmentListener(接口) void adjustmentValueChanged(AdjustmentEvent e)

39 使用下列列表的例子 import java.awt.*; import java.awt.event.*;
import java.applet.*; public class Exam5_8 extends Applet implements ItemListener { Choice cho; TextField text; public void init() { text = new TextField(10); cho = new Choice(); cho.add(“red”); cho.add(“yellow”); cho.add(“green”); cho.add(“blue”); add(cho); add(text); cho.addItemListener(this); } public void itemStateChanged(ItemEvent e) { if(e.getItemSelectable() == cho) { String s = cho.getSelectedItem(); text.setText(s);

40 使用下列列表例子 <Html> <Body>
<Applet code="Exam5_8.class" width=400 height=300> </Applet> </Body> </Html>

41 画布 画布(Canvas) 创建画布 常用方法 事件响应 public Canvas() public void setSize()
public void paint(Graphics g) 事件响应 java.awt.event.MouseMotionListener(接口) java.awt.event.MouseListener(接口) java.awt.event.KeyListener(接口)

42 第五章 图形用户界面设计 概述 事件处理 基本控制组件 布局设计 常用容器组件

43 布局管理 决定组件在界面中所处的位置和大小 六种布局管理器(Layout Manager) 两种简单布局 两种特定用途布局 两种灵活布局
java.awt.FlowLayout (JDK 1.0) java.awt.GridLayout (JDK 1.0) 两种特定用途布局 java.awt.BorderLayout (JDK 1.0) java.awt.CardLayout (JDK 1.0) 两种灵活布局 java.awt.GridBagLayout (JDK 1.0) javax.swing.BoxLayout

44 布局管理 FlowLayout (java.awt.FlowLayout) 所有组件从左往右排成一行 一行排满后转到下一行从左往右排
public void pack() Causes this Window to be sized to fit the perferred size and layouts of its subcomponents FlowLayout (java.awt.FlowLayout) 所有组件从左往右排成一行 一行排满后转到下一行从左往右排 居中、左对齐、右对齐 import java.awt.*; import javax.swing.*; public class FlowWindow extends JFrame { public FlowWindow() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5")); } public static void main(String args[]) { FlowWindow win = new FlowWindow(); win.setTitle("FlowLayout"); win.pack(); win.setVisible(true);

45 布局管理 GridLayout (java.awt.GridLayout)
将空间划分为由行和列组成的网格单元,每个单元放一个组件,网格单元大小相同(宽度和高度) 指定行数和列数 public GridLayout(int rows, int cols) rows and cols can be zero, which means that any number of objects can be placed in a row or in a column import java.awt.*; import javax.swing.*; public class GridWindow extends JFrame { public GridWindow() { Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(0,2)); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5")); } public static void main(String args[]) { GridWindow win = new GridWindow(); win.setTitle("FlowLayout"); win.pack(); win.setVisible(true);

46 布局管理 BorderLayout (java.awt.BorderLayout)
BorderLayout is the default layout manager for every content pane 上北、下南、左西、右东、中 Container contentPane = getContentPane(); //contentPane.setLayout(new BorderLayout()); contentPane.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); contentPane.add(new JButton("2 (CENTER)"), BorderLayout.CENTER); contentPane.add(new JButton("Button 3 (WEST)"), BorderLayout.WEST); contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), BorderLayout.SOUTH); contentPane.add(new JButton("Button 5 (EAST)"), BorderLayout.EAST);

47 布局管理 CardLayout (java.awt.CardLayout) 两个或多个组件共享相同的显示空间,在不同的时间显示不同的组件

48 布局管理 GridBagLayout (java.awt.GridBagLayout) 最精细、最灵活的布局管理
将空间划分为由行和列组成的网格单元,每个单元放一个组件,网格单元大小可以不同(宽度和高度)

49 布局管理 BoxLayout (javax.swing.BoxLayout) 将组件放在一行或一列 容器的嵌套 (面板的嵌套,相互包含)
JPanel jpv = new JPanel(); jpv.setLayout(new BoxLayout(jpv, BoxLayout.Y_AXIS)); for(int i = 0; i < 5; i++) jpv.add(new JButton("" + i)); JPanel jph = new JPanel(); jph.setLayout(new BoxLayout(jph, BoxLayout.X_AXIS)); jph.add(new JButton("" + i)); Container cp = getContentPane(); cp.add(BorderLayout.EAST, jpv); cp.add(BorderLayout.SOUTH, jph);

50 第五章 图形用户界面设计 概述 事件处理 基本控制组件 布局设计 常用容器组件

51 概述 容器 可包含其他组件和容器 Container类的子类 无边框容器: Panel, Applet
有边框容器: Window, Frame, Dialog, FieldDialog 可自动处理滚动操作的容器: Scrollpane Container Panel ScrollPane Window Frame Dialog Applet FileDialog

52 容器 常用方法 添加组件: add() 获取制定的组件 从容器中移出组件 设置容器布局: setLayout()
getComponent(int x, int y) getComponent(int index) 从容器中移出组件 remove(Component c) remove(int index) removeAll() 设置容器布局: setLayout()

53 容器 面板(Panel) 无边框容器 顺序布局(FlowLayout) Applet子类

54 窗口和菜单 java.awt.Window: 最顶层容器 java.awt.Frame: 有边框容器 Window(Frame f)
show() BorderLayout布局 java.awt.Frame: 有边框容器 构造方法 Frame() Frame(String title) 常用方法 getTitle() setTitle(String s) setVisible(boolean b) setBounds(int a, int b, int width, int height) setBackground(Color c) pack() setSize(int width, int height) dispose() add() remove()

55 使用Frame容器的例子 import java.awt.*; import java.awt.event.*;
public class Exam5_18 { public static void main(String args[]) { MyFrame mf = new MyFrame(); } class MyFrame extends Frame implements ActionListener, MouseListener, WindowListener { Button but; String str; String mouseClickCnt = “单击”; Dimension currentPos = new Dimension(); int clickCnt = 0; MyFrame() { super(“我制作的窗口”); but = new Button(“按钮”); setLayout(new FlowLayout()); add(but); but.addActionListener(this); addMouseListener(this); addWindowListener(this); pack(); show();

56 使用Frame容器的例子 public void paint(Graphics g) {
str = “单击了” + clickCnt + “次按钮”; g.drawString(str, 10 ,40); g.drawString(“鼠标” + mouseClickCnt + “位置:(” + currentPos.width + “,” currentPos.height + “)”, 10, 70); } public void actionPerformed(ActionEvent e) { if(e.getSource() == but) { clickCnt ++; repaint(); public void mouseClicked(MouseEvent e) { currentPos.width = e.getX(); currentPos.height = e.getY(); if(e.getClickCount() == 1) mouseClickCnt = “单击”; else mouseClickCnt = “双击”;

57 使用Frame容器的例子 public void mousePressed(MouseEvent e) { ; }
public void mouseReleased(MouseEvent e) { ; } public void mouseEntered(MouseEvent e) { ; } public void mouseExited(MouseEvent e) { ; } public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } public void windowOpened(WindowEvent e) { ; } 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) { ; }

58 菜单组件 java.awt.MenuBar类 java.awt.Menu类 java.awt.MenuItem类
setMenuBar(菜单对象) java.awt.Menu类 java.awt.MenuItem类 java.awt.CheckboxMenuItem类 java.awt.PopupMenu类 MenuComponent MenuBar MenuItem CheckboxMenuItem Menu PopupMenu

59 使用菜单组件的例子 import java.awt.*; import java.awt.event.*;
public class Exam5_19 extends Frame implements ActionListener, ItemListener { TextField text; public Exam5_19() { super(“我的菜单窗口”); setSize(300, 200); } public void init() { MenuBar myB = new MenuBar(); setMenuBar(myB); Menu m1 = new Menu(“文件”); m1.add(new MenuItem(“打开”)); MenuItem m11 = new MenuItem(“保存”); m11.setEnabled(false); m1.add(m11); m1.addSeparator(); m1.add(“退出”); m1.addActionListener(this); myB.add(m1);

60 使用菜单组件的例子 Menu m2 = new Menu(“编辑”); m2.add(“复制”);
m21.addActionListener(this); m2.add(m21); m2.addSeparator(); CheckboxMenuItem mycmi = new CheckboxMenuItem(“全选”); mycmi.addItemListener(this); m2.add(mycmi); m2.addActionListener(this); myB.add(m2); Menu m3 = new Menu(“帮助”); m3.add(“关于”); m3.addActionListener(this); myB.setHelpMenu(m3); text = new TextField(); add(“South”, text); }

61 使用菜单组件的例子 public static void main(String args[]) {
Exam5_19 myMenu = new Exam5_19(); myMenu.init(); myMenu.setVisible(true); } public void itemStateChanged(ItemEvent e) { text.setText(“状态改变”); public void actionPerformed(ActionEvent e) { text.setText(e.getActionCommand()); if (e.getActionCommand() == “退出”) System.exit(0);

62 对话框 java.awt.Dialog类 操作步骤 有边框和标题,可对立使用的容器 Dialog(Frame f)
Dialog(Frame f, boolean b) Dialog(Frame f, String s) Dialog(Frame f, String s, boolean b) setTitle()/getTitle() setModal()/setSize()/setVisible() 操作步骤 创建一个窗口类 创建一个对话框类 设置对话框大小 创建主类,启动和初始化窗口和对话框类

63 对话框 java.awt.FileDialog类 Dialog类的子类 构造方法 常用方法 FileDialog(Frame f)
FileDialog(Frame f, String s) FileDialog(Frame f, String s, int m) 常用方法 getDirectory() setDirectory() setFile()

64 关于Swing的设计

65 界面设计 设计流程 顶层容器 内容面板 在内容面板中添加组件 JFrame对象主窗口 JDialog对象二级窗口
JApplet对象applet程序在浏览器窗口中的显示区域 内容面板 JFrame f = new JFrame(“Swing1"); JLabel label = new JLabel("Hello!"); f.getContentPane().add(label); 面板的嵌套 (面板包含面板) 设计布局 在内容面板中添加组件

66 界面设计 Frame 内容面板 内容面板 内容面板 TextField Combox Slider TextField Combox

67 界面设计

68 应用实例 应用实例—菜单的构造 JMenuBar menuBar; JMenu menu, submenu;
JMenuItem menuItem; JCheckBoxMenuItem cbMenuItem; JRadioButtonMenuItem rbMenuItem; cbMenuItem = new JCheckBoxMenuItem( "A check box menu item"); menu.add(cbMenuItem); "Another one"); menu.addSeparator(); submenu = new JMenu("A submenu"); menuItem = new JMenuItem( "An item in the submenu"); submenu.add(menuItem); menuItem = new JMenuItem("Another item"); menu.add(submenu); ButtonGroup group = new ButtonGroup(); rbMenuItem = new JRadioButtonMenuItem( "A radio button menu item"); rbMenuItem.setSelected(true); group.add(rbMenuItem); menu.add(rbMenuItem); "Another one"); menu.addSeparator(); menu = new JMenu("Another Menu"); menuBar.add(menu); menuItem = new JMenuItem( "A text-only menu item"); menu.add(menuItem); menuItem = new JMenuItem("Both text and icon", new ImageIcon("images/middle.gif")); menuItem = new JMenuItem(new ImageIcon("images/middle.gif")); menu.addSeparator(); menuBar = new JMenuBar(); menu = new JMenu("A Menu"); menuBar.add(menu);

69 应用实例 应用实例—绘图(java.awt.Graphics类) import java.awt.*;
import java.applet.Applet; public class DrawGraph extends Applet { public void paint (Graphics g) { g.drawLine(30, 5, 40, 5); g.drawRect(40, 10, 50, 20); g.fillRect(60, 30, 70, 40); g.drawRoundRect(110, 10, 130, 50, 30,30); g.drawOval(150, 120, 70, 40); g.fillOval(190, 160, 70, 40); g.drawString("Hello", 100, 200); int[] x = {30, 50, 65, 119, 127}; int[] y = {100, 140, 127, 169, 201}; g.drawPolygon(x, y, 5); }

70 第五章 结束 !


Download ppt "Java语言程序设计 马 皓 mah@pku.edu.cn."

Similar presentations


Ads by Google