1. You use JButton jbt = new JButton("OK") to create a button b with the label OK. You use jbt.setText("New label") to change the label to New label, for example. Use the setIcon() method to set an icon in the button.
2. You use JLabel jlbl = new JLabel("Address") to create a label named Address, and jlbl.setText("New Name") to change the name to New Name. Use the setIcon() method to set an icon in the label..
3. JTextField jtf = new JTextField("Welcome to Java", 10)
4. You use JTextArea jta = new JTextArea(10, 20) to create a text area with 10 rows and 20 columns in the viewing area, and jta.append() or jta.insert() to add new lines into the text area. To make the text area to scroll, you need to add the text area to a JScrollPane.
5. You use JComboBox jcbo = new JComboBox() to create a combo box, jcbo.addItem(Object s) to add a string item, and c.getItem(int Index) to retrieve an item. Therefore, to add three items, the code might look like this:
jcbo.addItem("Item 1");
jcbo.addItem("Item 2");
jcbo.addItem("Item 3");
6. You use JCheckBox jchk = new JCheckBox("Red"), for example, to create a check box and jchk.isSelected()to check whether it is checked.
7. You use JRadioButton jrb = new JRadioButton(), for example, to create a radio button and use jrb.isSelected() to check if a radio button is selected. To group radio buttons, create an instance of ButtonGroup, and add radio buttons to this instance.
8. You can have a border for any subclass of JComponent. To create a titled border on a JPanel, create an instance of the titled border using the TitledBorder constructor, and add the instance to the JPanel using the setBorder() method.
9. // Create JMenuBar mb
JMenuBar mb = new JMenuBar ();
setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
mb.add(editMenu);
JMenu viewMenu = new JMenu("View");
mb.add(viewMenu);
JMenu insertMenu = new JMenu("Insert");
mb.add(insertMenu);
JMenu formatMenu = new JMenu("Format");
mb.add(formatMenu);
JMenu helpMenu = new JMenu("Help");
mb.add(helpMenu);
//add JMenuItems
viewMenu.add(toolbarMT= new JMenuItem("Toolbar"));
viewMenu.add(formatbarMT= new JMenuItem("Format Bar"));
viewMenu.add(rulerMT= new JMenuItem("Ruler"));
viewMenu.add(statusbarMT= new JMenuItem("Status Bar"));
viewMenu.add(optionsMT= new JMenuItem("Options"));
10. You use the default constructor of JScrollBar to create a scroll bar and use the setOrientation(JScrollBar.VERTICAL) to display it vertically.
11. Create a JLabel and set the icon for the image in the label. Add the label to a JScrollPane.
12. You can use the method
JOptionPane.showMessageDialog
(Component parentComponent,
Object message,
String title,
int messageType)
to create a message dialog box. The message types are ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE,
WARNING_MESSAGE, and QUESTION_MESSAGE
13. See the section “Creating Multiple Windows.”
14. The doLayout() method causes the components in the container to be laid out again. If you have dropped components or added new components after the container is displayed, you need to apply the doLayout() method on the container.
15. Each corner view is an individual component. So, you need to create four separate objects.