1. Trang chủ
  2. » Công Nghệ Thông Tin

LESSON 12 GUI Lập trình Java

59 293 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 59
Dung lượng 2,12 MB

Nội dung

DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Contact Full Name: Trần Duy Thanh Blog : http://duythanhcse.wordpress.com Email: tranduythanh@dntu.edu.vn Phone: 0987773061 DONG NAI UNIVERSITY OF TECHNOLOGY How to display a Window? Layout Manager Common Control Event Listener Dialogbox Advanced Control DONG NAI UNIVERSITY OF TECHNOLOGY How to display a Window?  Extends from Frame or JFrame? import java.awt.*; import java.awt.event.*; import javax.swing.*; DONG NAI UNIVERSITY OF TECHNOLOGY How to display a Window? public class MyWindow extends JFrame{ public MyWindow(){ super("Demo Windows"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { MyWindow ui=new MyWindow(); ui.setSize(400, 300); ui.setLocationRelativeTo(null); ui.setVisible(true); } } DONG NAI UNIVERSITY OF TECHNOLOGY How to display a Window? super("Demo Windows"); Use to set title for this window setDefaultCloseOperation(EXIT_ON_CLOSE); Allow click ‘x’ Top right corner to close the window ui.setSize(400, 300); set Width =400 and Height =300 ui.setLocationRelativeTo(null); Display window on desktop center screen ui.setVisible(true); Show the window DONG NAI UNIVERSITY OF TECHNOLOGY Layout Manager  FlowLayout  BoxLayout  BorderLayout  CardLayout  GridBagLayout  GridLayout  GroupLayout  SpringLayout - Setting up the Layout before add another control - Usually, we use JPanel to add another control, JPanel is container JPanel could add another JPanel DONG NAI UNIVERSITY OF TECHNOLOGY  FlowLayout FlowLayout cho phép add control dòng, hết chỗ chứa tự động xuống dòng, ta điều chỉnh hướng xuất control Mặc định JPanel khởi tạo thân lớp chứa có kiểu Layout FlowLayout Resize the Width DONG NAI UNIVERSITY OF TECHNOLOGY  FlowLayout Code E:\HUI\Java\Study\ hocui\src\MyFlowLayout JPanel pnFlow=new JPanel(); pnFlow.setLayout(new FlowLayout()); pnFlow.setBackground(Color.PINK); JButton btn1=new JButton("FlowLayout"); JButton btn2=new JButton("Add control"); JButton btn3=new JButton("Trên dòng"); JButton btn4=new JButton("Hết chỗ chứa"); JButton btn5=new JButton("Thì xuống dòng"); pnFlow.add(btn1);pnFlow.add(btn2); pnFlow.add(btn3);pnFlow.add(btn4); pnFlow.add(btn5); Container con=getContentPane(); con.add(pnFlow); DONG NAI UNIVERSITY OF TECHNOLOGY  FlowLayout pnFlow.setLayout(new FlowLayout()); Setup FlowLayout for pnFlow pnFlow.add(btn1); Add new button into the pnFlow Container con=getContentPane(); get the Window container con.add(pnFlow); add pnFlow panel into the window container 10 DONG NAI UNIVERSITY OF TECHNOLOGY Event Listener  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 45 DONG NAI UNIVERSITY OF TECHNOLOGY 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 :"); 46 DONG NAI UNIVERSITY OF TECHNOLOGY Advanced Control      JScrollpane JTable JTree JMenuBar JToolBar 47 DONG NAI UNIVERSITY OF TECHNOLOGY  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); 48 DONG NAI UNIVERSITY OF TECHNOLOGY  JTable The JTable is used to display and edit regular twodimensional tables of cells 49 DONG NAI UNIVERSITY OF TECHNOLOGY  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); 50 DONG NAI UNIVERSITY OF TECHNOLOGY  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); }}); 51 DONG NAI UNIVERSITY OF TECHNOLOGY  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) {} }); 52 DONG NAI UNIVERSITY OF TECHNOLOGY  JTable dm.setRowCount(0); Clear all data from JTable dm.getRowCount(); Get total row from JTable Ways to add new Row: 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); 53 DONG NAI UNIVERSITY OF TECHNOLOGY  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); 54 DONG NAI UNIVERSITY OF TECHNOLOGY  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); } }); 55 DONG NAI UNIVERSITY OF TECHNOLOGY  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); 56 DONG NAI UNIVERSITY OF TECHNOLOGY  JMenuBar Handle event as the same JButton mnuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); 57 DONG NAI UNIVERSITY OF TECHNOLOGY Advanced Control  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 58 DONG NAI UNIVERSITY OF TECHNOLOGY END 59 [...]... control sẽ bị che khuất nếu như thiếu không gian chứa nó 11 DONG NAI UNIVERSITY OF TECHNOLOGY  BoxLayout BoxLayout.X_AXIS BoxLayout.Y_AXIS No wrap row when resize dimension 12 DONG NAI UNIVERSITY OF TECHNOLOGY  BoxLayout Code E:\HUI \Java\ Study\ hocui\src\MyBoxLayout.j JPanel pnBox=new JPanel(); pnBox.setLayout(new BoxLayout(pnBox, BoxLayout.X_AXIS)); JButton btn1=new JButton("BoxLayout"); btn1.setForeground(Color.RED);... JTable, JTree, ListView, JScrollpane… ta thường đưa vào vùng Center để nó có thể tự co giãn theo kích thước cửa sổ giúp giao diện đẹp hơn 15 DONG NAI UNIVERSITY OF TECHNOLOGY  BorderLayout Code E:\HUI \Java\ Study\ hocui\src\MyBorderLayou JPanel pnBorder=new JPanel(); pnBorder.setLayout(new BorderLayout()); JPanel pnNorth=new JPanel(); pnNorth.setBackground(Color.RED); pnBorder.add(pnNorth,BorderLayout.NORTH);... trí hiển thị đó thì ta có thể cho các control khác hiển thị tại những thời điểm khác nhau, mặc định control được add đầu tiên sẽ hiển thị 18 DONG NAI UNIVERSITY OF TECHNOLOGY  CardLayout Code E:\HUI \Java\ Study\ hocui\src\MyCardLayout final JPanel pnCenter=new JPanel(); pnCenter.setLayout(new CardLayout()); final JPanel pnCard1=new JPanel(); pnCard1.setBackground(Color.LIGHT_GRAY); final JPanel pnCard2=new... 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);... NAI UNIVERSITY OF TECHNOLOGY 3 Common Control  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); if(chk1.isSelected()) { //do something } Create border with 2 color: Blue, Red: BorderFactory.createEtchedBorder(Color.BLUE,

Ngày đăng: 30/05/2016, 00:16

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN