我不知道该怎么办才能让ActionListener
与AlarmCalc
一起使用,并显示闹钟响起的结果。我也正在删除询问时间并显示JOptionPane
时间的Alarm
代码。
提前感谢您的帮助!
MainP3
import java.net.MalformedURLException;
import javax.swing.JFrame;
public class MainP3
{
public static void main(String[] args) throws MalformedURLException
{
AlarmFrame frame = new AlarmFrame();
frame.pack();
frame.setTitle("Alarm Clock");
frame.setSize(400, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new AlarmCalc();
}
}
AlarmCalc
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class AlarmCalc {
public AlarmCalc()
{
// Current computer time
Date dNow = new Date( );
// Creating format
SimpleDateFormat ft = new SimpleDateFormat ("MM/dd/yyyy HH:mm");
String userTime;
// Asking user for the date and time
userTime = JOptionPane.showInputDialog(null, "Please enter a date, time in military time:\n Example: 10/08/2013 12:00");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");
// Computing the computer time and user time to see when the alarm will end
Date Current = null;
Date User = null;
try
{
Current = dNow;
User = format.parse(userTime);
// Computing the time difference
long diffTime = User.getTime() - Current.getTime();
long diffSeconds = diffTime / 1000 % 60;
long diffMinutes = diffTime / (60 * 1000) % 60;
long diffHours = diffTime / (60 * 60 * 1000) % 24;
long diffDays = diffTime / (24 * 60 * 60 * 1000);
// Reporting when the alarm will hit
JOptionPane.showMessageDialog(null,diffDays + " days, " + diffHours + " hours, " + diffMinutes
+ " minutes, " + diffSeconds + " seconds" );
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
AlarmFrame
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AlarmFrame extends JFrame
{
private JTextField jtfAlarmClock = new JTextField();
private JButton jbtSetAlarm = new JButton("Set Alarm");
public AlarmFrame ()
{
JPanel p1 = new JPanel(new GridLayout(1, 1));
//p1.add(new JLabel("Enter Time: "));
p1.add(jtfAlarmClock);
p1.setBorder(new TitledBorder("Enter a Time: "));
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtSetAlarm);
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
jbtSetAlarm.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
Double.parseDouble(jtfAlarmClock.getText());
}
}
}
答案 0 :(得分:0)
根据您的示例,您有三个基本选择......