Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Sierra

Thread: Help with a number to increase by 1 each time something happens.

  1. #1
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Help with a number to increase by 1 each time something happens.

    So i am doing an algorithm, and what i can't understand is how to get the iterations to increase by 1 each time i press the next button. The code for my panel is:
    //Name______________________________ Date_____________
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Panel03 extends JPanel
    {
    private JLabel label1, label2;
    private JTextField box;
    private int number, count;
    public Panel03()
    {
    setLayout(new FlowLayout());
    number = 37;
    count = 0;

    label1 = new JLabel("37");
    label1.setFont(new Font("Serif", Font.BOLD, 100));
    label1.setForeground(Color.blue);
    add(label1);

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    add(panel);

    box = new JTextField("37", 5);
    box.setHorizontalAlignment(SwingConstants.RIGHT);
    panel.add(box);

    JButton button1 = new JButton("Set");
    button1.addActionListener(new Listener1());
    panel.add(button1);

    JButton button2 = new JButton("Next");
    button2.addActionListener(new Listener2());
    panel.add(button2);

    JButton button3 = new JButton("Quit");
    button3.addActionListener(new Listener3());
    panel.add(button3);

    label2 = new JLabel("Iterations: 0");
    add(label2);
    }
    private class Listener1 implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    int x=Integer.parseInt(box.getText());
    label1.setText(box.getText());

    }
    }
    private class Listener2 implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    int x=Integer.parseInt(box.getText());

    if(x%2==1)
    {
    x=(x*3+1);
    }
    else if(x%2==0)
    {
    x=(x/2);
    }
    label1.setText(""+x);
    box.setText(label1.getText());


    }
    }
    private class Listener3 implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    int r=0;
    Runtime.getRuntime().exit(r);

    }
    }
    }


    What should i add? thanks.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help with a number to increase by 1 each time something happens.

    First - how about putting your code into the forum code brackets...
    Second: implementing a variable that tracks your increments would be nice.
    Third, the actionListener gets called when you push the button, so that would be a good place to increment your variable and then update the label2 right?
    Fubarable likes this.
    I like likes!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Help with a number to increase by 1 each time something happens.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 26
    Last Post: 12-03-2013, 02:55 PM
  2. Replies: 0
    Last Post: 11-09-2012, 01:53 PM
  3. Replies: 5
    Last Post: 02-22-2012, 02:49 PM
  4. Methods to increase run-time efficiency
    By rajkobie in forum New To Java
    Replies: 0
    Last Post: 04-23-2011, 03:02 PM
  5. why the number will not increase?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 03-16-2009, 05:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •