本文介绍了JSlider旋钮不更新(基本Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中实现了一个简单的水平JSlider。没有刻度线或类似的东西。可能就像人类可以想象的一样简单。



当我试图通过用鼠标拖动JSlider左右来移动时,其数值

em> update(我设置了一个 system.out print语句来报告滑块的变化值,它们看起来很好)。



但是,滑块旋钮本身不会移动。它只是在初始化期间保持卡住状态。是否存在某种类似于每个JSlider都应该拥有的样板代码?



我一直在玩ChangeListener的东西,但我一直在打死路。

解决方案

似乎对我有用,请发布

I'm implementing a simple horizontal JSlider in Java. No tick marks or anything like that. Probably as simple as humanly imaginable.

When I try to "move" the JSlider left-right by dragging it with the mouse, its numerical value does update (I set up a system.out print statement to report the slider's changing values, and they look fine).

However, the slider knob itself does not move. It just stays stuck where it started during initialization. Is there some sort of boilerplate code that "every JSlider should have" regarding this aspect?

I've been playing around with the "ChangeListener" stuff, but I keep hitting dead ends.

解决方案

Seems to work fine for me, please post SSCCE to show specific problems:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SwingSliderExample extends JPanel {

    public SwingSliderExample() {

        super(true);
        this.setLayout(new BorderLayout());
        final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);

        slider.setMinorTickSpacing(2);
        slider.setMajorTickSpacing(10);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

        // We'll just use the standard numeric labels for now...
        slider.setLabelTable(slider.createStandardLabels(10));

        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent ce) {
                if (!slider.getValueIsAdjusting()) {//no adjustments are taking place thus we know the value is final
                    System.out.println(slider.getValue());
                }
            }
        });

        add(slider, BorderLayout.CENTER);
    }

    public static void main(String s[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    // If Nimbus is not available, you can set the GUI to another look and feel.
                }
                JFrame frame = new JFrame("Slider Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new SwingSliderExample());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Reference:

这篇关于JSlider旋钮不更新(基本Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:44