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: Text component, choice component, menu, mnemonic, toolbar, tooltip, tabbed pane, scroll pane, dialog box. 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 4 - Industrial university of Ho Chi Minh City1/3/201604. Graphic User Interface in JavaFaculty of Information TechnologiesIndustrial University of Ho Chi Minh City1GUI components (p3)Text componentChoice componentMenuMnemonicToolbarTooltipTabbed paneScroll paneDialog box2Text component311/3/2016Text component4Text componentGroupDescriptionTextAlso known simply as text fields,Controls text controls can display only oneline of editable text. Like buttons,they generate action events.Swing ClassesJTextField and itssubclassesJPasswordField andJFormattedTextFieldPlainTextAreasJTextArea can display multiple lines JTextAreaof editable text. Although a textarea can display text in any font, allof the text is in the same font.StyledTextAreasA styled text component can display JEditorPaneeditable text using more than one and its subclassfont. Some styled text components JTextPaneallow embedded images and evenembedded components.5JTextField•If the cursor is in the text field, the user presses theEnter key, JTextField generates an Action eventoListener?oMethod in the listener?oRegister listener to the text field?• … implements ActionListener• public void actionPerformed(ActionEvent e)• void addActionListener(ActionListener listener)621/3/2016JTextField Demopublic class JTextFieldDemo extends JFrameimplements ActionListener{JTextField mmText;JLabel resultLabel;public JTextFieldDemo() {super(Chuyen doi don vi);setLayout(new GridLayout(2,2));public void actionPerformed(ActionEvent e){double cm, mm;mm =Double.parseDouble(mmText.getText());cm = mm/10;resultLabel.setText(Double.toString(cm));}add (new JLabel (“Nhap vao so millimet:));add (mmText = new JTextField (10));add (new JLabel (“So centimet tuong ung:));add (resultLabel = new JLabel (---));mmText.addActionListener (this);setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(300,90);}}public static void main(String[] args) {new JTextFieldDemo().setVisible(true);}7JPasswordField••Purpose: used to enter a passwordA JPasswordField is similar to a JTextField except thecharacters typed in by the user are not echoed back tothe useroInstead, an alternate character such as asterisks (*) isdisplayedoYou can set the echo character using the method:• public void setEchoChar(char c)8JPasswordField DemopnlRight = new JPanel(new GridLayout(0,1));pnlRight.add(btnOk = new JButton(OK));pnlRight.add(btnCancel=new JButton(Cancel));public class JPasswordFieldDemo extends JFrameimplements ActionListener{JPasswordField txtPassword;JButton btnOk, btnCancel;add(pnlLeft, BorderLayout.WEST);add(pnlRight, BorderLayout.CENTER);public JPasswordFieldDemo(){super(JPasswordField Demo);JPanel pnlLeft, pnlRight;txtPassword = new JPasswordField(12);txtPassword.addActionListener(this);btnOk.addActionListener(this);btnCancel.addActionListener(this);setDefaultCloseOperation(EXIT_ON_CLOSE);setsize(200, 200);}pnlLeft = new JPanel();pnlLeft.add(new JLabel(Password: ));pnlLeft.add(txtPassword);931/3/2016JPasswordField Demo (contd.)public void actionPerformed(ActionEvent e){Object o = e.getSource();if (o == btnOk || o == txtPassword){char chPassword[] = txtPassword.getPassword();String strPassword = new String(chPassword);if(strPassword.trim().equals(pass)) {JOptionPane.showMessageDialog(this,Correct Password);System.exit(0);}else {JOptionPane.showMessageDialog(this,Incorrect Password,Error Message, JOptionPane.ERROR_MESSAGE);txtPassword.selectAll();txtPassword.requestFocus();}}else {System.exit(0);}}public static void main(String [] args){ new JPasswordFieldDemo().setVisible(true); }}10JTextArea•Purposeo•For texts with more than one line longConstructorsoJTextArea(int rows, int cols)oJTextArea(String text, int rows, int cols)• constructs a new text area with number of rows and columns• constructs a new text area with an initial text11JTextArea DemoJPanel buttonPanel = new JPanel();buttonPanel.add(insertButton=new JButton(Insert));buttonPanel.add(wrapButton = new JButton(Wrap));add(buttonPanel, BorderLayout.SOUTH);textArea = new JTextArea(8, 40);add(textArea, BorderLayout.CENTER);1241/3/2016Outline Text component Choice component Menu Mnemonic Toolbar Tooltip Tabbed pane Scroll pane Dialog box13JCheckBox•Purpose:oUsed for multi-option user input that the user may select ordeselect by clicking on them•Constructors:oJCheckBox()oJCheckBox(String text)• Creates an initially unchecked checkbox with no label• Creates a checkbox (initially unchecked) with the specified text;see setSelected for changing itoJCheckBox(String text, boolean selected)• Creates a checkbox with the specified text– The initial state is determined by the boolea ...