540 likes | 790 Views
Java 语言编程技术. 第7章 GUI 设计. GUI 设计. GUI 的组成元素 applet GUI 布局( layout) 事件处理 Swing components 的使用 Beans. Swing 组件分类. Top-level containers JFram e, JApple t, JWindo w, and JDialo g Lightweight components JButto n, JPane l, and JMen u 图示: components_pics.html. Buttons. JButton
E N D
Java 语言编程技术 第7章 GUI设计
GUI设计 • GUI的组成元素 • applet • GUI布局(layout) • 事件处理 • Swing components的使用 • Beans Java 语言编程技术
Swing 组件分类 • Top-level containers • JFrame, JApplet, JWindow, and JDialog • Lightweight components • JButton, JPanel, and JMenu • 图示:components_pics.html Java 语言编程技术
Buttons • JButton • JToggleButton • JCheckBox • JRadioButton • (JMenuItem) • 例:Buttons.java • 例:ButtonGroups.java AbstractButton ActionListener Java 语言编程技术
Check boxes & Radio buttons • public JCheckBox(String text, Icon icon, boolean selected) • 例 :CheckBoxes.java • 例 :RadioButtons.java • ItemListener • public void itemStateChanged(ItemEvent e) Java 语言编程技术
Combo boxes (drop-down lists) • Combo boxes (drop-down lists) 例:ComboBoxes.java • public void addItem(Object anObject) • setEditable(true) • ComboBoxEditor • ListCellRenderer • 例 :CustomComboBoxDemo.java Java 语言编程技术
Icons • Icon接口 • ImageIcon • ImageIcon(String filename) • AbstractButton • public void setIcon(Icon defaultIcon) • public void setDisabledIcon(Icon disabledIcon) • public void setPressedIcon(Icon pressedIcon) • public void setRolloverIcon(Icon rolloverIcon) • 例 :Faces.java(application方式运行) • Jcomponent • public void setToolTipText(String text) Java 语言编程技术
Borders • Jcomponent • public void setBorder(Border border) • javax.swing.border • Border接口 • AbstractBorder • BevelBorder • EmptyBorder • LineBorder • TitledBorder • … … Java 语言编程技术
Borders • 例:Borders.java • javax.swing.BorderFactory: • public static Border createLineBorder() • public static Border createRaisedBevelBorder() • public static Border createLoweredBevelBorder() • … … • 例: BorderDemo.java Java 语言编程技术
TextComponent • JTextComponent • JTextField • JPasswordField • JTextArea • JEditorPane • JTextPane Java 语言编程技术
TextComponent • JTextComponent • setEditable(boolean); • setDocument(Document); • setText(String); • selectAll(); • String getSelectedText(); • setSelectionStart(); Java 语言编程技术
TextComponent • Document接口 • 例 TextFields.java TextPane.java TextSamplerDemo.java Java 语言编程技术
JScrollPanes public JScrollPane(Component view, int vsbPolicy, int hsbPolicy) • 实现ScrollPaneConstants接口: • HORIZONTAL_SCROLLBAR_AS_NEEDED • HORIZONTAL_SCROLLBAR_NEVER • HORIZONTAL_SCROLLBAR_ALWAYS • VERTICAL_SCROLLBAR_AS_NEEDED • VERTICAL_SCROLLBAR_NEVER • VERTICAL_SCROLLBAR_ALWAYS • 例:JScrollPanes.java Java 语言编程技术
List boxes • public JList(ListModel dataModel) • 例 :List.java • DefaultListModel: • addElement(Object obj) • add(int index,Object element) • public JList(Object[] listData) String[] data = {"one", "two", "free"}; JList dataList = new JList(data); Java 语言编程技术
List boxes • setSelectionMode(ListSelectionModel.SINGLE_SELECTION); • SINGLE_SELECTION • SINGLE_INTERVAL_SELECTION • MULTIPLE_INTERVAL_SELECTION • ListSelectionListener • public void valueChanged(ListSelectionEvent e) Java 语言编程技术
Tabbed panes • JTabbedPane(int tabPlacement) • addChangeListener(ChangeListener l) • stateChanged(ChangeEvent e) • addTab(String title, Icon icon, Component component, String tip) • 例 :TabbedPane1.java Java 语言编程技术
Message boxes • JOptionPane • showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) • int showConfirmDialog • int showOptionDialog • String showInputDialog • Object showInputDialog • 例 :MessageBoxes.java Java 语言编程技术
Message boxes • showXXXDialog参数 • Component parentComponent • Object message • String title • int optionType • DEFAULT_OPTION • YES_NO_OPTION • YES_NO_CANCEL_OPTION • OK_CANCEL_OPTION Java 语言编程技术
Message boxes • showXXXDialog参数 • int messageType • PLAIN_MESSAGE (no icon) • ERROR_MESSAGE • INFORMATION_MESSAGE • WARNING_MESSAGE • QUESTION_MESSAGE • Icon icon • Object[] options • Object initialValue Java 语言编程技术
Message boxes • return • YES_OPTION • NO_OPTION • CANCEL_OPTION • OK_OPTION • CLOSED_OPTION Java 语言编程技术
Message boxes • JDialog JOptionPane optionPane = new JOptionPane( "Do you understand?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); JDialog dialog = new JDialog(frame, "Click a button",true); dialog.setContentPane(optionPane); dialog.setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE); Java 语言编程技术
Menus Java 语言编程技术
Menus • JApplet 、JFrame、JDialog: • setJMenuBar() • JMenubar+JMenu+JMenuItem JMenuBar mb = new JMenuBar(); JMenu menu = new JMenu(“File"); menu.add(new JMenuItem(“Open)); mb.add(menu); Java 语言编程技术
Menus • JMenuItem • JMenu • JCheckBoxMenuItem——ItemListener • JRadioButtonMenuItem • ActionListener • 例:SimpleMenus.java Menus.java Java 语言编程技术
Menus new JMenuItem("A menu item",KeyEvent.VK_T); menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); • 例:MenuLookDemo.java new JMenuItem("Both text and icon", new ImageIcon("images/middle.gif")); menuItem.setMnemonic(KeyEvent.VK_B); menu.addSeparator(); Java 语言编程技术
Menus • JPopupMenu • add(JMenuItem menuItem) • add(String s) • show(Component invoker,int x,int y) • 例:Popup.java • 例: MenuGlueDemo.java menuBar.add(Box.createHorizontalGlue()); • 例: MenuLayoutDemo.java menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS)); Java 语言编程技术
Drawing 编程控制 repaint() 调用 1. 擦除并填充成背景色 update() 2. 调用paint() paint( ) 第一次绘制 当某些操作破坏了显示,需重新绘制时 • java.awt.Component • JComponent • public void paintComponent(Graphics g) • protected void paintBorder(Graphics g) • protected void paintChildren(Graphics g) • super.paintComponent( g ); Java 语言编程技术
Drawing ——Graphics的方法 原点(0,0) X轴 Y轴 • drawLine(x1,y1,x2,y2) //直线 • drawRect(x,y,w,h) //矩形 • drawOval(x,y,w,h) //圆或椭圆 • fillOval(x,y,w,h) //实心圆或椭圆 • drawPolygon(X坐标数组,Y坐标数组,个数) //画多边形 • drawArc (x,y,w,h,开始角度,弧度角度) • setColor(Color c) //置颜色 • setFont(Font f) //置字体 • getColor() • getFont() Java 语言编程技术
Color类 • 构造函数1(设置颜色) Color c = new Color(int red, int green, int blue) 范围 :0~255 new Color(200,200,200) new Color(255,0,100) • 构造函数2 Color c = new Color(int rgb) 兰色:0~7 位 绿色:8~15位 红色:16~23位 • 颜色常量值 public static final Color black Color.black , Color.blue , Color.gray , Color. red Color.white … Java 语言编程技术
Font类 • java.awt.Font • 设计字体显示效果 Font mf = new Font(String 字体,int 风格,int 字号); • 字体:TimesRoman, Courier, Arial等 • 风格: • Font.BOLD • Font.ITALIC • Font.PLAIN • Font.BOLD + Font.ITALIC • 字号:字的大小(磅数) Java 语言编程技术
Font类(续) • 可以通过如下方式得到当前平台下支持的所有字体列表: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font[] fonts = ge.getAllFonts(); String[] FontFamilyNames = ge.getAvailableFontFamilyNames(); • 例:AllFonts.java Java 语言编程技术
Graphics2D • class Graphics2D extends Graphics public void paintComponent (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... } Java 语言编程技术
Graphics2D • Shape1.java • Shape2.java • ClipArea.java • draw(Shape s) • fill(Shape s) • drawString(String str,int x,int y) • … … • setStroke(Stroke s) • setPaint(Paint paint) • setComposite(Composite comp) • … … Java 语言编程技术
Dialog • JDialog • JDialog(Frame owner,String title, boolean modal) • ContentPane • 例:Dialogs.java Java 语言编程技术
Dialog • JFileChooser • int showOpenDialog(Component parent) • int showSaveDialog(Component parent) • int showDialog(Component parent,String approveButtonText) Java 语言编程技术
Dialog • JFileChooser • 例:FileChooserTest.java JFileChooser c = new JFileChooser(); int rVal = c.showOpenDialog(FileChooserTest.this); if(rVal == JFileChooser.APPROVE_OPTION) { filename.setText(c.getSelectedFile().getName()); dir.setText(c.getCurrentDirectory().toString()); } if(rVal == JFileChooser.CANCEL_OPTION) …… Java 语言编程技术
Dialog • JFileChooser • setFileFilter(FileFilter filter) • setFileView(FileView fileView) • setAccessory(JComponent newAccessory) • 例: FileChooserDemo2.java Java 语言编程技术
JSlider & JProgressBar • JSlider • JSlider(int orientation, int min, int max, int value) • JSlider(BoundedRangeModel brm) • JProgressBar • JProgressBar(int orient, int min, int max) • JProgressBar(BoundedRangeModel newModel) Java 语言编程技术
JSlider & JProgressBar • 共有方法 • int getValue() • void setValue(int n) • BoundedRangeModel getModel() • void setModel(BoundedRangeModel newModel) • 例:Progress.java Java 语言编程技术
Tree • TreeNode —— MutableTreeNode ——DefaultMutableTreeNode • add(MutableTreeNode newChild) • insert(MutableTreeNode child, int index) • remove(MutableTreeNode node) • DefaultMutableTreeNode root = • new DefaultMutableTreeNode("root"); • root.add(new DefaultMutableTreeNode(data)); • JTree tree = new JTree(root); Java 语言编程技术
Tree • TreeModel —— DefaultTreeModel • addTreeModelListener(TreeModelListener l) • Object getChild(Object parent,int index) • Object getRoot() • insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) • TreeModelListener • treeNodesChanged(TreeModelEvent e) • treeNodesInserted(TreeModelEvent e) • treeNodesRemoved(TreeModelEvent e) • treeStructureChanged(TreeModelEvent e) Java 语言编程技术
Tree • JTree • TreeModel getModel() • setModel(TreeModel newModel) • 例: Trees.java model =(DefaultTreeModel)tree.getModel(); chosen = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); model.insertNodeInto(child, chosen, 0); Java 语言编程技术
Table • JTable • JTable(Object[][] rowData, Object[] columnNames) • JTable(TableModel dm) Java 语言编程技术
Table • 例:SimpleTableDemo.java Object[][] data = { {"Mary", "Campione", …… }, {"Alison", "Huml", ……}, …… }; String[] columnNames = {"First Name", "Last Name", ……}; JTable table = new JTable(data, columnNames); Java 语言编程技术
Table • TableModel —— AbstractTableModel • int getColumnCount() • int getRowCount() • Object getValueAt(int row, int col) • void setValueAt(Object val, int row, int col) • boolean isCellEditable(int row, int col) • 例: Table.java • 例: TableDemo.java Java 语言编程技术
Other Component • JColorChooser ColorChooserDemo.java • JToolBar • JSplitPane Java 语言编程技术
Look&Feel try { UIManager.setLookAndFeel(UIManager. getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { } try { UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName()); } catch Java 语言编程技术
Look&Feel UIManager.setLookAndFeel(" com.sun.java. "+ " swing.plaf.windows.WindowsLookAndFeel"); "com.sun.java.swing.plaf.motif.MotifLookAndFeel" "javax.swing.plaf.metal.MetalLookAndFeel" "com.sun.java.swing.plaf.windows. WindowsLookAndFeel" "javax.swing.plaf.mac.MacLookAndFeel" • 例:LookAndFeel.java Java 语言编程技术
Clipboard • java.awt.datatransfer.Clipboard • Transferable getContents(Object requestor) • setContents(Transferable contents, ClipboardOwner owner) • Transferable • Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException Java 语言编程技术
Clipboard • java.awt.datatransfer. DataFlavor • DataFlavor.stringFlavor • 例:CutAndPaste.java clipbd.setContents(clipString,clipString); Transferable clipData = clipbd.getContents(CutAndPaste.this); String clipString = (String)clipData. getTransferData(DataFlavor.stringFlavor); Java 语言编程技术