Results 1 to 3 of 3
- 12-17-2012, 09:31 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 28
- Rep Power
- 0
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.
- 12-18-2012, 12:01 AM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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?I like likes!.gif)
- 12-18-2012, 04:28 AM #3
Re: Help with a number to increase by 1 each time something happens.
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
how to connect the server using same port number more than one time?
By tamilarasi in forum Java ServletReplies: 0Last Post: 11-09-2012, 01:53 PM -
Increase Oval size by 2 every time the program is run. For the coordinates 50,50
By Nakinsige in forum New To JavaReplies: 5Last Post: 02-22-2012, 02:49 PM -
Methods to increase run-time efficiency
By rajkobie in forum New To JavaReplies: 0Last Post: 04-23-2011, 03:02 PM -
inputs numbers then outputs how many time a particular number appears
By koji_kun in forum New To JavaReplies: 23Last Post: 12-22-2009, 08:33 PM -
why the number will not increase?
By rayda in forum New To JavaReplies: 3Last Post: 03-16-2009, 05:47 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks