0

HI,need help continuing this code, im stuck and i want to make it work
Thanx

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class WinListBs extends Frame {

    Button      btnExec,  btnExit ; 
    Label       lblNum ;
    TextField   fldNum ;    
    List        lstVals1, lstVals2, lstCommands;    

    Node head = null;
    Node last = null;

    WinListBs () {      
        super ("List Boxes Handling");      

        // define Components
        lstVals1 =  new List(20,false);
        lstVals2 =  new List(20,false);
        lstCommands =   new List(20,false);
        btnExit =   new Button("Exit");

        lblNum =    new Label("îñôø");
        fldNum =    new TextField("7");

        // nice Components
        Font f1 = new Font("David", Font.BOLD, 20);
        Font f2 = new Font("COURIER", Font.ITALIC, 20);     
        Font f3 = new Font("DAVID", Font.BOLD, 16);
        Font f4 = new Font("COURIER", Font.BOLD, 16);

        lblNum.setBackground( Color.YELLOW );
        lblNum.setFont( f2 );

        fldNum.setBackground( Color.CYAN );
        fldNum.setFont( f1 );

        btnExit.setFont( f4 );      
        btnExit.setForeground( Color.MAGENTA );     

        lstVals1.setFont(f4);
        lstVals2.setFont(f3);
        lstCommands.setFont(f4);
        lstCommands.setBackground(Color.GRAY);

        // listeners
        CActListener myHandler = new CActListener();
        btnExit.addActionListener( myHandler ); 
        lstCommands.addItemListener(new ClstBoxListener());

        // build main window (a panel)
        setLayout(new GridLayout(1,4));
        Panel pControls = new Panel ();
        pControls.setLayout(new GridLayout(7,1));
        pControls.add( lblNum  );       
        pControls.add( fldNum );
        pControls.add( btnExit );   

        add( pControls );
        add( lstCommands );

        add( lstVals1 );
        add( lstVals2 );
        lstVals1.add("<empty>");
        lstVals2.add("<empty>");
        setupCmds();
        // pack and show
        pack();     
        setLocation(400,100);
        setSize(500,400);
        setVisible(true); // show
    } // CTOR   

    private  void setupCmds () {
        lstCommands.add("Random"); // generate 5 random numbers
        lstCommands.add("Reset"); // list => empty
        lstCommands.add("Push as First");
        lstCommands.add("Push as Last");
        lstCommands.add("Get First");
        lstCommands.add("Get Last");
        lstCommands.add("Del First");
        lstCommands.add("Del Last");
        lstCommands.add("Add sort");   // add fldNum in the right (sorted) place
        lstCommands.add("Del num");    // delete (from list) the fldNum (all occurances)
        lstCommands.add("Del bigger"); // delete bigger than fldNum
        lstCommands.add("List2 even"); // show even numbers in lstVals2
        lstCommands.add("List2 bigger"); // show even numbers in lstVals2
    } // setupCmds

    private void showVals1 (){
        lstVals1.removeAll();
        Node p = head ;
        int k = 0;
        while (p != null){
            lstVals1.add(k + " => " + p.x);
            k++;
            p = p.next;
        } // while
    } // ShowVals1

    private void doRandom (int n) {
        for (int k = 0 ; k < n ; k++){
            int z = (int)(Math.random()*100);
            pushNum(z);
        } // for
    } // doRandom

    private class ClstBoxListener implements ItemListener {

        public void itemStateChanged(ItemEvent e){
            List lsm = (List)e.getSource();

            String s = lsm.getSelectedItem();

            if (s.equals("Random")) {
                doRandom(5);
                showVals1();
                return;
            } // Random

            if (s.equals("Push as First")) {
                String sf = fldNum.getText();
                int z = Integer.parseInt(sf);
                pushNum(z);
                showVals1();
                return ;
            } // Push as First
        } //itemStateChanged
    } // ClstBoxListener

    private void pushNum (int z) {
        Node tmp = new Node(z,head);
        head = tmp;
        if (last == null) last = tmp;
    } // pushNum

    private class CActListener implements ActionListener {

        public void actionPerformed (ActionEvent evt) {

            if (evt.getSource()== btnExit) System.exit(0);  

        } // actionPerformed
    } //CActListener

    private class Node {
        private int x ;
        private Node next;
        Node (int dx){
            x= dx ; next = null;
        }
        Node (int dx , Node dNext){
            x= dx ; next = dNext;
        }
        void setNext (Node dNext){
            next= dNext;
        }
        Node getNext () {
            return next;
        }
    } // private class LinkNum

    public static void main(String[] args) {    
        WinListBs mainWin = new WinListBs(); 
    } // main   
} // class

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by coil
0

What's the problem? Also, please use [code] tags.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.