我不能修改我的布尔字段?

I can't modify my boolean fields?

好吧,问题已经说明了,无论我做什么,我都无法修改我的布尔字段。以下代码是我正在执行的作业中的 class 但我需要能够修改布尔值才能执行此操作,但出于某种原因我不能这样做,所以我不确定发生了什么:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Rectangle2D;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    @SuppressWarnings("serial")

    public class Test extends JPanel implements ActionListener {

    private String senderName,reciverName,message;
    private int w=250,velx=13,x=330,senderW=350,senderH=100,reciverW=350,reciverH=270,messageW=350,messageH=50;
    private Timer tm = new Timer(50, this);
    private boolean something=true;

    public void setSomething(boolean s){            
        something=s;
    }

    public Test(String s1,String s2, String s3){
             String cutString1 = s1.substring(0, Math.min(15, s1.length()));
             String cutString2 = s2.substring(0, Math.min(15, s2.length()));
             String cutString3 = s3.substring(0, Math.min(30, s3.length()));

             senderName=cutString1;
             reciverName=cutString2;
             message=cutString3;                 
             setSomething(false);
             //Even though I set it to false it still holds true and won't print it out
           if(something=false){                
               System.out.print("Something");                  
           }                 
           setLayout(null);                
             timer();
    }

    public void timer(){            
         tm.setInitialDelay(10000);
         tm.start();        
    }

    public void paintComponent(Graphics g){
         setOpaque(true);            
         super.paintComponent(g);
         Font font1 = new Font(  "TimesRoman", Font.BOLD, 17);
         Font font2 = new Font(  "TimesRoman", Font.BOLD, 30);           
         g.setColor(Color.CYAN);
         g.fillRect(330, 30, 250, 390);
         g.setFont(font1);
         g.setColor(Color.BLUE);
         g.drawString(message, messageW, messageH);
         g.setColor(Color.RED);
         g.fillRect(x, 30, w, 390);
         g.setColor(Color.BLUE);
         g.setFont(font2);
         g.drawString(senderName, senderW, senderH);
         g.drawString("To",430, 200);
         g.drawString(reciverName, reciverW, reciverH);
        }

    public void anime(){            
        w=w-velx;
        repaint();
    }

    public void actionPerformed(ActionEvent e){                
                anime();
    }    

    public static void main(String[] args){
        JFrame frame2 = new JFrame();
        frame2.add(new Test("something","something","something"));
        frame2.setTitle("Title");  
        frame2.setSize(700,500);
        frame2.setResizable(true);
        frame2.setLocationRelativeTo(null);
        frame2.setVisible(true);            
    }
}

您正在使用赋值来检查变量 something,该变量将始终为 false,因此您的 print 语句永远不会到达:

if (something = false) {

使用布尔检查的简短形式可避免此类错误:

if (!something) {

An if statement consists of a Boolean expression followed by one or more statements.

改变这个

if(something=false){

           System.out.print("Something");

       }

if(!something){ // for false check  }

if(something) //  for true check

示例 为什么 if(boolean=boolean) 不是编译时错误

public static void main(String[] args) {
    boolean test = true;
    int a = 0;
    boolean test1 = false;
    test1 = (test = true); // no error boolean expression
    test = (a = 1); // compile error not a boolean expression
    if(test = false)
    {

    }
    System.out.println("" + test);
}