JSlider不会更改值并保持不变

时间:2014-02-16 02:18:52

标签: java jprogressbar jslider

我刚刚用eclipse制作了一个java程序 使用jslider更改Jprogress栏但滑块值不会更改它保持不变,这是代码:

头等舱:

package pro;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class pro1 {

int value;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //setting the objects
    final pro1 p = new pro1();
    final pro2 p2 = new pro2();


    //adding the JFrame.
    JFrame fr = new JFrame();
    fr.setVisible(true);
    fr.setSize(380, 80);
    fr.setLayout(new FlowLayout(FlowLayout.LEFT));
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //adding the JProgressBar.
    JProgressBar pb = new JProgressBar(0,100);
    pb.setOpaque(false);
    Color c = new Color (0,200,0);
    pb.setForeground(c);        
    pb.setValue(p.value);


    //adding the JPanel
    JPanel panel = new JPanel();
    panel.add(pb);
    panel.add(p2.slider());

    //adding the panel to the frame.
    fr.add(panel);
    fr.revalidate();

}
}

第二课程:

package pro;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class pro2 {

//setting the object
        pro1 p = new pro1();

public JSlider slider(){


//adding the slider.
        final JSlider s = new JSlider ();
        s.setMinimum(0);
        s.setMaximum(100);
        s.setValue(0);
        s.setMajorTickSpacing(10);
        s.setPaintTicks(true);
        s.addChangeListener(new ChangeListener(){

            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                p.value = s.getValue();
            }

        });

        return s;
}

我还尝试将滑块的值分配给一个值并将其打印出来,但它保持默认值,我将其设置为0 ......

代码中有什么错误吗?

1 个答案:

答案 0 :(得分:0)

在main方法中,您正在创建pro1的实例。在pro2中,您正在创建pro1的新实例。每个实例都有自己的value

此外,当您更改value时,进度条将不会注意到这一点。您必须明确致电JProgressBar#setValue

代码还存在一些其他问题,这是一个稍微修改过的版本,显示了一种可行的方法......

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderProgressTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        //adding the JFrame.
        JFrame fr = new JFrame();
        fr.setSize(380, 80);
        fr.setLayout(new FlowLayout(FlowLayout.LEFT));
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //adding the JProgressBar.
        JProgressBar pb = new JProgressBar(0,100);
        pb.setOpaque(false);
        Color c = new Color (0,200,0);
        pb.setForeground(c);        

        //adding the JPanel
        JPanel panel = new JPanel();
        panel.add(pb);
        panel.add(createSlider(pb));

        //adding the panel to the frame.
        fr.add(panel);
        fr.setVisible(true);
    }

    private static JSlider createSlider(final JProgressBar pb)
    {
        //adding the slider.
        final JSlider s = new JSlider ();
        s.setMinimum(0);
        s.setMaximum(100);
        s.setValue(0);
        s.setMajorTickSpacing(10);
        s.setPaintTicks(true);
        s.addChangeListener(new ChangeListener(){

            @Override
            public void stateChanged(ChangeEvent e) {
                pb.setValue(s.getValue());
            }

        });

        return s;
    }
}