Introduction to Java :Components Part-1
Static Text with Label
The Label class gives you the ability to draw a single line of text in your application. There is not much to it, consider the following example:
In all of the following examples, we will use the add() method to add components to a Frame object in order to show them on the monitor. Frame and add() will be discussed in the next section. So don’t worry about the specifics of baseFrame or add() for now. Instead, watch how the components are used within it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.awt.*; public class LabelExample { public static void main(String[] args) { Frame baseFrame = new Frame(); baseFrame.setLayout(new GridLayout(3,1)); baseFrame.reshape(10,10,200,200); baseFrame.setTitle("Label Example"); Label labelLeft = new Label("Hello Cyberspace!"); labelLeft.setForeground(Color.white); labelLeft.setBackground(Color.black); Label labelRight = new Label("Hello Cyberspace!", Label.RIGHT); labelRight.setFont(new Font("Helvetica", Font.BOLD, 15)); Label labelCenter = new Label("Hello Cyberspace!", Label.CENTER); baseFrame.add(labelLeft); baseFrame.add(labelRight); baseFrame.add(labelCenter); baseFrame.show(); } } |
The previous code yields the following interface:
Text Entry Components
The JDK also provides the TextField and TextArea components which allow you to get user input. They are derived from TextComponent, so if you are looking for a method that should obviously be there, you should check TextComponent right away.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.awt.*; public class TextEntryExample { public static void main(String[] args) { Frame baseFrame = new Frame(); baseFrame.setLayout(new GridLayout(2,1)); baseFrame.reshape(10,10,200,200); baseFrame.setTitle("Label Example"); TextField myTextField = new TextField(); myTextField.setText("Here is some " + "default and selected text"); myTextField.setSelectionStart(0); myTextField.setSelectionEnd(4); myTextField.setEditable(false); TextArea myTextArea = new TextArea ("The Text Area widget \n" + "allows multiple line text entry." + "it is also worth \n noting that it " + "will dynamically create scroll " + "bars \n if the text goes beyond the " + "boundaries of the widget"); baseFrame.add(myTextField); baseFrame.add(myTextArea); baseFrame.show(); } } |
When compiled and executed, the code above produces the following interface:
Check Boxes and Check Box Groups
The Check Box and Check Box Groups are very familiar components which allow a user to make a choice between several options. The Check Box component is used to give the user the ability to select or unselect some choice. The Check Box group allows the developer to group a set of check boxes in such a way that only one check box can be selected at any one time (On day one, we saw this type of widget in HTML FORM programming under the name “RADIO”). In the following example we create both single check boxes and checkboxes in a group. Notice that you can select or unselect AFC or JDK without affecting any other check boxes. However, you can choose only male or female from the sex group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.awt.*; public class CheckExample { public static void main(String[] args) { Frame baseFrame = new Frame(); baseFrame.setLayout(new GridLayout(4,1)); baseFrame.reshape(10,10,200,200); baseFrame.setTitle("Check Example"); Checkbox jdkChoice = new Checkbox("JDK"); Checkbox afcChoice = new Checkbox("AFC"); CheckboxGroup sexGroup = new CheckboxGroup(); Checkbox maleChoice = new Checkbox("male", sexGroup, false); Checkbox femaleChoice = new Checkbox("Female", sexGroup, false); baseFrame.add(jdkChoice); baseFrame.add(afcChoice); baseFrame.add(maleChoice); baseFrame.add(femaleChoice); baseFrame.show(); } } |
Choices
One of the problems with Check Boxes and Check Box Groups is that they take up a lot of space in your interface. If you have a lot of choices, it may be better to present them in a Choice control (similar to the SELECT widget in HTML FORM programming)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.awt.*; public class ChoiceExample { public static void main(String[] args) { Frame baseFrame = new Frame(); baseFrame.setLayout(new GridLayout(4,1)); baseFrame.reshape(10,10,200,200); baseFrame.setTitle("Choice Example"); Choice choice = new Choice(); choice.addItem("Selena"); choice.addItem("Eric"); choice.addItem("Mark"); baseFrame.add(choice); baseFrame.show(); } } |