Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于单击JButtons在JSlider上反映值_Java_Swing_Jslider - Fatal编程技术网

Java 基于单击JButtons在JSlider上反映值

Java 基于单击JButtons在JSlider上反映值,java,swing,jslider,Java,Swing,Jslider,我正在Java类中使用GUI。我们必须创建一个汽车类,该类包含刹车和加速方法,以及品牌、型号和颜色的设置和获取方法。然后我们必须创建一个GUI,我拥有一切,除了让滑块反映我单击JButtons时的速度。文本字段将更改,但旋钮不会更改。我知道如何使用旋钮来改变文本字段,但不是相反。任何帮助都将是惊人的 public class CarView extends JFrame { //Declare a new panel to hold contents private JPanel

我正在Java类中使用GUI。我们必须创建一个汽车类,该类包含刹车和加速方法,以及品牌、型号和颜色的设置和获取方法。然后我们必须创建一个GUI,我拥有一切,除了让滑块反映我单击JButtons时的速度。文本字段将更改,但旋钮不会更改。我知道如何使用旋钮来改变文本字段,但不是相反。任何帮助都将是惊人的

public class CarView extends JFrame
{
    //Declare a new panel to hold contents
    private JPanel panel;
    private JColorChooser colorChooser;
    //Declare a new instance variable from Car class
    private Car car;
    //Create a constructor that will use object from Car class with new car parameter
    private Color color = (Color.WHITE);
    public CarView(Car newCar)
    {
        //reference the current object and initialize to newCar
        this.car = newCar;
        //Give the window a title
        setTitle("The Car");
        //Set the size of the window
        setSize(400, 400);
        //Give the window instruction for program to exit when closed
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout layout = new GridLayout (8,1);
        //Create a new panel 
        JPanel contents = new JPanel();
        contents.setLayout(layout);
        
        //Create and add new label to panel
        contents.add(new JLabel("Enter the year of your car: " ));
        //Create a new textfield to hold user input for model year
        JTextField modelYearTextField = new JTextField();
        modelYearTextField.setEditable(false);
        modelYearTextField.setText(String.valueOf(newCar.getModelYear()));
        //Set the size of characters to be held in text field
        modelYearTextField.setColumns(10);
        //Add text field to panel
        contents.add(modelYearTextField);
        
        //Create and add new label to panel
        contents.add(new JLabel("Enter the make of your car: " ));
        //Create a new text field to hold user input for make
        JTextField makeTextField = new JTextField();
        makeTextField.setEditable(false);
        makeTextField.setText(String.valueOf(newCar.getMake()));
        //Set the size of characters to be held in text field
        makeTextField.setColumns(10);
        //Add text field to panel
        contents.add(makeTextField);
        
        //Create and add new label to panel
        contents.add(new JLabel("The speed of your car is: "));
        //Create a new text field  to hold user input for speed
        JTextField speedTextField = new JTextField();
        //Set the size of characters to be held in text field
        speedTextField.setColumns(5);
        //Add text field to panel
        contents.add(speedTextField);
        
        JSlider speedSlider = new JSlider (JSlider.HORIZONTAL, 0, 100, 0);
        speedSlider.setMajorTickSpacing(10);
        speedSlider.setMinorTickSpacing(5);
        speedSlider.setPaintTicks(true);
        speedSlider.setPaintLabels(true);
        JPanel sliderPanel = new JPanel();
        //speedSlider.addChangeListener(new SliderListener());
        contents.add(speedSlider);
        contents.add(sliderPanel);
        speedSlider.addChangeListener(new ChangeListener() 
        {
            public void stateChanged(ChangeEvent e) { 
                
                // when manually changing the slider via mouse, set textfield text accordingly
                speedTextField.setText(speedSlider.getValue() + "");
            }
        });
       
        
        
        
        
        //Create a new button for accelerate
        JButton accelerateButton = new JButton("Accelerate");
        //Add the action listener to the button that will hold the same parameters as listener
        accelerateButton.addActionListener(new AccelerateListener(newCar, speedTextField));
        //Add button to panel
        contents.add(accelerateButton);
        
        //Create a new button for brake
        JButton brakeButton = new JButton("Brake");
        //Add the action listener to the button that will hold the same parameters as listener
        brakeButton.addActionListener(new BrakeListener(newCar, speedTextField));
        //Add button to panel
        contents.add(brakeButton);
        
        
        
        
        JButton colorButton = new JButton("Choose car color");
        
        contents.add(colorButton);
        JTextField colorTextField = new JTextField();
        //Set the size of characters to be held in text field
        colorTextField.setColumns(5);
        contents.add(colorTextField);
        colorButton.addActionListener(new ColorListener(newCar, colorTextField));
        
        //Add panel to window
        add(contents);
        
        //Set window visbility to true
        setVisible(true);
        
        pack();
        
        
        
    }
对于我的ChangeListener,加速和制动(制动看起来相同,但调用了car.brake)以及我的汽车类中的方法,加速速度为8英里/小时,制动速度为5英里/小时

public class AccelerateListener implements ActionListener
{
    //Declare a new instance variable from Car class
    private Car car;
    //Declare the text field to be read
    private JTextField speedTextField;
    
    //Create a constructor
    public AccelerateListener(Car newCar, JTextField newSpeedTextField)
    {
        //reference the current object and initialize to newCar
        this.car= newCar;
        //Initialize the text field to the paramater
        speedTextField = newSpeedTextField;
        
        
        
    }
    public void actionPerformed(ActionEvent e)
    {
        //Call the accelerate method on the reference object
        this.car.accelerate();
        //Get the speed from the text box and convert to a string
        speedTextField.setText(String.valueOf(car.getSpeed()));
        
        
    }
}

我知道我必须为滑块创建一个ActionListener,但当我单击用于“制动”或“加速”的JButtons时,无法获取滑块上的值来反映该值。文本字段会更改,但旋钮不会更改。

正如一小时前的另一个问题()中已经回答的那样,您需要在actionlistener中使用
setValue(…)
更新
JSlider
值。因此,您只需将
speedSlider
传递给
AccerRateListener
,然后调用
speedSlider.setValue(car.getSpeed())
。侧注:对于汽车颜色选择,您可以使用。感谢您的帮助。我之前的问题结束了,所以我认为我的回答不会通过。对于颜色,我确实创建了一个ActionListener,其中包含一个JColorChooser,它可以正常工作。由于某些原因,滑块仍然没有移动,终端窗口中会弹出一个事件错误,但它仍在运行。我会仔细考虑一下,第二天早上再试一次。为了下次得到更好更快的帮助,你应该相应地标记你的问题。(例如java、swing、JSlider)