• No results found

Menu Bars and Menus

In document Java (Page 148-157)

Explain DataOutput interface ?

Chapter 7 EVENT HANDLING

11. Menu Bars and Menus

 A window can have a menu bar associated with it. A menu bar displays a list of top-level menu choices.

 Each choice is associated with a drop-down menu. This concept is implemented by the following classes:

 MenuBar, Menu and MenuItem. In short, a menu bar contains one or more Menu objects.

 Each menu object contains a list of MenuItem objects.

 Each MenuItem object represents something that can be selected by the user.

 since Menu is a subclass of MenuItem, a hierarchy of nested submenus can be created. It is also possible to include checkable menu items.

 These are menu options of type CheckboxMenuItem and will have a check mark next to them when they are selected.

 To create a menu bar, first create an instance of MenuBar.  This class only defines the default constructor.

 Next, create instances of Menu that will define the selections displayed on the bar. Following are the constructors for Menu:

Menu()

Menu(String optionName)

Menu(String optionName, Boolean removable)

 Here, optionName specifies the name of the menu selection.

 If removable is true, the pop-up menu can be removed and allowed to float free.

 Otherwise, it will remain attached to the menu bar.

Individual menu items are of type MenuItem. It defines these constructors: MenuItem()

MenuItem(String itemName)

MenuItem(String itemName, MenuShortcut keyAccel)

 Here, itemName is the name shown in the menu, and keyAccel is the menu shortcut for this item.

You can disable or enable a menu item by using the setEnabled() method. Its form is shown here:

Void setEnabled(Boolean enabledFlag)

 If the argument enabledFlag is true, the menu item is enabled.  If false, the menu item is disabled.

 You can determine an item‟s status by calling isEnabled(). This method is shown here:

Boolean isEnabled()

 isEnabled() returns true if the menu item on which it is called is enabled. Otherwise, it returns false.

 You can change the name of a menu item by calling setLable(). You can retrieve the current name by using getLable(). These methods are as follows:

Void setLabel(String newName) String getLabel()

 Here, newName becomes the new name of the invoking menu item. getLabel() returns the current name.

 You can create a checkable menu item by using a subclass of MenuItem called CheckboxMenuItem. It has these constructors:

CheckboxMenuItem()

CheckboxMenuItem(String itemName)

 Here, itemName is the name shown in the menu.

 Checkable items operate as toggles. Each time is selected, its state changes. In the first two forms, the checkable entry is unchecked.

 In the third form, if on is true, the checkable entry is initially checked. Otherwise, it is cleared.

 You can obtain the status of a checkable item by calling getState().  You can set it to a known state by using setState().

 These methods are shown here: Boolean getState()

Void setState(Boolean checked)

 If the item is checked, getState() returns true.

 Otherwise, it returns false. To check an item, pass true to setState(). To clear an item, pass false.

 Once you have created a menu item, you must add the item to a Menu object by using add(), which has the following general form:

MenuItem add(MenuItem item) Here, item is the item being added.

 Once you have added all items to a Menu object, you can add that object to the menu bar by using this version of add() defined by MenuBar:

Menu add(Menu menu)

Here, menu is the menu being added. The menu is returned. import java.awt.*;

import java.awt.event.*;

class MainFrame extends Frame {

MainFrame(String title) {

super(title);

MenuBar mb = new MenuBar(); setMenuBar(mb);

Menu file = new Menu("File");

MenuItem New = new MenuItem("New.. "); MenuItem Open = new MenuItem("Open.. "); MenuItem Save = new MenuItem("Save.. "); MenuItem Exit = new MenuItem("Exit.. "); file.add(New);

file.add(Open); file.add(Save); file.add(Exit);

Menu sub = new Menu("edit");

MenuItem Cut = new MenuItem("Cut"); MenuItem Copy = new MenuItem("Copy"); MenuItem Paste = new MenuItem("Paste"); sub.add(Cut); sub.add(Copy); sub.add(Paste); file.add(sub); mb.add(file); }

public static void main(String args[]) {

MainFrame mf = new MainFrame("Menu"); mf.setSize(300, 300);

mf.setVisible(true); }

}

