Continuously increase integer value as the button is pressed

后端 未结 9 1291
长情又很酷
长情又很酷 2020-12-04 11:10

I\'m new to Android so sorry if the question is easy to answer. I have two buttons, a decrease and an increase button, and in the middle of them a TextView which displays a

9条回答
  •  不知归路
    2020-12-04 11:28

    import java.awt.; import java.awt.event.; public class LabelNumber extends Frame implements ActionListener {

        Button badd,bsub;
        TextField t1;
        
    void display()
    {
        setTitle("Label number change");
        setSize(400,500);
        setLayout(new FlowLayout());
        setVisible(true);
    
        badd=new Button("+");
        t1=new TextField("0",6);
    
        bsub= new Button("-");
        add(bsub);add(t1);add(badd);
      
    
    badd.addActionListener(this);
    bsub.addActionListener(this);
    
    addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }
    );
    

    }

    public void actionPerformed(ActionEvent e)
        {
        int count=0,add=0,sub,n1,n2;
            count=Integer.parseInt(t1.getText());
                
        if(e.getSource()==badd)
        {
            if(count<10)
            {
                count=count+1;
                t1.setText(""+count);
            }
            else
            {
                t1.setText("limit:10");
            }               
        }
        
        if(e.getSource()==bsub)
        {
            if(count<10)
            {
                count=count-1;
                t1.setText(""+count);
            }
            else
            {
                t1.setText("limit:10");
            }               
        }
    }
    
    
    public static void main(String args[])
    {
        LabelNumber obj =new  LabelNumber();
        obj.display();
    
    }
    

    }

提交回复
热议问题