动作侦听器 - 从另一个文件连接

时间:2013-10-05 19:33:05

标签: java awt actionlistener

我不知道该怎么办才能让ActionListenerAlarmCalc一起使用,并显示闹钟响起的结果。我也正在删除询问时间并显示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());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的示例,您有三个基本选择......

  1. 您将AlarmCalc类的代码复制到AlarmFrame
  2. 您可以调用AlarmCalc,但这不允许您从框架中将所需信息传递给类
  3. 您编写了一个新方法,它允许您将时间值从主UI传递给它并进行所需的计算并返回结果,以便它可以显示在UI上