home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java 1.2 How-To
/
JavaHowTo.iso
/
code
/
ch08.txt
< prev
next >
Wrap
Text File
|
1998-12-14
|
16KB
|
731 lines
OneButton_Applet.java:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class OneButton_Applet extends JApplet
{
public void init()
{
JButton button1 = new JButton("Push Me");
setBackground(Color.lightGray);
getContentPane().add(button1,"Center");
}//init()
}//OneButton_Applet
BorderLayout1.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BorderLayout1 extends JPanel
{
//The constructor initializes the application
public BorderLayout1()
{
JButton btnNorth;
JButton btnSouth;
JButton btnEast;
JButton btnWest;
JButton btnCenter;
Font font1;
font1 = new Font("Times-Roman",Font.BOLD,18);
this.setLayout(new BorderLayout());
btnNorth = new JButton("North");
btnNorth.setFont(font1);
add(btnNorth, "North");
btnSouth = new JButton("South");
btnSouth.setFont(font1);
add(btnSouth, "South");
btnEast = new JButton("East");
btnEast.setFont(font1);
add(btnEast, "East");
btnWest = new JButton("West");
btnWest.setFont(font1);
add( btnWest, "West");
btnCenter = new JButton("Center");
btnCenter.setFont(font1);
add(btnCenter, "Center");
}//constructor
//main entry point for the application
public static void main(String s[])
{
JFrame f = new JFrame("Border Layout");
BorderLayout1 panel = new BorderLayout1();
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel,"Center");
f.setSize(400, 150);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class
//This class handles the closing of the application
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
FlowLayout1.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FlowLayout1 extends JPanel
{
public FlowLayout1()
{
JButton btnOne;
JButton btnTwo;
JButton btnThree;
JButton btnFour;
this.setLayout(new FlowLayout(FlowLayout.LEFT));
btnOne = new JButton("One");
add(btnOne);
btnTwo = new JButton("Two");
add(btnTwo);
btnThree = new JButton("Three");
add(btnThree);
btnFour = new JButton("Four");
add(btnFour);
}//constructor
public static void main(String s[])
{
JFrame f = new JFrame("Flow Layout");
FlowLayout1 panel = new FlowLayout1();
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel, "Center");
f.setSize(400,100);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
NoLayout1.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NoLayout1 extends JPanel
{
public NoLayout1()
{
JButton btnOne;
JButton btnTwo;
JButton btnThree;
JButton btnFour;
this.setLayout(null);
btnOne = new JButton("One");
btnOne.setBounds(0,0,100,100);
add(btnOne);
btnTwo = new JButton("Two");
btnTwo.setBounds(100,0,100,50);
add(btnTwo);
btnThree = new JButton("Three");
btnThree.setBounds(0,100,75,100);
add(btnThree);
btnFour = new JButton("Four");
btnFour.setBounds(115,115,100,100);
add(btnFour);
}//constructor
public static void main(String s[])
{
JFrame f = new JFrame("No Layout");
NoLayout1 panel = new NoLayout1();
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel, "Center");
f.setSize(250,250);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
MouseLocation.java:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MouseLocation extends JPanel
{
public MouseLocation()
{
setToolTipText("xxx");
setBackground(Color.white);
}//constructor
public String getToolTipText(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
String str = "(" + x + "," + y + ")";
return str;
}//getToolTipText
public static void main(String s[])
{
JFrame f = new JFrame("MouseLocation Example");
MouseLocation panel = new MouseLocation();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel,"Center");
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class MouseLocation
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
IconButtons.java:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class IconButtons extends JPanel
{
public IconButtons()
{
ImageIcon buttonImage = new ImageIcon("images/T1.gif");
ImageIcon pressImage = new ImageIcon("images/T2.gif");
ImageIcon selImage = new ImageIcon("images/T3.gif");
ImageIcon disabledImage = new ImageIcon("images/T4.gif");
ImageIcon rolloverImage = new ImageIcon("images/T4.gif");
ImageIcon selrolloverImage = new ImageIcon("images/T3.gif");
ImageIcon DOJImage = new ImageIcon("images/T2.gif");
ImageIcon shapeImage = new ImageIcon("images/T1.gif");
AbstractButton button1;
AbstractButton button2;
AbstractButton button3;
Font font1;
font1 = new Font("Serif", Font.PLAIN,16);
setFont(font1);
setDoubleBuffered(true);
setLayout(new GridLayout(1,3,5,5));
button1 = new JButton();
button1.setIcon(buttonImage);
button1.setPressedIcon(pressImage);
button1.setRolloverIcon(rolloverImage);
add(button1);
button2 = new JButton();
button2.setIcon(buttonImage);
button2.setDisabledIcon(disabledImage);
button2.setEnabled(false);
add(button2);
button3 = new JToggleButton();
button3.setIcon(DOJImage);
button3.setSelectedIcon(shapeImage);
button3.setRolloverIcon(shapeImage);
button3.setRolloverSelectedIcon(selrolloverImage);
add(button3);
}//constructor
public static void main(String s[])
{
JFrame f = new JFrame("Icon Buttons ");
IconButtons panel = new IconButtons();
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel, "Center");
f.setSize(200,70);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//IconButtons
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
Action1.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Action1 extends JPanel implements ActionListener
{
public Action1()
{
JButton button1;
JRadioButton radioButton1;
JRadioButton radioButton2;
ButtonGroup grp;
//Buffer to reduce flicker
setDoubleBuffered(true);
button1 = new JButton("Button");
button1.addActionListener(this);
button1.setActionCommand("Button Activated");
add(button1);
grp = new ButtonGroup();
radioButton1 = new JRadioButton("One");
radioButton1.addActionListener(this);
radioButton1.setActionCommand("One Activated");
grp.add(radioButton1);
add(radioButton1);
radioButton2 = new JRadioButton("Two");
radioButton2.addActionListener(this);
radioButton2.setActionCommand("Two Activated");
grp.add(radioButton2);
add(radioButton2);
}//constructor
public void actionPerformed(ActionEvent e)
{
String cmd;
Object source;
source = e.getSource();
cmd = e.getActionCommand();
System.out.println("Action: "+cmd+"\n\tperformed by: "+source);
System.out.println();
}//actionPerformed
public static void main(String s[])
{
JFrame f = new JFrame("Action1 ");
Action1 panel = new Action1();
f.getContentPane().add(panel, "Center");
f.setSize(200,100);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class Actions
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
List1.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class List1 extends JPanel
implements ListSelectionListener
{
JTextField actField;
JList list;
public List1()
{
String items[] = { "GA", "AL", "DC","NY",
"CA","UT","FL"};
JPanel footer;
JPanel tmp;
setLayout( new BorderLayout());
setBackground(Color.lightGray);
//Turn on buffering
setDoubleBuffered(true);
list = new JList(items);
list.addListSelectionListener(this);
add(new JScrollPane(list), "Center");
footer = new JPanel();
footer.setLayout(new GridLayout(3,1,5,5));
tmp = new JPanel();
tmp.add(new JLabel("Sel values:"));
actField = new JTextField(20);
tmp.add(actField);
footer.add(tmp);
add(footer, "South");
}//constructor
public void valueChanged(ListSelectionEvent e)
{
int first, last;
int i;
String newVal = "";
ListModel listData = list.getModel();
Object selValues[];
//Display the selected values
selValues = list.getSelectedValues();
if (selValues != null)
last = selValues.length;
else
last = 0;
newVal = "";
for(i=0;i<last;i++)
{
if(i!=0) newVal+=" ";
newVal += selValues[i].toString();
}
actField.setText(newVal);
}
public static void main(String s[])
{
JFrame f = new JFrame("List1");
List1 panel = new List1();
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel, "Center");
f.setSize(350,350);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser
Menu1.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Menu1 extends JPanel
implements ActionListener, MenuListener
{
JTextField fldStatus;
//Create the menu class
public Menu1(JFrame frm)
{
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Strategy");
JMenuItem tmp;
setBackground(Color.lightGray);
setLayout(new BorderLayout());
setDoubleBuffered(true);
menu.addMenuListener(this);
tmp = new JMenuItem("Lead");
tmp.addActionListener(this);
tmp.setActionCommand("Lead");
menu.add(tmp);
tmp = new JMenuItem("Follow");
tmp.addActionListener(this);
tmp.setActionCommand("Follow");
menu.add(tmp);
tmp = new JMenuItem("Resign");
tmp.addActionListener(this);
tmp.setActionCommand("Resign");
menu.add(tmp);
tmp = new JMenuItem("Quit");
tmp.addActionListener(this);
tmp.setActionCommand("Quit");
menu.add(tmp);
bar.add(menu);
frm.setJMenuBar(bar);
fldStatus = new JTextField(10);
add(fldStatus,"South");
}//constructor
public void actionPerformed(ActionEvent e)
{
String cmd;
cmd = e.getActionCommand();
if (cmd.equals("Lead"))
{
fldStatus.setText("Action: Lead");
}
if (cmd.equals("Follow"))
{
fldStatus.setText("Action: Follow");
}
if (cmd.equals("Resign"))
{
fldStatus.setText("Action: Resign");
}
if (cmd.equals("Quit"))
{
System.exit(0);
}
}
public void menuSelected(MenuEvent e)
{
fldStatus.setText("Menu Selected");
}
public void menuDeselected(MenuEvent e)
{
fldStatus.setText("Menu Deselected");
}
public void menuCanceled(MenuEvent e)
{
fldStatus.setText("Menu Cancelled");
}
public static void main(String s[])
{
JFrame f = new JFrame("Menu1");
Menu1 panel = new Menu1(f);
f.setForeground(Color.black);
f.setBackground(Color.lightGray);
f.getContentPane().add(panel, "Center");
f.setSize(300,200);
f.setVisible(true);
f.addWindowListener(new WindowCloser());
}//main
}//class
//close down gracefully
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}//windowClosing
}//class WindowCloser