views:

73

answers:

2

in what way can this be programmed.

a UI box that displays random number between min and max value for 2 seconds then shows blank for 2 seconds then shows another random numer for 2 seconds then shows blank for 10 seonds and then repeats the cycle infitely until form closed. Font of the text to be configurable.

any help at all will be appreciated.

UPDATE after feedback

this is my progress so far. simple jpanel. now how do i add random number and timer

import javax.swing.JFrame;  
import javax.swing.JPanel;   
public class RandomSlide {  

public static void main(String[]args)  
{  
//Create a JPanel  
JPanel panel=new JPanel();  

//Create a JFrame that we will use to add the JPanel  
JFrame frame=new JFrame("Create a JPanel");  
//ADD JPanel INTO JFrame  
frame.add(panel);  
//Set default close operation for JFrame  
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
//Set JFrame size to :  
//WIDTH : 400 pixels  
//HEIGHT : 400 pixels  
frame.setSize(400,400);  
//Make JFrame visible. So we can see it  
frame.setVisible(true);  
 }  
 }  
+1  A: 

You have multiple languages in your tags, so I'm unsure what to repond with.

For java you'd make a JPanel with the appropriate UI elements and build a Timer instance to shoot off scheduled events every 2 seconds to update the value.

In VBA for Excel you'd have to do the same with a Form and Timer Control, however you may encounter more issues in Excel/VBA than in java.

Update 16/04/2010

You can actually sub-class the JPanel to clean up the code.

class RandomDialog extends JFrame
{
   private Timer _timer;
   private JPanel _container;     

   public RandomDialog()
   {
      _timer = new Timer();
      _container = new JPanel();

      // Etc...
   }
}

From here you can instantiate your children and register an event on the timer to call a function on your class which generates the random number and displays it to a JLabel.

Then you can just call your dialog in your driver like so:

public static void main(string [] args)
{
   RandomDialog rand = new RandomDialog();
   rand.show();
}
Aren
the jpanel part ... can you show with some coding or pseudocode
silverkid
+ 1 for `javax.swing.Timer`. @silverkid: Here's a stopwatch in Java that may help. http://stackoverflow.com/questions/2576353/stop-a-stopwatch/2576909#2576909
trashgod
A: 

Here is some code that will get you rolling. Note that you could just extend a JLabel instead of JPanel. I used JPanel based on your question. Also, note that you may use timer.setDelay() API to change the timing. Also, you could stop the timer by a stop() call.

package answer;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class RandomPanel extends JPanel implements ActionListener {
    private static final int TWO_SECONDS=2000;
    private static final int MAX=99999;
    private static final int MIN=0;
    private Timer timer = new javax.swing.Timer(TWO_SECONDS, this);

    private JLabel msgLabel;
    Random generator = new Random();
    public RandomPanel(Font f){
        msgLabel = new JLabel();
        msgLabel.setFont(f);
        msgLabel.setText(rand());
        add(msgLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
            msgLabel.setText(msgLabel.getText().equals("")?rand():"");
    }

    private String rand() {
        //generate random beteween MIN & MAX
        return Integer.toString((int) (MIN + Math.random() * ( MAX - MIN) + 0.5));
    }
    public void start(){
        timer.start();
    }

    /**
     * Rudimentary test
     * @param args
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        RandomPanel randomPanel = new RandomPanel(new Font("Serif", Font.BOLD, 50));
        frame.getContentPane().add(randomPanel);
        frame.setSize(400,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        randomPanel.start();
    }
}
ring bearer