package buttons;
/*
Chapter 6: Borders
Programmer:
Date:
Filename: Borders.java
Purpose:
*/
import java.awt.*;
import java.awt.event.*;
public class Buttons extends Frame implements ActionListener, ItemListener
{
public Buttons()
{
//set the layout
setLayout(new BorderLayout(20,5));
//Add buttons
Button red = new Button("Red");
Button yellow = new Button("Yellow");
Button cyan = new Button("Cyan");
Button magenta = new Button("Magenta");
//Button white = new Button("White");
Choice colors = new Choice();
colors.add("Red");
colors.add("Yellow");
colors.add("Cyan");
colors.add("Magenta");
colors.add("White");
red.addActionListener(this);
yellow.addActionListener(this);
cyan.addActionListener(this);
magenta.addActionListener(this);
//white.addActionListener(this);
colors.addItemListener(this);
add(red, BorderLayout.NORTH);
add(yellow, BorderLayout.SOUTH);
add(cyan, BorderLayout.EAST);
add(magenta, BorderLayout.WEST);
//add(white, BorderLayout.CENTER);
add(colors, BorderLayout.CENTER);
//override the windowClosing event
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String[] args)
{
// set frame properties
Buttons f = new Buttons();
f.setTitle("Border Application");
f.setBounds(200,200,300,300);
f.setVisible(true);
f.setBackground(Color.RED);
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Red")
{
setBackground(Color.RED);
}
else if (arg == "Yellow")
{
setBackground(Color.YELLOW);
}
else if (arg == "Cyan")
{
setBackground(Color.CYAN);
}
else if (arg == "Magenta")
{
setBackground(Color.MAGENTA);
}
else
{
setBackground(Color.WHITE);
}
}
public void itemStateChanged(ItemEvent ie)
{
int arg = ie.getStateChange();
if (arg == 0)
{
setBackground(Color.RED);
}
else if (arg == 1)
{
setBackground(Color.YELLOW);
}
else if (arg == 2)
{
setBackground(Color.CYAN);
}
else if (arg == 3)
{
setBackground(Color.MAGENTA);
}
else
{
setBackground(Color.WHITE);
}
}
}
ASKER
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY