我创建了一个显示当前时间的程序,并且可以选择通过使用按钮打开第二个窗口来设置警报。在第二个窗口中输入闹钟时间后,您可以在第一个窗口中使用复选框激活闹钟。我已经创建了两个窗口,但我不确定如何将第二个窗口中的警报变量传递给第一个窗口中的复选框。
第一个窗口的代码:
enter code here
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JOptionPane;
import java.util.*;
public class Exercise18_21 extends JFrame {
private JTextField jtfHour, jtfMinute, jtfSeconds, jtfAlarm;
private JLabel hour, minute, seconds;
private JCheckBox jchkAlarm;
private JButton jbtAlarm;
private SetAlarm setAlarm = new SetAlarm();
private JFrame SetAlarmFrame = new JFrame();
private javax.swing.Timer t;
private int alarmHour;
//private int num1, num2, num3;
public Exercise18_21()
{
setLayout(new GridLayout(3,1,2,2));
GridBagConstraints c = new GridBagConstraints();
hour = new JLabel("Hour");
add(hour, c);
minute = new JLabel("Minutes");
add(minute, c);
seconds = new JLabel("Seconds");
add(seconds, c);
jtfHour = new JTextField(10);
add(jtfHour, c);
jtfMinute= new JTextField(5);
add(jtfMinute, c);
jtfSeconds = new JTextField(5);
add(jtfSeconds, c);
jchkAlarm = new JCheckBox("Alarm");
add(jchkAlarm, c);
jbtAlarm = new JButton("Set Alarm");
add(jbtAlarm, c);
javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener());
t.start();
jbtAlarm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SetAlarmFrame.setVisible(true);
}
});
jchkAlarm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Calendar calendar = new GregorianCalendar();
int h = calendar.get(Calendar.HOUR_OF_DAY);
jtfHour.setText("" + h);
int m = calendar.get(Calendar.MINUTE);
jtfMinute.setText("" + m);
int S = calendar.get(Calendar.SECOND);
jtfSeconds.setText("" + S);
//System.out.print(h);
//System.out.print(m);
//System.out.print(S);
}
});
SetAlarmFrame.add(setAlarm);
SetAlarmFrame.pack();
SetAlarmFrame.setTitle("Set Alarm");
}//end of constructor
private int getHour() {
Calendar calendar = new GregorianCalendar();
int alarmHour = calendar.get(Calendar.HOUR_OF_DAY);
return alarmHour; // Return hour
}
/////////////////////////////////////////////// inner class ClockListener
class ClockListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Calendar calendar = new GregorianCalendar();
int h = calendar.get(Calendar.HOUR_OF_DAY);
jtfHour.setText("" + h);
int m = calendar.get(Calendar.MINUTE);
jtfMinute.setText("" + m);
int S = calendar.get(Calendar.SECOND);
jtfSeconds.setText("" + S);
}
}
public static void main(String [] arg) {
Exercise18_21 gui = new Exercise18_21();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(200, 100);
gui.setTitle("Exercise18_21");
}//end of main
}//end of exercise18_21 class
设置警报的第二个窗口的代码:
enter code here
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JOptionPane;
import java.util.*;
public class SetAlarm extends JPanel {
private JTextField jtfHour2, jtfMinute, jtfSeconds;
private JLabel hour, minute, seconds;
private JButton jbtOK, jbtCancel;
private int num1, num2, num3;
public SetAlarm()
{
setLayout(new GridLayout(4,2,2,2));
GridBagConstraints c = new GridBagConstraints();
hour = new JLabel("Hour");
add(hour, c);
jtfHour2 = new JTextField(10);
add(jtfHour2, c);
minute = new JLabel("Minutes");
add(minute, c);
jtfMinute= new JTextField(5);
add(jtfMinute, c);
seconds = new JLabel("Seconds");
add(seconds, c);
jtfSeconds = new JTextField(5);
add(jtfSeconds, c);
jbtOK = new JButton("OK");
add(jbtOK, c);
jbtCancel = new JButton("Cancel");
add(jbtCancel, c);
jbtCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
jbtOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int alarmHour = getHour();
//int num1 = Integer.parseInt(jtfHour2.getText().trim());
//int num2 = Integer.parseInt(jtfMinute.getText().trim());
//int num3 = Integer.parseInt(jtfSeconds.getText().trim());
System.out.print(alarmHour);
System.out.print(num2);
System.out.print(num3);
}
});
}//end of constructor
public void showHour(int alarmHour)
{
this.alarmHour = alarmHour;
}
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}//end of Dimension getPreferredSize
}//end of setAlarm class
第二个窗口的新代码:
public class SetAlarm extends JDialog {
private JTextField jtfHour2, jtfMinute, jtfSeconds;
private JLabel hour, minute, seconds;
private JButton jbtOK, jbtCancel;
protected int num1, num2, num3;
public SetAlarm()
{
setLayout(new GridLayout(4,2,2,2));
GridBagConstraints c = new GridBagConstraints();
hour = new JLabel("Hour");
add(hour, c);
jtfHour2 = new JTextField(10);
add(jtfHour2, c);
minute = new JLabel("Minutes");
add(minute, c);
jtfMinute= new JTextField(5);
add(jtfMinute, c);
seconds = new JLabel("Seconds");
add(seconds, c);
jtfSeconds = new JTextField(5);
add(jtfSeconds, c);
jbtOK = new JButton("OK");
add(jbtOK, c);
jbtCancel = new JButton("Cancel");
add(jbtCancel, c);
jbtCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
System.exit(0);
}
});
jbtOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int num1 = Integer.parseInt(jtfHour2.getText().trim());
//int num2 = Integer.parseInt(jtfMinute.getText().trim());
//int num3 = Integer.parseInt(jtfSeconds.getText().trim());
//System.out.print(alarmHour);
//System.out.print(num2);
//System.out.print(num3);
setVisible(false);
}
});
}//end of constructor
public int getHour()
{
return num1;
}
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}//end of Dimension getPreferredSize
}//end of setAlarm class
第一个窗口的新代码
public class Exercise18_21 extends JFrame {
private JTextField jtfHour, jtfMinute, jtfSeconds, jtfAlarm;
private JLabel hour, minute, seconds;
private JCheckBox jchkAlarm;
private SetAlarm setAlarm1= new SetAlarm();;
private JButton jbtAlarm;
private int h1, m1, s1;
//private JFrame SetAlarmFrame = new JFrame();
private javax.swing.Timer t;
//private int num1, num2, num3;
public Exercise18_21()
{
setLayout(new GridLayout(3,1,2,2));
GridBagConstraints c = new GridBagConstraints();
hour = new JLabel("Hour");
add(hour, c);
minute = new JLabel("Minutes");
add(minute, c);
seconds = new JLabel("Seconds");
add(seconds, c);
jtfHour = new JTextField(10);
add(jtfHour, c);
jtfMinute= new JTextField(5);
add(jtfMinute, c);
jtfSeconds = new JTextField(5);
add(jtfSeconds, c);
jchkAlarm = new JCheckBox("Alarm");
add(jchkAlarm, c);
jbtAlarm = new JButton("Set Alarm");
add(jbtAlarm, c);
javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener());
t.start();
jbtAlarm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setAlarm1.setVisible(true);
}
});
jchkAlarm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setAlarm1.setModal(true) ;
setAlarm1.getHour();
int h1 = Integer.parseInt(jtfHour.getText().trim());
System.out.print(setAlarm1.num1);
}
});
//SetAlarmFrame.add(setAlarm);
//SetAlarmFrame.pack();
//SetAlarmFrame.setTitle("Set Alarm");
}//end of constructor
/////////////////////////////////////////////// inner class ClockListener
class ClockListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Calendar calendar = new GregorianCalendar();
int h = calendar.get(Calendar.HOUR_OF_DAY);
jtfHour.setText("" + h);
int m = calendar.get(Calendar.MINUTE);
jtfMinute.setText("" + m);
int S = calendar.get(Calendar.SECOND);
jtfSeconds.setText("" + S);
}
}
public static void main(String [] arg) {
Exercise18_21 gui = new Exercise18_21();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(200, 100);
gui.setTitle("Exercise18_21");
}//end of main
}//end of exercise18_21 class