java—是否可以使用jslider控制多个jtextfield?

f8rj6qna  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(391)

我尝试使用jslider在三个jtextfields中设置文本。我的条件是滑块应该适用于textfield_1,只有当textfield_1获得焦点时,其他两个textfield也是如此。当我尝试对其他textfield使用相同的滑块时,只有第一个文本字段的值发生了更改。
期待有价值的建议,谢谢。

JSlider slider;
JTextField tf;

tf.addFocusListener(new FoucusListener(){
 public void foucusGained(FocusEvent fe){   
   slider.addChangeListener(new ChangeListener()){
 public void stateChanged(ChangeEvent ce){
   JSlider slider =(JSlider)ce.getSource();
    if(slider.getValueisAdjusting())
        tf.setText(String.valueOf(slider.getValue()))
    }
});
});
omhiaaxx

omhiaaxx1#

基本思想是您需要知道上次选择的字段。问题是,当你选择滑块时,它会触发一个焦点获得事件。。。
最简单的方法就是使用 FocusListener 只注册到文本字段,并保留对最后选定字段的引用。
当滑块更改时,您只需与最后选定的字段(如果不是)交互 null )

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderControl {

    public static void main(String[] args) {
        new SliderControl();
    }

    public SliderControl() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JSlider slider;
        private JTextField[] fields;

        private JTextField selectedField;

        public TestPane() {
            setLayout(new GridBagLayout());
            fields = new JTextField[3];
            FocusHandler focusHandler = new FocusHandler();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            for (int index = 0; index < 3; index++) {
                fields[index] = new JTextField(3);
                fields[index].addFocusListener(focusHandler);
                add(fields[index], gbc);
                gbc.gridy++;
            }
            gbc.fill = GridBagConstraints.HORIZONTAL;
            slider = new JSlider();
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (selectedField != null) {
                        selectedField.setText(String.valueOf(slider.getValue()));
                    }
                }
            });
            add(slider, gbc);
        }

        protected class FocusHandler extends FocusAdapter {

            @Override
            public void focusGained(FocusEvent e) {
                if (e.getComponent() instanceof JTextField) {
                    selectedField = (JTextField) e.getComponent();
                }
            }

        }
    }
}

相关问题