Danh mục

Bài giảng Lập trình Java: Buổi 3 - Industrial university of Ho Chi Minh City

Số trang: 19      Loại file: pdf      Dung lượng: 1.50 MB      Lượt xem: 13      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 9,000 VND Tải xuống file đầy đủ (19 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Bài giảng "Lập trình Java - Graphic user interface in Java" trình bày các nội dung: Top-level container, layout manager, common control, event listener, dialogbox, advanced control. Mời các bạn cùng tham khảo nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình Java: Buổi 3 - Industrial university of Ho Chi Minh City1/3/201603. Graphic User Interface in JavaFaculty of Information TechnologiesIndustrial University of Ho Chi Minh City1GUI components (p2) Top-level container Layout Manager Common Control Event Listener Dialogbox Advanced Control2Top-level container311/3/2016Top-level container: JFrame4Top-level container: JDialog5Top-level container: Window621/3/2016GUI Components - JPanel•A JPanel is used to group other components intorectangular regions•Properties:o•A panel can be nested within another paneloThe default layout manager is FlowLayoutUsage:o•Allocate JPanel, put other components in it, add to other containerConstructors:oJPanel()oJPanel(LayoutManager lm)7GUI Components - JPanel8GUI Components - JLabel•Labels are usually used to display information oridentify other components in the interface•A label can be composed of text, and image, or both atthe same time•The ImageIcon class is used to represent an imagethat is stored in a label•The alignment of the text and image within the labelcan be set explicitly931/3/2016GUI Components - JLabel•Constructors:oJLabel()oJLabel (String labeltext)oJLabel (String labeltext, int alignment)• Creates an empty label• Creates a label with a given text• Creates a label with given alignment where alignment can beLEFT, RIGHT, CENTER, LEADING or TRAILING.oJLabel (Icon img)oJLabel (String str, Icon img, int align)• Only Icon will be used for label10GUI Components - JLabel• Some methods:oString getText()ovoid setText(String text)• Gets or sets the text displayed on the labeloFont getFont()ovoid setFont(Font font)• Gets or sets the current font of the label11Using special fonts for text•To draw characters in a font, you must first create an object of theclass Font•Constructor:oFont( String font_name, int font_style, int font_size )ArialTahomaTimes NewRoman….•Font.PLAINFont.BOLDFont.ITALICFont.BOLD +Font.ITALICExample:Font fo = new Font (Times New Roman, Font.BOLD, 14);1241/3/2016JLabel Demopublic class JLabelDemo extends JFrame {public JLabelDemo() {super(Panel on a Frame);JPanel jp = new JPanel();jp.add( new JLabel(User Name: , new ImageIcon(blue-ball.gif),SwingConstants.CENTER);jp.add( new JLabel(Password: ));add(jp);setSize(400, 400);}public static void main(String args[]){new JLabelDemo().setVisible(true);}}13GUI Components - JTextField•Purposeo•to input textodisplay informationThe width of JTextField: is measured bycolumnoThe column width that you set in the JTextFieldconstructor is not an upper limit on the number ofcharacters the user can enter14JTextField - Constructors•JTextField()o•o•creates a new textfield with the given stringJTextField(int cols)o•creates an empty textfield with 1 columnsTextField(String s)creates an empty textfield with given number of columnsJTextField(String text, int cols)ocreates a new textfield with given string and given number of columnsExample:JTextField mmText = new JTextField(10);JTextField txtName = new JTextField(“Hello”, 10);155

Tài liệu được xem nhiều: