[英]JSlider doesnt change the value and keeps constant
I have just made a java program with eclipse to change a Jprogress bar with a jslider but the sliders value doesn't change It keeps constant, here is the code: 我刚刚制作了一个带eclipse的Java程序,用jslider更改了Jprogress栏,但是sliders的值没有改变。它保持不变,这是代码:
First class: 头等舱:
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();
}
}
Second Class: 二等舱:
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;
}
I have also tried to assign the value of a slider to a value and print it out but it keeped at the default value I set it to which was 0...... 我还尝试将滑块的值分配给一个值并打印出来,但它保持在我将其设置为0的默认值上……
anything wrong thing in the code?? 代码中有什么错误的东西吗?
In your main method, you are creating an instance of pro1
. 在您的main方法中,您正在创建
pro1
的实例。 In pro2
you are creating a new instance of pro1
. 在
pro2
您将创建pro1
的新实例。 Each of these instances has its own value
. 这些实例中的每一个都有其自己的
value
。
Additionally, when you change the value
, then the progress bar will not notice this. 另外,当您更改
value
,进度条将不会注意到这一点。 You have to call JProgressBar#setValue
explicitly. 您必须显式调用
JProgressBar#setValue
。
There have been some additional issues with the code, here is a slightly modified version that shows ONE possible way of doing it... 该代码还有一些其他问题,这里是一个稍作修改的版本,显示了一种可能的实现方式...
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.