简体   繁体   中英

Jslider displaying values of array

I' m trying to change the displayed value(selected variable) to what the slider is in line with. The main issue is the worksheet I'm working from specifies this "The displaySelected method is responsible for displaying just the selected item. Look at it,and work out why it is always element 0 that is displayed – it is something quite simple, and not something complex." I understand that the change of display requires the variable selected, I've already tried ( g.drawString(names[selected], 200, 150);) but the issue with this is that it only does value 4 of the array and resets to 0 once moved and doesn't, I know that I have to change the value of selected then, I've tried selected = selector.getValue(); but I'm returned with a null error.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    /**
     * A simple address database for Practical 5B
     * Demonstrates arrays, graphics, selection.
     */
    public class Addresses extends JFrame
                           implements ChangeListener 
{
        /**
     * Frame coordinate constants
     */
    private static final int FRAME_X = 200;
    private static final int FRAME_Y = 200;

    /**
     * Frame size constants
     */
    private static final int FRAME_WIDTH = 500;
    private static final int FRAME_HEIGHT = 400;

    /**
     *  The slider for selecting an address record (selectable 0 to size-1).
     */
    private JSlider selector;

    /**
     * Array to hold the database.
     */
    private String[] names;

    /**
     * To indicate which entry is currently selected.
     */
    private int selected;

    /**
     *  The drawing panel for display of information.
     */
    private JPanel panel;

    /**
     *  Drawing panel size constants
     */
    private final int PANEL_WIDTH = 400;
    private final int PANEL_HEIGHT = 300;

    /**
     *  The main program launcher for the Addresses class.
     *
     * @param  args  The command line arguments (ignored here).
     */



    public static void main( String[] args ) 
    {
        Addresses frame = new Addresses();
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setUpData();       // Initial data set-up
        frame.createGUI();       // Initial GUI set-up
        frame.setVisible( true );
    } 

    /**
     *  Sets up the graphical user interface.
     */




    private void createGUI() 
    { 
        setDefaultCloseOperation( EXIT_ON_CLOSE );  
        Container window = getContentPane();
        window.setLayout( new FlowLayout() );

        // Slider for selecting an address entry
        selector = new JSlider(JSlider.VERTICAL, 0, 0, 0);
                   // Note: the useable range of the slider is 0 - 0 at the moment!
        window.add(selector);
        selector.addChangeListener(this);

        // Graphics panel for displaying the address list
        panel = new JPanel()
        {
            // paintComponent is called automatically when a screen refresh is needed
            public void paintComponent(Graphics g)
            {
                // g is a cleared panel area
                super.paintComponent(g); // Paint the panel's background
                paintScreen(g);          // Then the required graphics
            }
        };
        panel.setPreferredSize( new Dimension( PANEL_WIDTH, PANEL_HEIGHT ) );
        panel.setBackground( Color.white );
        window.add( panel );
    }

    /**
     * Helper method to set up the array data, and the associated variable selected,
     * with their initial configuration.
     */



    private void setUpData()
    {  
        // All the data is "built-in": set it up here - just one entry at the moment
        names = new String[6];         // Create the array with space for one entry
        names[0] = "James";            // The entry
        names[1] = "Connor";
        names[2] = "Alfred";
        names[3] = "Billy";
        names[4] = "Jack";
        names[5] = "Chris";





        selected = names.length-1;                  // Indicate that entry 0 is selected
    }

    /**
     * This methods redraws the screen.
     */



    private void paintScreen(Graphics g) 
    {
        displayList(g);
        displaySelected(g);
    }

    /**
     * Display all the elements of array names in a column on the screen.
     */
    private void displayList(Graphics g) 
    {
        int y = 100;                       // Top y coordinate of the column

        g.setColor(Color.black);
       /* 
        g.drawString(names[0], 20, y);
        g.drawString(names[1], 20, y+25);
        g.drawString(names[2], 20, y+50);
        g.drawString(names[3], 20, y+75);
        g.drawString(names[4], 20, y+100);  
        g.drawString(names[5], 20, y+125);
        */

        for (int i = 0; i< names.length; i++)
            g.drawString(names[i], 20, y+25*i);

    } 

    /**
     * Display the single element of array names that is currently selected by the slider.
     */



    private void displaySelected(Graphics g) 
    {
        g.setColor(Color.black);
        g.drawString("Current selection is:", 200, 135);

        g.drawString(names[selected], 200, 150);



    }

    /**
     *  Reacts to adjustment of the slider.
     *  Notes the new selector setting, then forces screen refresh.
     */
    public void stateChanged( ChangeEvent e ) 
    {
        // selector has been adjusted: record the new setting
        selected = selector.getValue();
        repaint(); // Refresh the screen
    }

}

You never set the maximum value for the slider. That's why you can't move it. Make this change to your code, then you will be able to debug the rest of this program.

selector = new JSlider(JSlider.VERTICAL, 0,   names.length-1 /* HERE */  , 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM