Results 1 to 7 of 7

Thread: Button help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Button help

    Im stuck in java on buttons can someone make me a script that has 2 buttons that when you click the first button it diplays the time in a label and for the other one when you press it it display the date ?? in a second label?

  2. #2
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    This should do what you want.........

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    
    public class Buttons extends JFrame
    {
        JLabel dateTimeJLabel = new JLabel("");
        JButton dateJButton = new JButton("Show Date");
        JButton timeJButton = new JButton("Show Time");
    
        public Buttons()
        {
            dateJButton.addActionListener(new Listener());
            timeJButton.addActionListener(new Listener());
            JPanel mainJP = new JPanel(new GridLayout(2,1));
            getContentPane().add(mainJP);
            mainJP.add(dateTimeJLabel); 
            JPanel buttonJP = new JPanel(new GridLayout(1,2));
            buttonJP.add(dateJButton); 
            buttonJP.add(timeJButton);
            mainJP.add(buttonJP);
    
        }
        public class Listener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String command = e.getActionCommand();
                if (command.equals("Show Date"))
                {
                    Calendar cal = Calendar.getInstance();
                    String theDate = "" + cal.get(Calendar.DAY_OF_MONTH);
                    theDate += "/" + (cal.get(Calendar.MONTH) + 1);
                    theDate += "/" + cal.get(Calendar.YEAR);
                    dateTimeJLabel.setText(theDate); 
                }
                if (command.equals("Show Time"))
                {
                    Calendar cal = Calendar.getInstance();
                    String theDate = "" + cal.get(Calendar.HOUR);
                    theDate += ":" + (cal.get(Calendar.MINUTE));
                    theDate += ":" + cal.get(Calendar.SECOND);
                    dateTimeJLabel.setText(theDate); 
                }
            }
        }
    
        public static void main(String [] args)
        {
            Buttons b = new Buttons();
            b.setVisible(true);
            b.setBounds(50,50,200,200);
        }
    }


    Infinity isn't large it's just incomprehensible

  3. #3
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    For 2 buttons just change the mainJP.add to add a GridLayout then add another JLabel within there

    then within the ActionListener just change the JLabel that the Time gets set to.....
    Last edited by daveyboy; Mar 27th, 2002 at 05:38 AM.


    Infinity isn't large it's just incomprehensible

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Ok thanks that helped me a lot i was stuck on how the button would know what action to preform but you just fixed that thanks!!

  5. #5
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    Just in case anyone does want some code for file output:

    Code:
    import java.io.*;
    
    public class FileSavingDemo implements Serializable
    {
        File myFile = new File("sd.rnd");
        SerializableDemo sd;
    
        public void init()
        {
            if (myFile.exists()) //the file has been created, therefore can be used
            {
                try
                {
                    FileInputStream in = new FileInputStream(myFile);
                    ObjectInputStream p;
                    try
                    {
                        p = new ObjectInputStream(in);
                        Object s;
                        try
                        {
                            s = p.readObject();
                        }
                        catch(ClassNotFoundException e)
                        {
                            s = "";
                        } 
                    }
                    catch(StreamCorruptedException e)
                    {
                        sd = new SerializableDemo(new java.util.Date().toString());
                        System.out.println("Corrupted File, created to be written: " + sd.getText());                    
                    }
     
                    if (s instanceof SerializableDemo)
                    { 
                        sd = (SerializableDemo)s;
                        System.out.println("Read from file: " + sd.getText());
                    }
                    else
                    {
                       sd = new SerializableDemo(new java.util.Date().toString());
                    }
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                    System.out.println("Error on IO, created to be written: " + sd.getText());
                    sd = new SerializableDemo(new java.util.Date().toString());
                }
            }  
            else
            {
               sd = new SerializableDemo(new java.util.Date().toString());
               System.out.println("No file detected, created to be written: " + sd.getText());
            } 
        }
    
        public void save()
        {
            try
            {
                FileOutputStream out = new FileOutputStream(myFile);
                ObjectOutputStream p = new ObjectOutputStream(out);
                sd = new SerializableDemo(new java.util.Date().toString());
                System.out.println("Written to file: " + sd.getText());
                p.writeObject(sd);
                p.flush();
                out.close();
            }
            catch(IOException e){}
        }
    
        public class SerializableDemo implements Serializable
        {
            String text;
    
            public SerializableDemo(String theText)
            {
                text = theText;
            }
    
            public void setText(String theText)
            {
                text = theText;
            }
    
            public String getText()
            {
                return text;
            }
        }
    
        public static void main(String [] args)
        {
            FileSavingDemo fd = new FileSavingDemo();
            fd.init();
            fd.save();
        }
    }
    If anyone needs any more help let me know............



    Infinity isn't large it's just incomprehensible

  6. #6
    Hyperactive Member Pix's Avatar
    Join Date
    Feb 2001
    Location
    I'm not telling you (or them)
    Posts
    282

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width