12. Dialog Box

 Dialog boxes are used to obtain user input. They are similar to frame windows, except that dialog boxes are always child windows of a top-level window.

 Dialog boxes do not have menu bars. Dialog boxes can be modal or modeless.

 When a modal dialog box is active, all input is directed to it until it is closed. This means that you cannot access other parts of your program until you have closed the dialog box.

 When a modeless dialog box is active, input focus can be directed to another window in your program. Thus, other parts of your program remain active and accessible.

 Constructors for dialog box are:

Dialog(Frame parentWindow, Boolean mode)

Dialog(Frame parentWindow, String title, Boolean mode)

Here, parentWindow is the owner of the dialog box. If mode is true, the dialog box is modal. Otherwise, it is modeless. The title of the dialog box can be passed in title.

CHAPTER 9

Explain Swing Layout Controls ?

[

Nov / Dec – 2008] , [Oct / Nov – 2009]

 Swing Layout control is use to set the layout  Swing Layout consider many layout as follows

Box Lay out

Example : import java.awt.*; import javax.swing.*; /* <JAPPLET CODE = boxlayout.class WIDTH = 250 HEIGHT = 200 > </JAPPLET> */

public class boxlayout extends JJApplet {

public void init() {

Container contentPane = getContentPane(); JPanel jpanel1, jpanel2, jpanel3;

jpanel1 = new JPanel(); jpanel2 = new JPanel(); jpanel3 = new JPanel();

jpanel1.setLayout(new BoxLayout(jpanel1, BoxLayout.Y_AXIS)); jpanel2.setLayout(new BoxLayout(jpanel2, BoxLayout.X_AXIS)); jpanel3.setLayout(new BoxLayout(jpanel3, BoxLayout.Y_AXIS)); contentPane.setLayout(new FlowLayout()); jpanel1.add(new JTextField("Text 1")); jpanel1.add(new JTextField("Text 2")); jpanel1.add(new JTextField("Text 3")); jpanel1.add(new JTextField("Text 4")); jpanel2.add(new JTextField("Text 1")); jpanel2.add(new JTextField("Text 2")); jpanel2.add(new JTextField("Text 3")); jpanel2.add(new JTextField("Text 4")); jpanel3.add(new JTextField("Text 1")); jpanel3.add(new JTextField("Text 2")); jpanel3.add(new JTextField("Text 3")); jpanel3.add(new JTextField("Text 4")); contentPane.add(jpanel1);

contentPane.add(jpanel2); contentPane.add(jpanel3); } } Flow Layout import java.awt.*; import javax.swing.*; import javax.swing.border.*; /* <JAPPLET CODE = box.class WIDTH = 450 HEIGHT = 400 > </JAPPLET> */

