Sie sind auf Seite 1von 8

Creating Menus and MenuItems

import java.awt.*;
class AWTMenu extends Frame
{
MenuBar mbar;
Menu menu,submenu;
MenuItem m1,m2,m3,m4,m5;

public AWTMenu()
{
// Set frame properties
setTitle("AWT Menu"); // Set the title
setSize(400,400); // Set size to the frame
setLayout(new FlowLayout()); // Set the layout
setVisible(true); // Make the frame visible
setLocationRelativeTo(null); // Center the frame

// Create the menu bar


mbar=new MenuBar();

// Create the menu


menu=new Menu("Menu");

// Create the submenu


submenu=new Menu("Sub Menu");

// Create MenuItems
m1=new MenuItem("File);
m2=new MenuItem("Edit");
m3=new MenuItem("Menu Item 3");

m4=new MenuItem("Menu Item 4");


m5=new MenuItem("Menu Item 5");

// Attach menu items to menu


menu.add(m1);
menu.add(m2);
menu.add(m3);

// Attach menu items to submenu


submenu.add(m4);
submenu.add(m5);

// Attach submenu to menu


menu.add(submenu);

// Attach menu to menu bar


mbar.add(menu);

// Set menu bar to the frame


setMenuBar(mbar);
}
public static void main(String args[])
{
new AWTMenu();
}
}

AWT Dialog Box

import java.awt.*;
import java.awt.geom.*;
class AWTDialog extends Frame
{
Dialog d1,d2;

public AWTDialog()
{
createAndShowGUI();
}

private void createAndShowGUI()


{
setTitle("AWT Dialog in Java Example");

// A dialog with current frame as parent


d1=new Dialog(this);

// Set the title


d1.setTitle("Dialog 1");

d1.setSize(400,400);

// Hide title bar and borders


d1.setUndecorated(true);

// Set background for dialog


d1.setBackground(Color.GRAY);

// Set opacity
d1.setOpacity(0.4f);

// Set d1 location relative to desktop


// d1 appears at center of screen
d1.setLocationRelativeTo(null);

// Set some shape to d1


RoundRectangle2D.Double r=new
RoundRectangle2D.Double(0,0,400,400,20,20);

// Set the rounded rectangle shape


d1.setShape(r);
d1.setVisible(true);

// Create a dialog with current frame as parent


// with title Simple Dialog
d2=new Dialog(d1,"Simple Dialog");

// d1 cannot be made active if d2 isn't closed


// this is theme behind setting a dialog modal
// default value is false
d2.setModal(true);

// Set size
d2.setSize(150,150);

// Set layout
d2.setLayout(new FlowLayout());

// Make it non resizable


d2.setResizable(false);

// Set d2 location relative to d1


// d2 appears at center of d1
d2.setLocationRelativeTo(d1);

// Add some components


d2.add(new Button("Button"));
d2.add(new Checkbox("Checkbox"));
d2.add(new TextField(20));

// Show the dialog


d2.setVisible(true);
setSize(400,400);
setVisible(true);
}

public static void main(String args[])


{
new AWTDialog();
}
}

AWT FileDialog

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FileDialogExample extends JFrame
{
JFramethisFrame;
JComboBoxjb;
JButton b;
public FileDialogExample()
{
// Store current object in thisFrame
thisFrame=this;
createAndShowGUI();
}

private void createAndShowGUI()


{

// Set frame properties


setTitle("File Dialog Example");
setSize(500,500);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

// Create JComboBox
jb=new JComboBox();

// Add items to the frame


jb.addItem("LOAD"); // 0 index
jb.addItem("SAVE"); // 1 index

b=new JButton("Fire!");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEventae)
{
// Create a FileDialog whose parent is this frame and with title
selected in JComboBox
FileDialogfd=newFileDialog(thisFrame,(String)jb.getSelectedItem());

// Set a dir
fd.setDirectory("E:\\java");

// Set FileDialog.LOAD=0 or FileDialog.SAVE=1


fd.setMode(jb.getSelectedIndex());

// Make it visible
fd.setVisible(true);
}
});
// Add JComboBox
add(jb);

// Add button
add(b);
}

public static void main(String args[])


{
new FileDialogExample();
}
}

Create a panel in AWT


import java.awt.*;
class AWTPanel extends Frame
{
Panel panel;
Button b1,b2;
public AWTPanel()
{

// Set frame properties


setTitle("AWT Panel"); // Set the title
setSize(400,400); // Set size to the frame
setLayout(new FlowLayout()); // Set the layout
setVisible(true); // Make the frame visible
setLocationRelativeTo(null); // Center the frame

// Create the panel


panel=new Panel();

// Set panel background


panel.setBackground(Color.gray);
// Create buttons
b1=new Button(); // Create a button with default constructor
b1.setLabel("I am button 1"); // Set the text for button

b2=new Button("Button 2"); // Create a button with sample text


b2.setBackground(Color.lightGray); // Set the background to the
button

// Add the buttons to the panel


panel.add(b1);
panel.add(b2);

// Add the panel to the frame


add(panel);

}
public static void main(String args[])
{
new AWTPanel();
}
}

Das könnte Ihnen auch gefallen