Java Graphics User Interface
1. How to display a Window?
2. Layout Manager
3. Common Control
4. Event Listener
5. Dialogbox
6. Advanced Control
Tóm tắt nội dung Java Graphics User Interface, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
e method and take false value 3. Common Control 28 JTextField To set Text in code behind for JTextField: txtTen.setText("Hello Tèo"); To get Text from JTextField: String s=txtTen.getText(); We could convert data: int n=Integer.parseInt(s); double d=Double.parseDouble(s); float f=Float.parseFloat(s); To set focus:txtTen.requestFocus(); 3. Common Control 29 JTextArea Input data multi line JLabel lblDes=new JLabel("Mô tả:"); JTextArea are=new JTextArea(5, 15); JScrollPane sc=new JScrollPane(are); add(lblDes);add(sc); JTextArea are=new JTextArea(5, 15); 5 rows, 15 columns We should use JScrollPane to create Scroll for JTextArea when user input data over limit row or column 3. Common Control 30 ButtonGroup & JRadioButton Make single choice Must add JRadioButton into the ButtonGroup if(rad1.isSelected()) { } 31 ButtonGroup & JRadioButton JPanel pnGroup=new JPanel(); pnGroup.setLayout(new BoxLayout(pnGroup, BoxLayout.Y_AXIS)); Border bor=BorderFactory.createLineBorder(Color.RED); TitledBorder titlebor=new TitledBorder(bor, "Chọn nè:"); pnGroup.setBorder(titlebor); JRadioButton rad1=new JRadioButton("Rất hài lòng"); JRadioButton rad2=new JRadioButton("Hài lòng"); JRadioButton rad3=new JRadioButton("Tạm chấp nhận"); JRadioButton rad4=new JRadioButton("Không chấp nhận"); ButtonGroup group=new ButtonGroup(); group.add(rad1);group.add(rad2); group.add(rad3);group.add(rad4); pnGroup.add(rad1);pnGroup.add(rad2); pnGroup.add(rad3);pnGroup.add(rad4); add(pnGroup); 32 ButtonGroup & JRadioButton Create Border with title: Border bor=BorderFactory.createLineBorder(Color.RED); TitledBorder titlebor=new TitledBorder (bor, "Chọn nè:"); pnGroup.setBorder(titlebor); Define a buttongroup to add all radio: ButtonGroup group=new ButtonGroup(); group.add(rad1); And add all Radio into the pnGroup: pnGroup.add(rad1); Add pnGroupd into the Window: add(pnGroup); 3. Common Control 33 JCheckBox Make multi choice JPanel pnCheck=new JPanel(); pnCheck.setLayout(new GridLayout(2, 2)); Border bor2=BorderFactory .createEtchedBorder(Color.BLUE, Color.RED); TitledBorder titlebor2= new TitledBorder(bor2, "Môn học yêu thích:"); pnCheck.setBorder(titlebor2); JCheckBox chk1=new JCheckBox("Java"); JCheckBox chk2=new JCheckBox("F Sharp"); JCheckBox chk3=new JCheckBox("C Sharp"); JCheckBox chk4=new JCheckBox("Ruby"); pnCheck.add(chk1);pnCheck.add(chk2); pnCheck.add(chk3);pnCheck.add(chk4); add(pnCheck); 3. Common Control 34 JCheckBox Make multi choice Set grid layout 2 rows and 2 column pnCheck.setLayout(new GridLayout(2, 2)); Create JCheckBox: JCheckBox chk1=new JCheckBox("Java"); Add chk1 into the pnCheck: pnCheck.add(chk1); Add pnCheck into the Window: add(pnCheck); BorderFactory.createEtchedBorder(Color.BLUE, Color.RED); Create border with 2 color: Blue, Red: if(chk1.isSelected()) { //do something } 3. Common Control 35 JComboBox JComboBox cbo=new JComboBox(); cbo.addItem("Xuất sắc"); cbo.addItem("Giỏi"); cbo.addItem("Khá"); cbo.addItem("Trung bình"); add(cbo); String arr[]={"Xuất sắc" ,"Giỏi" ,"Khá","Trung bình"}; JComboBox cbo=new JComboBox(arr); add(cbo); 3. Common Control 36 JComboBox int n=cbo.getSelectedIndex(); n is position that we selected Object o=cbo.getSelectedItem(); We could cast object to valid another type cbo.setSelectedIndex(-1); To clear selection 3. Common Control 37 JComboBox class Person { private String Id; private String Name; public Person(String id,String name){ Id=id; Name=name; } public String toString() { return Name; } } How to add Array Person to JComboBox? 3. Common Control 38 JComboBox How to add Array Person to JComboBox? Person []list={ new Person("1", "Trần Thành Công"), new Person("2", "Nguyễn Đại Thắng"), new Person("3", "Hoàng Thành Đạt")}; JComboBox cbo2=new JComboBox(list); add(cbo2); We get Person from selected: Person p=(Person)cbo2.getSelectedItem(); 3. Common Control 39 JList Person []list={ new Person("1", "Đỗ Công Thành"), new Person("2", "Nguyễn Văn Hùng"), new Person("3", "Trần Duy Thanh"), new Person("4", "Đoàn Ái Nương"), … new Person("10", "Đào Cẩm Hằng") }; JList jl=new JList(list); jl.setSelectionBackground(Color.RED); jl.setSelectionForeground(Color.WHITE); JScrollPane scjl=new JScrollPane(jl, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); add(scjl); 3. Common Control 40 JList ListSelectionModel ListSelectionModel.SINGLE_SELECTION; ListSelectionModel.SINGLE_INTERVAL_SELECTION; ListSelectionModel.MULTIPLE_INTERVAL_SELECTION; jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) int n=jl.getSelectedIndex(); int m[]=jl.getSelectedIndices(); Object o=jl.getSelectedValue(); Object arr[]=jl.getSelectedValues(); 4. Event Listener 41 Inline anonymous listener Listener in variable Listener class 4. Event Listener 42 Inline anonymous listener JButton btn1=new JButton("Say Hello!"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, "Hello tèo"); } }); 4. Event Listener 43 Inline anonymous listener btn1.addMouseListener(new MouseListener() { public void mouseReleased(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mouseEntered(MouseEvent arg0) { } public void mouseClicked(MouseEvent arg0) { } }); 4. Event Listener 44 Listener in variable Hold a reference to the Listener in a variable could share event ActionListener btnClick=new ActionListener() { public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, "Click!"); } }; btn1.addActionListener(btnClick); btn2.addActionListener(btnClick); As the same for mouse events 4. Event Listener 45 Listener class private class MyClick implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } } btn1.addActionListener(new MyClick()); Could use class event anywhere 46 5. Dialogbox JOptionPane JOptionPane.showMessageDialog(null,"Hello Tèo"); int ret=JOptionPane.showConfirmDialog(null,"Thoát hả?","Thoát",JOptionPane.YES_NO_OPTION); if(ret==JOptionPane.YES_OPTION) { } String s=JOptionPane.showInputDialog("Nhập :"); 6. Advanced Control 47 JScrollpane JTable JTree JMenuBar JToolBar 48 JScrollpane Provides a scrollable view of a lightweight component. A JScrollPane manages a viewport, optional vertical and horizontal scroll bars, and optional row and column heading viewports. ImageIcon img=new ImageIcon("baby.jpg"); JLabel lblImg=new JLabel(img); JScrollPane scimg=new JScrollPane(lblImg, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scimg.setPreferredSize(new Dimension(600, 500)); add(scimg); 49 JTable The JTable is used to display and edit regular two-dimensional tables of cells. 50 JTable DefaultTableModel dm=new DefaultTableModel(); dm.addColumn("Mã"); dm.addColumn("Tên"); dm.addColumn("Tuổi"); final JTable tbl=new JTable(dm); dm.addRow(new String[]{"112","Ngô văn Bắp","21"}); dm.addRow(new String[]{"113","Nguyễn Thị Tý","18"}); dm.addRow(new String[]{"114","Trần Văn Tèo","22"}); JScrollPane sc=new JScrollPane(tbl); Container con=getContentPane(); con.setLayout(new BorderLayout()); con.add(sc,BorderLayout.CENTER); 51 JTable How to handle event : Mouse, Key from JTable? tbl.addMouseListener(new MouseListener() { public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseClicked(MouseEvent e) { int row=tbl.getSelectedRow(); int col=tbl.getSelectedColumn(); String s=(String)tbl.getValueAt(row, col); JOptionPane.showMessageDialog(null, s); }}); 52 JTable How to handle event : Mouse, Key from JTable? tbl.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent arg0) {} public void keyReleased(KeyEvent arg0) { int row=tbl.getSelectedRow(); int col=tbl.getSelectedColumn(); String s=(String)tbl.getValueAt(row, col); JOptionPane.showMessageDialog(null, s); } public void keyPressed(KeyEvent arg0) {} }); 53 JTable dm.setRowCount(0); Clear all data from JTable dm.addRow( new String[]{"ID_002","Võ Tòng","32"}); Vectorvec=new Vector(); vec.add("ID_003"); vec.add("Lâm Sung"); vec.add("30"); dm.addRow(vec); 2 Ways to add new Row: dm.getRowCount(); Get total row from JTable 54 JTree A control that displays a set of hierarchical data as an outline. DefaultMutableTreeNode root= new DefaultMutableTreeNode("ĐH Công Nghiệp"); final JTree tree=new JTree(root); DefaultMutableTreeNode cnttNode=new DefaultMutableTreeNode("Công Nghệ TT"); root.add(cnttNode); DefaultMutableTreeNode dhth1Node=new DefaultMutableTreeNode("Lớp ĐHTH1"); cnttNode.add(dhth1Node); 55 JTree Handle event tree.addMouseListener(new MouseListener() { public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseClicked(MouseEvent e) { Object o=tree.getLastSelectedPathComponent(); DefaultMutableTreeNode node=(DefaultMutableTreeNode)o; JOptionPane.showMessageDialog(null, node); } }); 56 JMenuBar JMenuBar menubar=new JMenuBar(); setJMenuBar(menubar); JMenu mnuFile=new JMenu("File"); JMenu mnuEdit=new JMenu("Edit"); menubar.add(mnuFile); menubar.add(mnuEdit); JMenuItem mnuFileNew=new JMenuItem("New"); JMenuItem mnuFileOpen=new JMenuItem("Open"); JMenuItem mnuFileExit=new JMenuItem("Exit"); mnuFile.add(mnuFileNew); mnuFile.add(mnuFileOpen); mnuFile.addSeparator(); mnuFile.add(mnuFileExit); 57 JMenuBar Handle event as the same JButton mnuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); 6. Advanced Control 58 JToolBar JToolBar toolBar=new JToolBar("MyBar"); JButton btn1=new JButton("New"); JCheckBox chk1=new JCheckBox("Checkme"); toolBar.add(btn1); toolBar.add(chk1); JButton btn2=new JButton("Exit"); toolBar.add(btn2); add(toolBar,BorderLayout.NORTH); Handle event as the same JButton END 59
File đính kèm:
- LESSON_12_GUI.pptx