1 / 84

第 7 章 Java 与人机界面

第 7 章 Java 与人机界面. LECTURER: 张祥 X.ZHANG@SEU.EDU.CN. Content. AWT and Swing Introduction Swing Container ( JFrame, JPanel ) Swing Components Layout Manager Event and Event-based Programming Menu. AWT Introduction. Abstract Window Tookit Basic UI component of Java

adriel
Download Presentation

第 7 章 Java 与人机界面

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第7章 Java与人机界面 LECTURER: 张祥 X.ZHANG@SEU.EDU.CN

  2. Content • AWTand SwingIntroduction • SwingContainer(JFrame, JPanel) • Swing Components • Layout Manager • Eventand Event-based Programming • Menu CoSE Java 2009

  3. AWTIntroduction • Abstract Window Tookit • Basic UI component of Java • Early Technology of Java • Limited component • Different appearance in different platform • No pop-up menu, scrolling pane • No clipboard, print ability, keyboard navigation… • java.awt CoSE Java 2009

  4. 通过 AWT ( 重量级组件) 创建 GUI java.awt 包 AWT Container 包含 AWT 容器组件 Component 以图形表示的对象允许用户交互 Layout Manager 定义 GUI 组件的位置 Graphics 使用 AWT 图形方法相对于窗口绘制图形 Font 创建并构造用于选择新字体的对象 Event 用于用户和系统交互 AWTIntroduction CoSE Java 2009

  5. AWTIntroduction • Lessons of AWT • Not fully featured (a very short development cycle) • Heavy-weighted components CoSE Java 2009

  6. SwingIntroduction • Overcome AWTs Shortage • Pure Java • Swing package is based on AWT • Swing is slower than AWT • javax.swing CoSE Java 2009

  7. 通过 Swing (light-weighted, pure java) 创建 GUI Javax.swing 包 Swing Container 包含 Swing 组件 Component 以图形表示的对象允许用户交互 Variable Appearance 可更换图形界面的风格 Java2D 使用Java2D绘制图形 SwingIntroduction CoSE Java 2009

  8. AWT vs. Swing CoSE Java 2009

  9. Swingis NOT Out! CoSE Java 2009

  10. Self-study • AWT / Swing / SWT / JFaceComparation • Substance / JIDE • Reference • 《SWT/JFace in Action》 • 《Eclipse in Action》 CoSE Java 2009

  11. SwingContainer • Basic Steps to Create GUIusing Swing: • Step 1: create a Frame (a window) • Step 2: create a Component (here is a button) CoSE Java 2009

  12. Swing容器 • Step 3: add the component into a Pane of Frame • Step 4: show the Frame (set its size and make it visible) CoSE Java 2009

  13. SwingContainer JButton Frame Panel JComboBox add JCheckBox Component Container CoSE Java 2009

  14. Cnotainer • JFrame • JPanel • JSplitPane • JScrollPane CoSE Java 2009

  15. JFrame • To Create a Window in Swing Program • Including the Rim, Title, Icon and Min/Max/Close • Constructor • JFrame() • JFrame(String title) CoSE Java 2009

  16. JPanel • Middle-level Container • Combining Small Light-weighted Component • Constructor • JPanel() • JPanel(boolean isDoubleBuffered) • JPanel(LayoutManager layout) • JPanel(LayoutManager layout, boolean isDoubleBuffered) CoSE Java 2009

  17. CoSE Java 2009

  18. JSplitPane • To Split One Container into Two • Constructor • JSplitPane() • JSplitPane(int newOrientation) • JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) CoSE Java 2009

  19. CoSE Java 2009

  20. JScrollPane View CoSE Java 2009

  21. JScrollPane • To Show Horizontal or Vertical Scroll Bar When Content is Out Of Range • Constructor • JScrollPane() • JScrollPane(Component view) • JScrollPane(Component view, int vsbPolicy, int hsbPolicy) • JScrollPane(int vsbPolicy, int hsbPolicy) CoSE Java 2009

  22. CoSE Java 2009

  23. JComponent • A Basic Class for All Swing Component Except For Top-level Container • Light-weighted CoSE Java 2009

  24. JComponent • Methods CoSE Java 2009

  25. JLabel JTextField JComboBox JCheckBox JTexArea JRadioButton JButton JComponents CoSE Java 2009

  26. JLabel • Constructor • JLabel() • JLabel(String text) • JLabel(Icon image) • Methods • String getText()、 void setText(String text) • void setIcon(Icon icon) CoSE Java 2009

  27. JTextField • Constructor • JTextField() • JTextField(String text) • Methods CoSE Java 2009

  28. JTextArea • Constructor • JTextArea(); JTextArea(int rows, int columns) • JTextArea(String text); JTextArea(String text, int rows, int columns) • Methods CoSE Java 2009

  29. JButton • Constructor • JButton(); JButton(Icon icon) • JButton(String text); JButton(String text, Icon icon) • Methods CoSE Java 2009

  30. Example • Create Following GUI • Create a JTextArea, where users can type text; • Create an noneditable JTextField; • Create a Jbutton. When the button is clicked, the text in the JTextArea will be copied into the JTextField CoSE Java 2009

  31. CoSE Java 2009

  32. CoSE Java 2009

  33. CoSE Java 2009

  34. CoSE Java 2009

  35. JCheckBox • Constructor • JCheckBox(Icon icon)、JCheckBox(Icon icon, boolean selected) • JCheckBox(String text)、JCheckBox(String text, boolean selected) • JCheckBox(String text, Icon icon)、JCheckBox(String text, Icon icon, boolean selected) • Methods • boolean isSelected() 、void setSelected(boolean b) • public ActionListener[] getActionListeners()、public void addActionListener(ActionListener l)、void removeActionListener(ActionListener l) CoSE Java 2009

  36. JRadioButton • Constructor • JRadioButton(Icon icon)、JRadioButton(Icon icon, boolean selected) • JRadioButton(String text)、JRadioButton(String text, boolean selected) • JRadioButton(String text, Icon icon)、JRadioButton(String text, Icon icon, boolean selected) • Methods • boolean isSelected() 、void setSelected(boolean b) • public ActionListener[] getActionListeners()、public void addActionListener(ActionListener l)、void removeActionListener(ActionListener l) CoSE Java 2009

  37. ButtonGroup • To Group JRadioButton • Constructor • ButtonGroup() • Methods • int getButtonCount() • void add(AbstractButton b) • void remove(AbstractButton b) CoSE Java 2009

  38. Example • Quiz • Write the Following GUI: • A question • Four options: A B C D • A Submit button • If correct, pop up “Correct”, or“Wrong”otherwise CoSE Java 2009

  39. CoSE Java 2009

  40. CoSE Java 2009

  41. CoSE Java 2009

  42. CoSE Java 2009

  43. Other JComponents • JComboBox • addItem() • get/setSelectedIndex • get/setSelectedItem • removeAllItems CoSE Java 2009

  44. Other JComponents • JPasswordField • get/setEchoChar() • getPassword() CoSE Java 2009

  45. Other JComponents • JSlider • get/setMinimum() • get/setMaximum() • get/setOrientation() CoSE Java 2009

  46. Other JComponents • Jspinner • get/setValue() • getNextValue() • getPreviousValue() CoSE Java 2009

  47. Other JComponents • JToolBar • JToolTip CoSE Java 2009

  48. Other JComponents • JList • JTable CoSE Java 2009

  49. Other JComponents • JTree CoSE Java 2009

  50. Other JComponents • JFileChooser CoSE Java 2009

More Related