home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / code / ch08.txt < prev    next >
Text File  |  1998-12-14  |  16KB  |  731 lines

  1. OneButton_Applet.java:
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. import java.awt.*;
  7.  
  8. public class OneButton_Applet extends JApplet
  9. {
  10.     public void init()
  11.     {
  12.         JButton button1 = new JButton("Push Me");
  13.  
  14.         setBackground(Color.lightGray);
  15.         getContentPane().add(button1,"Center");
  16.     }//init()
  17.  
  18.  
  19. }//OneButton_Applet
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. BorderLayout1.java:
  29.  
  30. import javax.swing.*;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33.  
  34. public class BorderLayout1 extends JPanel
  35. {
  36.  
  37. //The constructor initializes the application
  38.  
  39.     public BorderLayout1()
  40.     {
  41.         JButton btnNorth;
  42.         JButton btnSouth;
  43.         JButton btnEast;
  44.         JButton btnWest;
  45.         JButton btnCenter;
  46.  
  47.         Font font1;
  48.  
  49.         font1 = new Font("Times-Roman",Font.BOLD,18);
  50.  
  51.         this.setLayout(new BorderLayout());
  52.  
  53.         btnNorth = new JButton("North");
  54.         btnNorth.setFont(font1);
  55.         add(btnNorth, "North");
  56.  
  57.         btnSouth = new JButton("South");
  58.         btnSouth.setFont(font1);
  59.         add(btnSouth, "South");
  60.  
  61.         btnEast = new JButton("East");
  62.         btnEast.setFont(font1);
  63.         add(btnEast, "East");
  64.  
  65.         btnWest = new JButton("West");
  66.         btnWest.setFont(font1);
  67.         add( btnWest, "West");
  68.  
  69.         btnCenter = new JButton("Center");
  70.         btnCenter.setFont(font1);
  71.         add(btnCenter, "Center");
  72.  
  73.     }//constructor
  74.  
  75. //main entry point for the application
  76. public static void main(String s[])
  77.    {
  78.         JFrame f = new JFrame("Border Layout");
  79.         BorderLayout1 panel = new BorderLayout1();
  80.  
  81.         f.setForeground(Color.black); 
  82.         f.setBackground(Color.lightGray);
  83.         f.getContentPane().add(panel,"Center");
  84.  
  85.         f.setSize(400, 150);
  86.         f.setVisible(true);
  87.         f.addWindowListener(new WindowCloser());
  88.  
  89.     }//main
  90. }//class
  91.  
  92.  
  93. //This class handles the closing of the application
  94. class WindowCloser extends WindowAdapter
  95. {
  96.     public void windowClosing(WindowEvent e)
  97.     {
  98.         Window win = e.getWindow();
  99.         win.setVisible(false);
  100.         win.dispose();
  101.         System.exit(0);
  102.     }//windowClosing
  103. }//class WindowCloser
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. FlowLayout1.java:
  111.  
  112. import javax.swing.*;
  113. import java.awt.*;
  114. import java.awt.event.*;
  115.  
  116.  
  117. public class FlowLayout1 extends JPanel
  118. {
  119.     public FlowLayout1()
  120.     {
  121.         JButton btnOne;
  122.         JButton btnTwo;
  123.         JButton btnThree;
  124.         JButton btnFour;
  125.  
  126.         this.setLayout(new FlowLayout(FlowLayout.LEFT));
  127.  
  128.         btnOne = new JButton("One");
  129.         add(btnOne);
  130.  
  131.         btnTwo = new JButton("Two");
  132.         add(btnTwo);
  133.  
  134.         btnThree = new JButton("Three");
  135.         add(btnThree);
  136.  
  137.         btnFour = new JButton("Four");
  138.         add(btnFour);
  139.  
  140.     }//constructor
  141.  
  142.  
  143.  
  144.     public static void main(String s[])
  145.     {
  146.         JFrame f = new JFrame("Flow Layout");
  147.         FlowLayout1 panel = new FlowLayout1();
  148.  
  149.         f.setForeground(Color.black);
  150.         f.setBackground(Color.lightGray);
  151.         f.getContentPane().add(panel, "Center");
  152.  
  153.         f.setSize(400,100);
  154.         f.setVisible(true);
  155.         f.addWindowListener(new WindowCloser());
  156.  
  157.  
  158.     }//main
  159. }//class
  160.  
  161.  
  162. class WindowCloser extends WindowAdapter
  163. {
  164.     public void windowClosing(WindowEvent e)
  165.     {
  166.         Window win = e.getWindow();
  167.         win.setVisible(false);
  168.         win.dispose();
  169.         System.exit(0);
  170.     }//windowClosing
  171. }//class WindowCloser
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180. NoLayout1.java:
  181.  
  182. import javax.swing.*;
  183. import java.awt.*;
  184. import java.awt.event.*;
  185.  
  186. public class NoLayout1 extends JPanel
  187. {
  188.     public NoLayout1()
  189.     {
  190.         JButton btnOne;
  191.         JButton btnTwo;
  192.         JButton btnThree;
  193.         JButton btnFour;
  194.  
  195.         this.setLayout(null);
  196.  
  197.         btnOne = new JButton("One");
  198.         btnOne.setBounds(0,0,100,100);
  199.         add(btnOne); 
  200.  
  201.         btnTwo = new JButton("Two");
  202.         btnTwo.setBounds(100,0,100,50);
  203.         add(btnTwo);
  204.  
  205.         btnThree = new JButton("Three");
  206.         btnThree.setBounds(0,100,75,100);
  207.         add(btnThree);
  208.  
  209.         btnFour = new JButton("Four");
  210.         btnFour.setBounds(115,115,100,100);
  211.         add(btnFour);
  212.  
  213.     }//constructor
  214.  
  215.  
  216.  
  217.     public static void main(String s[])
  218.     {
  219.         JFrame f = new JFrame("No Layout");
  220.         NoLayout1 panel = new NoLayout1();
  221.  
  222.         f.setForeground(Color.black);
  223.         f.setBackground(Color.lightGray);
  224.         f.getContentPane().add(panel, "Center");
  225.  
  226.         f.setSize(250,250);
  227.         f.setVisible(true);
  228.         f.addWindowListener(new WindowCloser());
  229.  
  230.  
  231.     }//main
  232. }//class
  233.  
  234.  
  235. class WindowCloser extends WindowAdapter
  236. {
  237.     public void windowClosing(WindowEvent e)
  238.     {
  239.         Window win = e.getWindow();
  240.         win.setVisible(false);
  241.         win.dispose();
  242.         System.exit(0);
  243.     }//windowClosing
  244. }//class WindowCloser
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. MouseLocation.java:
  254.  
  255. import javax.swing.*;
  256. import java.awt.event.*;
  257.  
  258. import java.awt.*;
  259.  
  260. public class MouseLocation extends JPanel
  261. {
  262.     public MouseLocation()
  263.     {
  264.        setToolTipText("xxx");
  265.        setBackground(Color.white);
  266.     }//constructor
  267.  
  268.     public String getToolTipText(MouseEvent event)
  269.     {
  270.  
  271.         int x = event.getX();
  272.         int y = event.getY();
  273.         String str = "(" + x + "," + y + ")";
  274.  
  275.         return str;
  276.     }//getToolTipText
  277.  
  278.  
  279.     public static void main(String s[])
  280.     {
  281.         JFrame f = new JFrame("MouseLocation Example");
  282.         MouseLocation panel = new MouseLocation();
  283.  
  284.         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  285.         f.setForeground(Color.black);
  286.         f.setBackground(Color.lightGray);
  287.         f.getContentPane().add(panel,"Center");
  288.         f.setSize(300,300);
  289.         f.setVisible(true);
  290.         f.addWindowListener(new WindowCloser());
  291.     }//main
  292.  
  293. }//class MouseLocation
  294.  
  295. class WindowCloser extends WindowAdapter
  296. {
  297.     public void windowClosing(WindowEvent e)
  298.     {
  299.         Window win = e.getWindow();
  300.         win.setVisible(false);
  301.         win.dispose();
  302.         System.exit(0);
  303.     }//windowClosing
  304. }//class WindowCloser
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313. IconButtons.java:
  314.  
  315. import javax.swing.*;
  316. import java.awt.event.*;
  317. import java.awt.*;
  318.  
  319. public class IconButtons extends JPanel
  320. {
  321.     public IconButtons()
  322.     {
  323.         ImageIcon buttonImage = new ImageIcon("images/T1.gif");
  324.         ImageIcon pressImage = new ImageIcon("images/T2.gif");
  325.         ImageIcon selImage = new ImageIcon("images/T3.gif");
  326.         ImageIcon disabledImage = new ImageIcon("images/T4.gif");
  327.         ImageIcon rolloverImage = new ImageIcon("images/T4.gif");
  328.         ImageIcon selrolloverImage = new ImageIcon("images/T3.gif");
  329.         ImageIcon DOJImage = new ImageIcon("images/T2.gif");
  330.         ImageIcon shapeImage = new ImageIcon("images/T1.gif");
  331.         AbstractButton button1;
  332.         AbstractButton button2;
  333.         AbstractButton button3;
  334.         Font font1;
  335.  
  336.         font1 = new Font("Serif", Font.PLAIN,16);
  337.         setFont(font1);
  338.         setDoubleBuffered(true);
  339.  
  340.         setLayout(new GridLayout(1,3,5,5));
  341.  
  342.         button1 = new JButton();
  343.         button1.setIcon(buttonImage);
  344.         button1.setPressedIcon(pressImage);
  345.         button1.setRolloverIcon(rolloverImage);
  346.         add(button1); 
  347.  
  348.         button2 = new JButton();
  349.         button2.setIcon(buttonImage);
  350.         button2.setDisabledIcon(disabledImage);
  351.         button2.setEnabled(false);
  352.         add(button2);
  353.  
  354.         button3 = new JToggleButton();
  355.         button3.setIcon(DOJImage);
  356.         button3.setSelectedIcon(shapeImage);
  357.         button3.setRolloverIcon(shapeImage);
  358.         button3.setRolloverSelectedIcon(selrolloverImage);
  359.         add(button3);
  360.  
  361.     }//constructor
  362.  
  363.    public static void main(String s[])
  364.     {
  365.         JFrame f = new JFrame("Icon Buttons ");
  366.         IconButtons panel = new IconButtons();
  367.  
  368.         f.setForeground(Color.black);
  369.         f.setBackground(Color.lightGray);
  370.         f.getContentPane().add(panel, "Center");
  371.  
  372.         f.setSize(200,70);
  373.         f.setVisible(true);
  374.         f.addWindowListener(new WindowCloser());
  375.  
  376.  
  377.     }//main
  378. }//IconButtons
  379.  
  380.  
  381. class WindowCloser extends WindowAdapter
  382. {
  383.     public void windowClosing(WindowEvent e)
  384.     {
  385.         Window win = e.getWindow();
  386.         win.setVisible(false);
  387.         win.dispose();
  388.         System.exit(0);
  389.     }//windowClosing
  390. }//class WindowCloser
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399. Action1.java:
  400.  
  401. import java.awt.*;
  402. import java.awt.event.*;
  403. import javax.swing.*;
  404.  
  405. public class Action1 extends JPanel implements ActionListener
  406. {
  407.    public Action1()
  408.    {
  409.         JButton button1;
  410.         JRadioButton radioButton1;
  411.         JRadioButton radioButton2;
  412.         ButtonGroup grp;
  413.  
  414.  
  415.   //Buffer to reduce flicker
  416.         setDoubleBuffered(true);
  417.  
  418.         button1 = new JButton("Button");
  419.         button1.addActionListener(this);
  420.         button1.setActionCommand("Button Activated");
  421.         add(button1); 
  422.  
  423.         grp = new ButtonGroup();
  424.  
  425.         radioButton1 = new JRadioButton("One");
  426.         radioButton1.addActionListener(this);
  427.         radioButton1.setActionCommand("One Activated");
  428.         grp.add(radioButton1);
  429.         add(radioButton1);
  430.  
  431.         radioButton2 = new JRadioButton("Two");
  432.         radioButton2.addActionListener(this);
  433.         radioButton2.setActionCommand("Two Activated");
  434.         grp.add(radioButton2);
  435.         add(radioButton2);
  436.  
  437.    }//constructor
  438.  
  439.    public void actionPerformed(ActionEvent e)
  440.    {
  441.         String cmd;
  442.         Object source;
  443.  
  444.         source = e.getSource();
  445.         cmd = e.getActionCommand();
  446.  
  447.  
  448.         System.out.println("Action: "+cmd+"\n\tperformed by: "+source);
  449.         System.out.println();
  450.  
  451.    }//actionPerformed
  452.  
  453.    public static void main(String s[])
  454.     {
  455.         JFrame f = new JFrame("Action1 ");
  456.         Action1 panel = new Action1();
  457.  
  458. f.getContentPane().add(panel, "Center");
  459.  
  460.         f.setSize(200,100);
  461.         f.setVisible(true);
  462.         f.addWindowListener(new WindowCloser());
  463.  
  464.  
  465.     }//main
  466.  
  467. }//class Actions
  468.  
  469. class WindowCloser extends WindowAdapter
  470. {
  471.     public void windowClosing(WindowEvent e)
  472.     {
  473.         Window win = e.getWindow();
  474.         win.setVisible(false);
  475.         win.dispose();
  476.         System.exit(0);
  477.     }//windowClosing
  478. }//class WindowCloser
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486. List1.java:
  487.  
  488. import java.awt.*;
  489. import java.awt.event.*;
  490. import javax.swing.*;
  491. import javax.swing.event.*;
  492.  
  493. public class List1 extends JPanel
  494.    implements ListSelectionListener
  495. {
  496.     JTextField actField;
  497.     JList list;
  498.  
  499.     public List1()
  500.     {
  501.         String items[] = { "GA", "AL", "DC","NY",
  502.                             "CA","UT","FL"};
  503.  
  504.         JPanel footer;
  505.         JPanel tmp;
  506.  
  507.         setLayout( new BorderLayout());
  508.         setBackground(Color.lightGray);
  509.  
  510.         //Turn on buffering
  511.  
  512.         setDoubleBuffered(true);
  513.         list = new JList(items);
  514.         list.addListSelectionListener(this);
  515.         add(new JScrollPane(list), "Center");
  516.  
  517.         footer = new JPanel();
  518.         footer.setLayout(new GridLayout(3,1,5,5));
  519.  
  520.         tmp = new JPanel();
  521.         tmp.add(new JLabel("Sel values:"));
  522.         actField = new JTextField(20); 
  523.         tmp.add(actField);
  524.  
  525.         footer.add(tmp);
  526.  
  527.         add(footer, "South");
  528.  
  529.     }//constructor
  530.  
  531.     public void valueChanged(ListSelectionEvent e)
  532.     {
  533.         int first, last;
  534.         int i;
  535.         String newVal = "";
  536.         ListModel listData = list.getModel();
  537.         Object selValues[];
  538.  
  539.  
  540.         //Display the selected values
  541.  
  542.         selValues = list.getSelectedValues();
  543.  
  544.         if (selValues != null)
  545.             last = selValues.length;
  546.         else
  547.             last = 0;
  548.  
  549.         newVal = "";
  550.  
  551.         for(i=0;i<last;i++)
  552.         {
  553.             if(i!=0) newVal+=" ";
  554.             newVal += selValues[i].toString();
  555.         }
  556.  
  557.         actField.setText(newVal);
  558.    }
  559.  
  560.    public static void main(String s[])
  561.    {
  562.         JFrame f = new JFrame("List1");
  563.         List1 panel = new List1();
  564.  
  565.         f.setForeground(Color.black);
  566.         f.setBackground(Color.lightGray);
  567.         f.getContentPane().add(panel, "Center");
  568.  
  569.         f.setSize(350,350);
  570.         f.setVisible(true);
  571.         f.addWindowListener(new WindowCloser());
  572.  
  573.  
  574.     }//main
  575.  
  576.  
  577. }//class
  578.  
  579.  
  580. class WindowCloser extends WindowAdapter
  581. {
  582.     public void windowClosing(WindowEvent e)
  583.     {
  584.         Window win = e.getWindow();
  585.         win.setVisible(false);
  586.         win.dispose();
  587.         System.exit(0);
  588.     }//windowClosing
  589. }//class WindowCloser
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600. Menu1.java:
  601.  
  602. import java.awt.*;
  603. import java.awt.event.*;
  604. import javax.swing.*;
  605. import javax.swing.event.*;
  606.  
  607. public class Menu1 extends JPanel
  608.     implements ActionListener, MenuListener
  609. {
  610.     JTextField fldStatus;
  611.  
  612.     //Create the menu class
  613.     public Menu1(JFrame frm)
  614.     {
  615.         JMenuBar bar = new JMenuBar();
  616.         JMenu menu = new JMenu("Strategy");
  617.         JMenuItem tmp;
  618.  
  619.         setBackground(Color.lightGray);
  620.         setLayout(new BorderLayout());
  621.  
  622.         setDoubleBuffered(true);
  623.  
  624.         menu.addMenuListener(this);
  625.  
  626.         tmp = new JMenuItem("Lead");
  627.         tmp.addActionListener(this);
  628.         tmp.setActionCommand("Lead");
  629.         menu.add(tmp);
  630.  
  631.         tmp = new JMenuItem("Follow");
  632.         tmp.addActionListener(this);
  633.         tmp.setActionCommand("Follow");
  634.         menu.add(tmp);
  635.  
  636.         tmp = new JMenuItem("Resign");
  637.         tmp.addActionListener(this);
  638.         tmp.setActionCommand("Resign");
  639.         menu.add(tmp);
  640.  
  641.         tmp = new JMenuItem("Quit");
  642.         tmp.addActionListener(this);
  643.         tmp.setActionCommand("Quit");
  644.         menu.add(tmp);
  645.  
  646.         bar.add(menu);
  647.  
  648.         frm.setJMenuBar(bar); 
  649.  
  650.         fldStatus = new JTextField(10);
  651. add(fldStatus,"South");
  652.  
  653.     }//constructor
  654.  
  655.     public void actionPerformed(ActionEvent e)
  656.     {
  657.         String cmd;
  658.         cmd = e.getActionCommand();
  659.  
  660.         if (cmd.equals("Lead"))
  661.         {
  662.             fldStatus.setText("Action: Lead");
  663.         }
  664.         if (cmd.equals("Follow"))
  665.         {
  666.             fldStatus.setText("Action: Follow");
  667.         }
  668.         if (cmd.equals("Resign"))
  669.         {
  670.             fldStatus.setText("Action: Resign");
  671.         }
  672.         if (cmd.equals("Quit"))
  673.         {
  674.             System.exit(0);
  675.         }
  676.  
  677.     }
  678.  
  679.     public void menuSelected(MenuEvent e)
  680.     {
  681.         fldStatus.setText("Menu Selected");
  682.     }
  683.  
  684.     public void menuDeselected(MenuEvent e)
  685.     {
  686.         fldStatus.setText("Menu Deselected");
  687.     }
  688.  
  689.     public void menuCanceled(MenuEvent e)
  690.     {
  691.         fldStatus.setText("Menu Cancelled");
  692.     }
  693.  
  694.    public static void main(String s[])
  695.    {
  696.         JFrame f = new JFrame("Menu1");
  697.         Menu1 panel = new Menu1(f);
  698.  
  699.         f.setForeground(Color.black);
  700.         f.setBackground(Color.lightGray);
  701.         f.getContentPane().add(panel, "Center");
  702.  
  703.         f.setSize(300,200);
  704.         f.setVisible(true);
  705.         f.addWindowListener(new WindowCloser());
  706.  
  707.  
  708.     }//main
  709.  
  710.  
  711. }//class
  712.  
  713. //close down gracefully
  714. class WindowCloser extends WindowAdapter
  715. {
  716.     public void windowClosing(WindowEvent e)
  717.     {
  718.         Window win = e.getWindow();
  719.         win.setVisible(false);
  720.         win.dispose();
  721.         System.exit(0);
  722.     }//windowClosing
  723. }//class WindowCloser
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.