java—基于单击jbuttons在jslider上反映值

zysjyyx4  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(189)

我正在java类中处理gui。我们必须创建一个car类,该类包含刹车和加速方法,以及make、model和color的setter和getter。然后我们必须创建一个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)和我的car类中的方法,加速速度是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进行brake或accelerate时,我无法得到滑块上的值来反映值。文本字段会更改,但旋钮不会更改。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题