Java:为滑块添加“接受”按钮

时间:2012-12-18 18:53:20

标签: java swing jbutton jslider

我正在尝试为我的滑块添加一个按钮。我想要的是当我单击按钮时,滑块的值将保存在变量中。但是在使用ActionListener时我总是得到错误。任何人都可以帮忙。这是我的滑块的代码:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.Hashtable;

public class SliderSample {

    public static void main(final String args[]) {
        Runnable runner = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Sample Sliders");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ChangeListener listener = new SliderChangeListener();

                JSlider js4 = new JSlider(1,5);
                Hashtable<Integer, JComponent> table = new Hashtable<Integer, JComponent>();
                table.put(1, new JLabel("1"));
                table.put(2, new JLabel("2"));
                table.put(3, new JLabel("3"));
                table.put(4, new JLabel("4"));
                table.put(5, new JLabel("5"));
                js4.setLabelTable(table);
                js4.setPaintLabels(true);
                js4.addChangeListener(listener);
                frame.add(js4, BorderLayout.CENTER);
                frame.setSize(400, 300);
                frame.setVisible(true);
            }
        };
        EventQueue.invokeLater(runner);
    }

    public static class SliderChangeListener implements ChangeListener {
        public void stateChanged(ChangeEvent changeEvent) {
            Object source = changeEvent.getSource();
            JSlider theJSlider = (JSlider)source;
            if (!theJSlider.getValueIsAdjusting()) {System.out.println ("Slider changed: " +         theJSlider.getValue());
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

“this”目前无法使用,因为您正在处理您所在的Runnable。请更改为:

public class SliderSample {
    private JSlider js4 = new JSlider(1, 5);
    private int state = js4.getValue();

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

    public SliderSample() {
        JFrame frame = new JFrame("Sample Sliders");
        ...

        JButton b = new JButton("Save");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                state = js4.getValue();
                System.out.println("new state: " + state);
            }
        });

        Hashtable<Integer, JComponent> table = new Hashtable<Integer, JComponent>();
        ...
        frame.setVisible(true);
}

答案 1 :(得分:2)

我修改了你的代码。添加了一个按钮,可以将滑块的值保存到变量中。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.Hashtable;

public class SliderSample {
    private static JFrame frame = new JFrame("Sample Sliders");
    private static JSlider slider = new JSlider(1,5);
    private static JButton button =new JButton("Button");
    private static int saveNumberHere;


    public static void main(final String args[]) {
        Runnable runner = new Runnable() {
            public void run() {

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                Hashtable<Integer, JComponent> table = new Hashtable<Integer, JComponent>();
                table.put(1, new JLabel("1"));
                table.put(2, new JLabel("2"));
                table.put(3, new JLabel("3"));
                table.put(4, new JLabel("4"));
                table.put(5, new JLabel("5"));
                slider.setLabelTable(table);
                slider.setPaintLabels(true);

                ChangeListener sliderListener = new SliderChangeListener();
                slider.addChangeListener(sliderListener);
                ChangeListener buttonListener = new ButthonChangeListener();
                button.addChangeListener(buttonListener);

                frame.add(slider, BorderLayout.CENTER);
                frame.add(button, BorderLayout.SOUTH);
                frame.setSize(400, 300);
                frame.setVisible(true);
            }
        };
        EventQueue.invokeLater(runner);
    }
    public static class SliderChangeListener implements ChangeListener {
        public void stateChanged(ChangeEvent changeEvent) {
            if (!slider.getValueIsAdjusting()) {
                System.out.println ("Slider changed: " +         slider.getValue());
            }            
        }
    }

    public static class ButthonChangeListener implements ChangeListener{
        public void stateChanged(ChangeEvent changeEvent) {
            saveNumberHere = slider.getValue();
        }
    }
}