使用 java 具有价值的可移动条

发布于 2024-11-08 03:24:31 字数 173 浏览 0 评论 0原文

我想创建一个有价值的酒吧。如果我移动圆圈,那么该字段中的值应该改变。我该怎么做? 这是屏幕截图。我怎样才能做到这一点?

类似这样的东西也可以。请帮我解决这个问题。 预先感谢您:)

在此处输入图像描述

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 image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热情消退 2024-11-15 03:24:31

SpinSlider可能是一个有用的例子。

附录:这是一个为 JSpinner.NumberEditor 使用合适格式字符串的示例。

附录:您可以通过重写paintTrack()来更改滑块的颜色,如下所示此处

SpinSlider 图片

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);
    }
}

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.

SpinSlider image

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);
    }
}
千里故人稀 2024-11-15 03:24:31

要在 Java 中创建带有值(滑块)的栏,您可以使用 JSlider 类。当您创建此类的实例时,您可以指定所有参数,例如 min。和最大。值、步长等。

您可以向滑块添加一个 ChangeListener。此 Changelistener 应实现方法 stateChanged(),在该方法中,您可以根据滑块的位置更改文本框中显示的值。

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));
}

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));
}
枕头说它不想醒 2024-11-15 03:24:31

在 Java Swing 中,您可以使用 JSlider (已经具有所需的模型和行为),然后自定义其 UI

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文