import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
public class AlarmClock {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
AlarmFrame f = new AlarmFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}@SuppressWarnings("serial")
class AlarmFrame extends JFrame implements Runnable {
Thread clock; public AlarmFrame() {
MenuItem b=null;
//MenuItem btStart=null;
//MenuItem btStop=null;
//final boolean turnOn;
//turnOn=true;
setTitle("AlarmClock");
setSize(200, 100);
setFont(new Font("Times New Roman", Font.BOLD, 40));
start();
//Menu switchMenu = new Menu("开关设置");
//btStart=new MenuItem("开");
//switchMenu.add(btStart);
//btStop=new MenuItem("关");
//switchMenu.add(btStop);
Menu timeMenu = new Menu("闹铃设置");
b=new MenuItem("闹铃时间");
timeMenu.add(b);
b.addActionListener(listener);
Menu playMenu = new Menu("播放设置");
playMenu.add(new MenuItem("循环播放"));
playMenu.add(new MenuItem("停止播放"));
MenuBar menuBar = new MenuBar();
setMenuBar(menuBar);
//menuBar.add(switchMenu);
menuBar.add(timeMenu);
menuBar.add(playMenu);
}public void start() {
if (clock == null) {
clock = new Thread(this);
clock.start();
}
}
public void run() {
while (clock != null) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
} public void stop() {
clock = null;
} public void paint(Graphics g) {
Calendar now = new GregorianCalendar();
String timeInfo = "";
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND); if (hour <= 9)
timeInfo += "0" + hour + ":";
else
timeInfo += hour + ":";
if (minute <= 9)
timeInfo += "0" + minute + ":";
else
timeInfo += minute + ":";
if (second <= 9)
timeInfo += "0" + second;
else
timeInfo += second; g.setColor(Color.white);
Dimension dim = getSize();
g.fillRect(0, 0, dim.width, dim.height);
g.setColor(Color.orange);
g.drawString(timeInfo, 20, 80); }
ActionListener listener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if((e.getActionCommand()).equals("闹铃时间")){
TextField tfHour;
TextField tfMinute;
TextField tfSecond;
Button btStart,btStop;
tfHour=new TextField(1);
tfMinute=new TextField(1);
tfSecond=new TextField(1);
JFrame ff=new JFrame("闹铃时间");
ff.setSize(250,100);
ff.setVisible(true);
final Panel p1=new Panel();
final Panel p2=new Panel();
p1.add(new Label("闹铃时间"));
btStart=new Button("开");
btStop=new Button("关");
p2.add(btStart);
p2.add(btStop);
p1.add(tfHour);
p1.add(new Label(":"));
p1.add(tfMinute);
p1.add(new Label(":"));
p1.add(tfSecond);
ff.add(p1);
ff.add(p2,BorderLayout.SOUTH);
boolean turnOn;
turnOn=true;
}
}
};
btStart.addActionListener(new ActionListener(){ //打开闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=true; //设置打开标志为True
}
});
btStop.addActionListener(new ActionListener(){ //关闭闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=false; //设置打开标志为false
}
});
}
这代码是在菜单中点击“闹铃设置'后,再点里面的子菜单“闹铃时间”就弹出一个新窗口,我在新窗口里设置了两个按钮“开”“关”,想给这两个按钮加监听器,也就是 btStart.addActionListener(new ActionListener(){ //打开闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=true; //设置打开标志为True
}
});
btStop.addActionListener(new ActionListener(){ //关闭闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=false; //设置打开标志为false
}
});
不知这代码应放在哪里,请指教,分不多,但请帮帮忙,谢谢……
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
public class AlarmClock {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
AlarmFrame f = new AlarmFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}@SuppressWarnings("serial")
class AlarmFrame extends JFrame implements Runnable {
Thread clock; public AlarmFrame() {
MenuItem b=null;
//MenuItem btStart=null;
//MenuItem btStop=null;
//final boolean turnOn;
//turnOn=true;
setTitle("AlarmClock");
setSize(200, 100);
setFont(new Font("Times New Roman", Font.BOLD, 40));
start();
//Menu switchMenu = new Menu("开关设置");
//btStart=new MenuItem("开");
//switchMenu.add(btStart);
//btStop=new MenuItem("关");
//switchMenu.add(btStop);
Menu timeMenu = new Menu("闹铃设置");
b=new MenuItem("闹铃时间");
timeMenu.add(b);
b.addActionListener(listener);
Menu playMenu = new Menu("播放设置");
playMenu.add(new MenuItem("循环播放"));
playMenu.add(new MenuItem("停止播放"));
MenuBar menuBar = new MenuBar();
setMenuBar(menuBar);
//menuBar.add(switchMenu);
menuBar.add(timeMenu);
menuBar.add(playMenu);
}public void start() {
if (clock == null) {
clock = new Thread(this);
clock.start();
}
}
public void run() {
while (clock != null) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
} public void stop() {
clock = null;
} public void paint(Graphics g) {
Calendar now = new GregorianCalendar();
String timeInfo = "";
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND); if (hour <= 9)
timeInfo += "0" + hour + ":";
else
timeInfo += hour + ":";
if (minute <= 9)
timeInfo += "0" + minute + ":";
else
timeInfo += minute + ":";
if (second <= 9)
timeInfo += "0" + second;
else
timeInfo += second; g.setColor(Color.white);
Dimension dim = getSize();
g.fillRect(0, 0, dim.width, dim.height);
g.setColor(Color.orange);
g.drawString(timeInfo, 20, 80); }
ActionListener listener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if((e.getActionCommand()).equals("闹铃时间")){
TextField tfHour;
TextField tfMinute;
TextField tfSecond;
Button btStart,btStop;
tfHour=new TextField(1);
tfMinute=new TextField(1);
tfSecond=new TextField(1);
JFrame ff=new JFrame("闹铃时间");
ff.setSize(250,100);
ff.setVisible(true);
final Panel p1=new Panel();
final Panel p2=new Panel();
p1.add(new Label("闹铃时间"));
btStart=new Button("开");
btStop=new Button("关");
p2.add(btStart);
p2.add(btStop);
p1.add(tfHour);
p1.add(new Label(":"));
p1.add(tfMinute);
p1.add(new Label(":"));
p1.add(tfSecond);
ff.add(p1);
ff.add(p2,BorderLayout.SOUTH);
boolean turnOn;
turnOn=true;
}
}
};
btStart.addActionListener(new ActionListener(){ //打开闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=true; //设置打开标志为True
}
});
btStop.addActionListener(new ActionListener(){ //关闭闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=false; //设置打开标志为false
}
});
}
这代码是在菜单中点击“闹铃设置'后,再点里面的子菜单“闹铃时间”就弹出一个新窗口,我在新窗口里设置了两个按钮“开”“关”,想给这两个按钮加监听器,也就是 btStart.addActionListener(new ActionListener(){ //打开闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=true; //设置打开标志为True
}
});
btStop.addActionListener(new ActionListener(){ //关闭闹钟按钮事件处理
public void actionPerformed(final ActionEvent event){
turnOn=false; //设置打开标志为false
}
});
不知这代码应放在哪里,请指教,分不多,但请帮帮忙,谢谢……
要使用在你这2个匿名内部类里面又必须是final,但是final修饰的参数不能被修改,所以我觉得你在逻辑上应该改下