840 likes | 1.03k Views
第 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
E N D
第7章 Java与人机界面 LECTURER: 张祥 X.ZHANG@SEU.EDU.CN
Content • AWTand SwingIntroduction • SwingContainer(JFrame, JPanel) • Swing Components • Layout Manager • Eventand Event-based Programming • Menu CoSE Java 2009
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
通过 AWT ( 重量级组件) 创建 GUI java.awt 包 AWT Container 包含 AWT 容器组件 Component 以图形表示的对象允许用户交互 Layout Manager 定义 GUI 组件的位置 Graphics 使用 AWT 图形方法相对于窗口绘制图形 Font 创建并构造用于选择新字体的对象 Event 用于用户和系统交互 AWTIntroduction CoSE Java 2009
AWTIntroduction • Lessons of AWT • Not fully featured (a very short development cycle) • Heavy-weighted components CoSE Java 2009
SwingIntroduction • Overcome AWTs Shortage • Pure Java • Swing package is based on AWT • Swing is slower than AWT • javax.swing CoSE Java 2009
通过 Swing (light-weighted, pure java) 创建 GUI Javax.swing 包 Swing Container 包含 Swing 组件 Component 以图形表示的对象允许用户交互 Variable Appearance 可更换图形界面的风格 Java2D 使用Java2D绘制图形 SwingIntroduction CoSE Java 2009
AWT vs. Swing CoSE Java 2009
Swingis NOT Out! CoSE Java 2009
Self-study • AWT / Swing / SWT / JFaceComparation • Substance / JIDE • Reference • 《SWT/JFace in Action》 • 《Eclipse in Action》 CoSE Java 2009
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
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
SwingContainer JButton Frame Panel JComboBox add JCheckBox Component Container CoSE Java 2009
Cnotainer • JFrame • JPanel • JSplitPane • JScrollPane CoSE Java 2009
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
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
JSplitPane • To Split One Container into Two • Constructor • JSplitPane() • JSplitPane(int newOrientation) • JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) CoSE Java 2009
JScrollPane View CoSE Java 2009
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
JComponent • A Basic Class for All Swing Component Except For Top-level Container • Light-weighted CoSE Java 2009
JComponent • Methods CoSE Java 2009
JLabel JTextField JComboBox JCheckBox JTexArea JRadioButton JButton JComponents CoSE Java 2009
JLabel • Constructor • JLabel() • JLabel(String text) • JLabel(Icon image) • Methods • String getText()、 void setText(String text) • void setIcon(Icon icon) CoSE Java 2009
JTextField • Constructor • JTextField() • JTextField(String text) • Methods CoSE Java 2009
JTextArea • Constructor • JTextArea(); JTextArea(int rows, int columns) • JTextArea(String text); JTextArea(String text, int rows, int columns) • Methods CoSE Java 2009
JButton • Constructor • JButton(); JButton(Icon icon) • JButton(String text); JButton(String text, Icon icon) • Methods CoSE Java 2009
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
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
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
ButtonGroup • To Group JRadioButton • Constructor • ButtonGroup() • Methods • int getButtonCount() • void add(AbstractButton b) • void remove(AbstractButton b) CoSE Java 2009
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
Other JComponents • JComboBox • addItem() • get/setSelectedIndex • get/setSelectedItem • removeAllItems CoSE Java 2009
Other JComponents • JPasswordField • get/setEchoChar() • getPassword() CoSE Java 2009
Other JComponents • JSlider • get/setMinimum() • get/setMaximum() • get/setOrientation() CoSE Java 2009
Other JComponents • Jspinner • get/setValue() • getNextValue() • getPreviousValue() CoSE Java 2009
Other JComponents • JToolBar • JToolTip CoSE Java 2009
Other JComponents • JList • JTable CoSE Java 2009
Other JComponents • JTree CoSE Java 2009
Other JComponents • JFileChooser CoSE Java 2009