Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSlider将侦听器值更改为JTextArea_Java_Jslider_Changelistener - Fatal编程技术网

Java JSlider将侦听器值更改为JTextArea

Java JSlider将侦听器值更改为JTextArea,java,jslider,changelistener,Java,Jslider,Changelistener,我需要做什么更改才能生成这段代码,以便JTextArea显示JSlider的最终值?当前,它将打印滑块已到达的所有点。我只想让它在JTextArea中显示滑块的最终位置,而不是它所显示的所有数字 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SliderDemo extends JPanel

我需要做什么更改才能生成这段代码,以便JTextArea显示JSlider的最终值?当前,它将打印滑块已到达的所有点。我只想让它在JTextArea中显示滑块的最终位置,而不是它所显示的所有数字

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;


public class SliderDemo extends JPanel
                    implements 
                               WindowListener,
                               ChangeListener {
//Set up animation parameters.
static final int FPS_MIN = 0;
static final int FPS_MAX = 30;
static final int FPS_INIT = 15;    //initial frames per second


//This label uses ImageIcon to show the doggy pictures.


public SliderDemo() {
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));



    //Create the label.
    JLabel sliderLabel = new JLabel("Frames Per Second", JLabel.CENTER);
    sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

    //Create the slider.
    JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,
                                          FPS_MIN, FPS_MAX, FPS_INIT);


    framesPerSecond.addChangeListener(this);

    //Turn on labels at major tick marks.

    framesPerSecond.setMajorTickSpacing(10);
    framesPerSecond.setMinorTickSpacing(1);
    framesPerSecond.setPaintTicks(true);
    framesPerSecond.setPaintLabels(true);
    framesPerSecond.setBorder(
            BorderFactory.createEmptyBorder(0,0,10,0));
    Font font = new Font("Serif", Font.ITALIC, 15);
    framesPerSecond.setFont(font);

    add(sliderLabel);
    add(framesPerSecond);

    setBorder(BorderFactory.createEmptyBorder(10,10,10,10));}



/** Add a listener for window events. */
void addWindowListener(Window w) {
    w.addWindowListener(this);
}

//React to window events.


public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

/** Listen to the slider. */
public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider)e.getSource();
    if (!source.getValueIsAdjusting()) {
        int fps = (int)source.getValue();


        String rick = Integer.toString(fps);

        JTextArea bob = new JTextArea();
        add(bob);

        bob.setText(rick);




        }
    }


private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("SliderDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SliderDemo animator = new SliderDemo();

    //Add content to the window.
    frame.add(animator, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);

}

public static void main(String[] args) {
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);



    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

@Override
public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub

}

}

每次状态更改时,您都会添加一个新的
JTextArea

JTextArea bob = new JTextArea();
add(bob);
相反,只需在
SliderDemo
构造函数中添加一次
JTextArea
,然后使用:

bob.setText(...)

在您的
ChangeListener

中,每当您的状态发生变化时,您都会添加一个新的
JTextArea

JTextArea bob = new JTextArea();
add(bob);
相反,只需在
SliderDemo
构造函数中添加一次
JTextArea
,然后使用:

bob.setText(...)

在您的
更改侦听器中

我明白了。非常感谢,我明白了。非常感谢你。