我正在尝试让slide.get\u value()在测试中得到数字5时打印“5”

kwvwclae  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(353)
package com.company.Iguana;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.InputEvent;

public class MyCPS implements ChangeListener {
    JSlider Slider;
    JPanel panel;
    JLabel label;
    JFrame frame;
    JComboBox Combo;

    MyCPS() {
        frame = new JFrame("CPS");
        panel = new JPanel();
        label = new JLabel();
        Slider = new JSlider();
        String[] Buttons = {"Left Click", "Right Click"};
        JComboBox combo = new JComboBox(Buttons);
        ImageIcon image = new ImageIcon("BOAROR.png");
        frame.setIconImage(image.getImage());
        frame.getContentPane().setBackground(Color.YELLOW);
        frame.setTitle("Iguana.exe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 420);
        Slider = new JSlider(0, 23, 12);
        Slider.setPreferredSize(new Dimension(400, 200));
        Slider.setPaintTrack(true);
        Slider.setMajorTickSpacing(4);
        Slider.setPaintLabels(true);
        Slider.setOrientation(SwingConstants.VERTICAL);
        label.setText("CPS" + Slider.getValue());
        label.setIcon(image);
        Slider.addChangeListener(this);
        panel.add(combo);
        panel.add(Slider);
        panel.add(label);
        frame.add(panel);
        frame.setSize(420,420);
        frame.setVisible(true);
        frame.pack();

    }

    @Override
    public void stateChanged(ChangeEvent e) {
        label.setText("CPS" + Slider.getValue());
        if(e.equals(Slider.getValue()));              <<<<<<<< Problem
        System.out.println("Five");

}

}
每当我尝试放置if(e.equals(slider.getvalue());==5表示运算符“==”不能应用于“java.lang.object”、“int”
我只是想得到它每当我把cps作为5它会打印“5”回来作为一个测试我是相当新的,所以我可能做了一些愚蠢的错误

ojsjcaue

ojsjcaue1#

错误是因为您试图比较对象, e 类型, ChangeEventSlider.getValue() 返回一个 int .
你应该把它写成

if(Slider.getValue() == 5) {            
    System.out.println("Five");
}

检查 JSlider#getValue 了解更多。

相关问题