public class box extends JJApplet {

public void init() {

Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JPanel jpanel1 = new JPanel();

jpanel1.setBorder(

BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Glue"));

jpanel1.setLayout(new BoxLayout(jpanel1, BoxLayout.X_AXIS)); jpanel1.add(Box.createGlue()); jpanel1.add(new JTextField("Text 1")); jpanel1.add(Box.createGlue()); jpanel1.add(new JTextField("Text 2")); jpanel1.add(Box.createGlue()); jpanel1.add(new JTextField("Text 3")); jpanel1.add(Box.createGlue()); contentPane.add(jpanel1); JPanel jpanel2 = new JPanel(); jpanel2.setBorder(

BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Struts"));

jpanel2.setLayout(new BoxLayout(jpanel2, BoxLayout.X_AXIS)); jpanel2.add(new JTextField("Text 1"));

jpanel2.add(Box.createHorizontalStrut(20)); jpanel2.add(new JTextField("Text 2"));

jpanel2.add(Box.createHorizontalStrut(20)); jpanel2.add(new JTextField("Text 3")); contentPane.add(jpanel2);

JPanel jpanel3 = new JPanel(); jpanel3.setBorder(

BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Rigid"));

jpanel3.setLayout(new BoxLayout(jpanel3, BoxLayout.X_AXIS)); jpanel3.add(Box.createRigidArea(new Dimension(10, 40))); jpanel3.add(new JTextField("Text 1")); jpanel3.add(Box.createRigidArea(new Dimension(10, 40))); jpanel3.add(new JTextField("Text 2")); jpanel3.add(Box.createRigidArea(new Dimension(10, 40))); jpanel3.add(new JTextField("Text 3")); contentPane.add(jpanel3); JPanel jpanel4 = new JPanel(); jpanel4.setBorder(

BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Glue"));

jpanel4.setLayout(new BoxLayout(jpanel4, BoxLayout.Y_AXIS)); jpanel4.add(Box.createGlue()); jpanel4.add(new JTextField("Text 1")); jpanel4.add(Box.createGlue()); jpanel4.add(new JTextField("Text 2")); jpanel4.add(Box.createGlue()); jpanel4.add(new JTextField("Text 3")); jpanel4.add(Box.createGlue()); contentPane.add(jpanel4); JPanel jpanel5 = new JPanel(); jpanel5.setBorder(

BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Struts"));

jpanel5.setLayout(new BoxLayout(jpanel5, BoxLayout.Y_AXIS)); jpanel5.add(new JTextField("Text 1")); jpanel5.add(Box.createVerticalStrut(30)); jpanel5.add(new JTextField("Text 2")); jpanel5.add(Box.createVerticalStrut(30)); jpanel5.add(new JTextField("Text 3")); contentPane.add(jpanel5);

JPanel jpanel6 = new JPanel(); jpanel6.setBorder(

BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(),

"Rigid"));

jpanel6.setLayout(new BoxLayout(jpanel6, BoxLayout.Y_AXIS)); jpanel6.add(Box.createRigidArea(new Dimension(40, 60))); jpanel6.add(new JTextField("Text 1")); jpanel6.add(Box.createRigidArea(new Dimension(40, 60))); jpanel6.add(new JTextField("Text 2")); jpanel6.add(Box.createRigidArea(new Dimension(40, 60))); jpanel6.add(new JTextField("Text 3")); contentPane.add(jpanel6); } }

1. JLabels [March/April -2009], [Oct / Nov – 2009]

 A JLabels is a string that appears on a graphical user interface. JLabels s are passive controls that do not support any interaction with the user.  It can be changed by your program but cannot be changed by a user. The

JLabels class defines these constructors: JLabel ()

JLabel (String str)

JLabel (String str, int align) Here, str is the text for the JLabels .

 Align is a constant that indicates if the JLabels should be left justified, centered, or right justified. The constants are LEFT, CENTER, or RIGHT. Example

import java.JApplet.*; import java.awt.*; import java.swing.*; /*

<JApplet code = "JLabels " width = 100 height = 100> </JApplet>

*/

public class JLabels extends JApplet {

public void init() {

String s1 = "Rome1"; String s2 = "Rome2"; String s3 = "Rome3";

JLabels l1 = new JLabels (s1, JLabels .LEFT); JLabels l2 = new JLabels (s2, JLabels .CENTER); JLabels l3 = new JLabels (s3, JLabels .RIGHT); add(l1);

add(l3); }

}

2. JButtons [March/April -2009] , [Oct / Nov – 2009]

 A JButton is a component that appears as a push JButton.

 A JButton is a component that contains a JLabels and that generates an event when it is pressed. The JButton class defines these constructors:

JButton()

JButton(String str)

Here, str is the text for the JButton.

 An action event is generated each time a JButton is pressed.

 The methods by which objects register and unregistered to receive action events generated by this component are:

Void addActionListener(ActionListener all) Void removeActionListener(ActionListener all) Here, all is an action listener.

 An action listener use getActionCommand() method to obtain a command string associated with an action event generated by the JButton.

 setActionCommand(String str) sets the string that is returned by the getActionCommand(). Example import java.JApplet.*; import java.awt.*; import java.awt.event.*; import java.swing.*; /*

<JApplet code = "awt2" width = 200 height = 200> </JApplet>

*/

public class awt2 extends JApplet implements ActionListener {

JLabels l1; public void init() {

JButton b1 = new JButton("Yellow"); JButton b2 = new JButton("Red"); JButton b3 = new JButton("Blue");

add(b1); add(b2); add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); l1=new JLabels (" "); add(l1); }

public void actionPerformed(ActionEvent ae) {

l1.setText(ae.getActionCommand()); }

}

In document Java (Page 148-157)

Related documents