简体   繁体   English

Jslider显示数组的值

[英]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." 主要问题是我正在使用的工作表中指定了以下内容:“ displaySelected方法仅负责显示选定的项目。查看它,弄清楚为什么总是显示元素0 –这很简单,并且不复杂。” 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(); 我知道更改显示要求选择变量,我已经尝试过了(g.drawString(names [selected],200,150);),但是这样做的问题是它只对数组的值4进行复位一次移动到并且不移动到0,我知道我必须更改selected的值,然后,我尝试了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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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