Java Push Counter
To program java, you will need a Java program such as "Ready to Program Java IDE" or "JDK", I use a different, custom made one for the server i use, but any will do.
CODE
//PushCounter.java
import javax.swing.*;
public class PushCounter
{
public static void main (String [] args)
{
JFrame frame = new JFrame ("Push Counter");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new PushCounterPanel());
frame.pack();
frame.setVisible(true);
}
}
In case you are wondering what everything here does: import javax.swing*; brings in the fancy window graphics that make it look so good.
JFrame is creating a new window, frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); makes the window close when you click on the "X".
frame.getContentPane().add(new PushCounterPanel()); gets the program to look for "PushCounterPanel" (as shown below) which is what runs inside of the window.
frame.setVisible(true); tells it that the window itself is visible.
CODE
//PushCounterPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PushCounterPanel extends JPanel
{
private int count;
private JButton push;
private JLabel lavel;
public PushCounterPanel();
{
count = 0;
push = new JButton ("Push Counter");
push.addActionListener (new ButtonListener());
label = new JLabel ("You Pressed Teh Button " + count + " Times!!");
add (push);
add (label);
setPreferredSize (new Dimension (300, 40));
setBackround (Color.green);
}
public class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
count++;
label.setText ("You Pressed The Button " + count + " Times");
}
}
}
java.awt.event.*; imports the java utility that allows java to listen to each button press and eventually gets stored as "int count" through the line.
\"push.addActionListener (new ButtonListener());\". \"count = 0\" which sets the integer count at 0.
\"push = new JButton (\"Push Counter\");\" creates a new button named \"Push Counter\". \"label = new JLabel (\"You Pressed Teh Button \" + count + \" Times!!\");\" lets you know how many times you pushed the button by printing out the integer "count". "public class ButtonListener implements ActionListener" is the actual listener that is used through \"java.awt.event.*;\".
Just remember, you have to have both PushCounter and PushCounterPanel open for this to work, i hope this pointless little program brings a little light into your day, so have fun, nd wait for the next one :)
EXAMPLES:
http://totaldream.org/uploads/tutorials/java/PushCounter.java
&
http://totaldream.org/uploads/tutorials/java/PushCounterPanel.java
Sunday May 27, 2007 - 1182 reads