如何根据分配的JTextField移动多个JSlider

时间:2016-03-16 11:25:47

标签: java jslider

我需要根据JSwing中相应的文本字段移动滑块。我必须相应地移动第一个JSlider,但是当在第二个JSlider中输入值时,调用First来移动第二个JSlider。只需要在为值

输入指定的textField时移动相应的滑块

以下是您的代码:

package sliderexample;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel; 
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class BlueSliderBeanFrame extends JFrame implements DocumentListener       {

/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * 
 */
private JPanel contentPane;
private JTextField textField;


JSlider slider = new JSlider();
private JTextField textField_1;
JSlider slider_2 = new JSlider();


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                BlueSliderBeanFrame frame = new BlueSliderBeanFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public BlueSliderBeanFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textField = new JTextField();
    textField.getDocument().putProperty("owner", textField);
    textField.getDocument().addDocumentListener(this);
    textField.setBounds(53, 37, 125, 50);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.getDocument().putProperty("owner", textField_1);
    textField_1.getDocument().addDocumentListener(this);
    textField_1.setColumns(10);
    textField_1.setBounds(53, 126, 125, 50);
    contentPane.add(textField_1);


    slider.setValue(0);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent arg0) {
                textField.setText(String.valueOf(slider.getValue()));
                }
        });
    slider.setMaximum(999);
    slider.setBounds(188, 37, 200, 50);
    contentPane.add(slider);

    slider_2.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        textField.setText(String.valueOf(slider_2.getValue()));
        }
    });
    slider_2.setValue(0);
    slider_2.setMaximum(999);
    slider_2.setBounds(188, 126, 200, 50);
    contentPane.add(slider_2);




}



public void updateSlider( int i ) {

    Runnable doHighlight = new Runnable() {

    @Override
    public void run() {
     // your highlight code
     int val = 0;
     System.out.println(" i val : "+i);
     if( i == 1 ){
            val = Integer.parseInt(textField.getText());
            System.out.println("owner1 slider");
            slider.setValue(val);
     } else if ( i == 2 ){
            val = Integer.parseInt(textField_1.getText());
            System.out.println("owner2 slider");
            slider_2.setValue(val);
     }
 }};   

    SwingUtilities.invokeLater(doHighlight);

}

@Override
public void changedUpdate(DocumentEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void insertUpdate(DocumentEvent arg0) {
    // TODO Auto-generated method stub
    /*Object owner = arg0.getDocument().getProperty("owner");
    if( owner == textField ){
            System.out.println("owner1");
            updateSlider(1);
    } else if ( owner == textField_1 ){
        System.out.println("owner2");
            updateSlider(2);
    }*/
       if (arg0.getDocument()== textField.getDocument()){
            System.out.println("event caused by field");
            updateSlider(1);
        }
        else if (arg0.getDocument()== textField_1.getDocument()){
            System.out.println("event caused by field2");
            updateSlider(2);
        }
}

@Override
public void removeUpdate(DocumentEvent arg0) {
    // TODO Auto-generated method stub

}
}

输出:

enter image description here

请有人帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

只需稍微更新您的代码,

更新代码:

 slider_2.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
         /* 
         here you did wrong...
         textField.setText(String.valueOf(slider_2.getValue()));
         */
        textField_1.setText(String.valueOf(slider_2.getValue()));
        }
    });

见输出:

enter image description here