Bài giảng Lập trình CSDL - Java - Bài 3: Components - Nguyễn Hữu thể

LẬP TRÌNH JAVA CSDL  
BÀI 3  
COMPONENTS  
Nguyễn Hữu Thể  
1
Nội dung  
JLabel  
JTable  
JButton  
JMenu  
JTextField  
JCheckBox  
JRadioButton  
JPanel  
JToolBar  
JOptionPane  
JFileChooser  
JComboBox, JList  
2
GUI Components  
JButton, JLabel, JTextField, JCheckBox, JRadioButton, and  
JComboBox.  
Each GUI component class provides several constructors that  
you can use to create GUI component objects.  
3
GUI Components  
// Create a button with text OK  
JButton btOK = new JButton("OK");  
// Create a label with text "Enter your name: "  
JLabel lbName = new JLabel("Enter your name: ");  
// Create a text field with text "Type Name Here"  
JTextField txtName = new JTextField("Type Name Here");  
// Create a check box with text bold  
JCheckBox chkBold = new JCheckBox("Bold");  
// Create a radio button with text red  
JRadioButton rbRed = new JRadioButton("Red");  
// Create a combo box with choices red, green, blue  
JComboBox cboColor = new JComboBox(new String[]{"Red",  
4
"Green", "Blue"});  
Working with Components  
javax.swing.Jcomponent  
Several methods:  
. setEnabled(boolean): receive user input (an argument of  
true) or is inactive and cannot receive input (false).  
Components are enabled by default.  
. isEnabled() returns a boolean value.  
. setVisible(boolean): for all components. Use true to display  
a component and false to hide it.  
. isVisible()  
. setSize(int, int): width and height specified as arguments  
. setSize(Dimension) uses a Dimension  
. getSize(): returns a Dimension object with height and width  
5
Working with Components  
. setText()  
. getText()  
. setValue() and getValue() for components that store a  
numeric value.  
6
JLabel  
Display Text, not editable  
Constructor:  
Label() : An empty label  
JLabel(String) : A label with the specified text  
JLabel(String, int) : A label with the specified text and  
alignment LEFT, CENTER, and RIGHT.  
JLabel(String, Icon, int) : A label with the specified  
text, icon, and Alignment  
7
JLabel  
Common Methods:  
void setFont (Font f)  
void setText(String S)  
String getText()  
void setIcon(Icon)  
8
JLabel  
Example:  
JLabel lbl=new JLabel("Họ và tên:");  
JLabel lbl=new JLabel("Ngày sinh:");  
9
JLabel  
The use of HTML is supported by most Swing components.  
Example: use HTML markup to create multiline and multifont  
labels:  
JLabel lbHoten = new JLabel("<html>Dòng 1<p  
style=\"color:red;font-size:20\">Dòng  
2</p></html>");  
10  
JTextField  
Display data, Input data  
Constructors  
JTextField(): An empty text field  
JTextField(int): A text field with the specified width  
JTextField(String): A text field with text  
JTextField(String, int): A text field with the specified text  
and width  
Common Methods  
void setText(String S)  
String getText()  
void setEditable(boolean b)  
boolean isEditable()  
JTextField  
Example  
setLayout(new FlowLayout());  
JLabel lbHoten = new JLabel("Nhập họ và tên");  
add(lbHoten);  
JTextField txtHoten = new JTextField(20);  
add(txtHoten);  
JTextField  
Don’t allow input data  
txtHoten.setEditable(false)  
To set Text in code behind for JTextField  
txtHoten.setText("Hello world");  
To get Text from JTextField  
String s = txtHoten.getText();  
We could convert data  
int n = Integer.parseInt(s); //s is String  
double d = Double.parseDouble(s);  
float f = Float.parseFloat(s);  
To set focus:  
txtHoten.requestFocus();  
JPasswordField  
Hide the characters a user is typing into the field.  
JPasswordField class, a subclass of JTextField.  
JPasswordField constructor methods take the same arguments as  
those of its parent class.  
Methods:  
. JPasswordField(String text, int columns)  
. char[] getPassword(): returns the text contained in this  
password field  
JPasswordField  
. setEchoChar(char): replacing each input character with the  
specified character  
JPasswordField pass = new JPasswordField(20);  
pass.setEchoChar('#');  
JTextArea  
Input is more than one line long  
Constructors  
JTextArea(): Create a default text area.  
JTextArea(int rows, int columns): Create a text area  
with the specified number of rows and columns.  
JTextArea(String text)  
JTextArea(String text, int rows, int columns)  
JTextArea(Document doc): Create a text area that uses the  
specified Document.  
JTextArea(Document doc, String text, int rows, int  
columns)  
JTextArea  
Common methods  
void append(String str)  
. Append the given text to the end of the document.  
void insert(String str, int pos)  
. Insert the specified text at the given position . To insert text  
at the beginning of the document, use a position of 0.  
void replaceRange(String str, int start, int  
end)  
. Replace a section of the document  
JTextArea  
public int getLineStartOffset(int line)  
throws BadLocationException  
. Return the character offset (from the beginning) that marks  
the beginning of the specified line number.  
public int getLineEndOffset(int line) throws  
BadLocationException  
. Return the character offset (from the beginning) that marks  
the end of the specified line number. This is actually the  
offset of the first character of the next line.  
public int getLineOfOffset(int offset) throws  
BadLocationException  
. Return the line number that contains the given character  
offset (from the beginning of the document).  
JTextArea  
JPanel contentPane = new JPanel();  
JLabel lblNewLabel = new JLabel("Nhập dữ liệu:");  
contentPane.add(lblNewLabel);  
JTextArea textArea = new JTextArea(3,15);  
textArea.setWrapStyleWord(true);  
textArea.setLineWrap(true);  
JScrollPane scrollPane = new JScrollPane(textArea);  
contentPane.add(scrollPane);  
class MyJTextArea extends JFrame {  
private JPanel contentPane;  
public MyJTextArea() {  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
setBounds(100, 100, 257, 128);  
contentPane = new JPanel();  
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  
setContentPane(contentPane);  
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));  
JLabel lblNewLabel = new JLabel("Nhập dữ liệu:");  
contentPane.add(lblNewLabel);  
JScrollPane scrollPane = new JScrollPane();  
JTextArea textArea = new JTextArea(3,15);  
textArea.setWrapStyleWord(true);  
textArea.setLineWrap(true);  
scrollPane.setViewportView(textArea);  
contentPane.add(scrollPane);  
}
}
Tải về để xem bản đầy đủ
pdf 36 trang yennguyen 21280 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình CSDL - Java - Bài 3: Components - Nguyễn Hữu thể", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_lap_trinh_csdl_java_bai_3_components_nguyen_huu_th.pdf