Movable bars with value using java

风流意气都作罢 提交于 2019-11-26 10:02:43

问题


I want to create a bar with a value. If i move the circle, then the value in the field should change. How can i do this? Here is the screen shot. How can i achieve this?

Something similar to this is also ok. Please help me with this. Thank you in advance :)

\"enter


回答1:


SpinSlider may be a useful example.

Addendum: Here's an example using a suitable format string for JSpinner.NumberEditor.

Addendum: You can change the color of the slider by overriding paintTrack(), as shown here.

import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/** @see https://stackoverflow.com/questions/6067898 */
public class SpinSlider extends JPanel {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("SpinSlider!");
                f.add(new SpinSlider());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    public SpinSlider() {
        this.setLayout(new FlowLayout());
        final JSpinner spinner = new JSpinner();
        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider s = (JSlider) e.getSource();
                spinner.setValue(s.getValue());
            }
        });
        this.add(slider);
        spinner.setModel(new SpinnerNumberModel(50, 0, 100, 1));
        spinner.setEditor(new JSpinner.NumberEditor(spinner, "0'%'"));
        spinner.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner s = (JSpinner) e.getSource();
                slider.setValue((Integer) s.getValue());
            }
        });
        this.add(spinner);
    }
}



回答2:


To create a bar with a value (slider) in Java, you can use the JSlider class. When you create an instance of this class you can specify all the parameters like min. and max. value, step size, etc.

You can add a ChangeListener to the slider. This Changelistener should implement the method stateChanged() and in this method you can change the value displayed in the text box based on the position of the slider.

JSlider slider= new JSlider(JSlider.HORIZONTAL,0,100,50); //min value 0, max value 100, initial value 50
slider.addChangeListener(this)
JTextFox text = new JTextFox("50");
//Some other code, adding the the slider, text box (and other stuff) to the application
//...

public void stateChanged(ChangeEvent e)
{
   JSlider source = (JSlider)e.getSource();
   int value = (int)source.getValue();
   text.setText(Integer.toString(value));
}



回答3:


In Java Swing you could use a JSlider (which already has the desired model and behavior) and then customize its UI



来源:https://stackoverflow.com/questions/6067898/movable-bars-with-value-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!