Click to See Complete Forum and Search --> : Button help
308holes
Mar 27th, 2002, 04:39 AM
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?
daveyboy
Mar 27th, 2002, 05:31 AM
This should do what you want.........
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);
}
}
daveyboy
Mar 27th, 2002, 05:32 AM
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.....
308holes
Mar 27th, 2002, 01:16 PM
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!!
daveyboy
Mar 28th, 2002, 11:02 AM
Just in case anyone does want some code for file output:
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............
:)
Pix
Mar 28th, 2002, 12:02 PM
:)
308holes
Mar 28th, 2002, 02:47 PM
Thanks
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.