java如何监听菜单项点击_如何实现点击菜单项运行其他操作?

这是实现窗口的部分代码;

package EngineeringDesign;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JRadioButtonMenuItem;

import javax.swing.JTextArea;

import javax.swing.KeyStroke;

public class MenuDemo implements ActionListener {

private String newline = "\n";

private JTextArea output;

public JMenuBar createMenuBar(){

JMenuBar menubar =new JMenuBar();

JMenu menu,submenu,imenu = new JMenu();

JMenuItem menuitem,menuitem1,menuitem2;

JRadioButtonMenuItem rbmenu;

JCheckBoxMenuItem checkbox;

//定义第一个菜单

menu = new JMenu("(A)功能菜单");

menu.setMnemonic(KeyEvent.VK_A);

menubar.add(menu);

menuitem = new JMenuItem("(O)计时秒表窗口");

menuitem.setMnemonic(KeyEvent.VK_O);

//设置快捷键

menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.ALT_MASK));

//设置监听,当键盘或鼠标操作是调用操作

menu.add(menuitem);

menuitem.addActionListener(this);

public static void main(String[] args) {

JFrame frame = new JFrame("菜单演示窗口");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MenuDemo demo = new MenuDemo();

JMenuBar mb = demo.createMenuBar();

frame.setJMenuBar(mb);

frame.setSize(500,400);

frame.setLocation(450,300);

frame.setVisible(true);

}

}是时钟的代码

package EngineeringDesign;

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.text.DateFormat;

import java.awt.event.*;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

/**

* This is the main Function of the program.

*/

public class Clock{

public static void main(String []args){

ClockFrame frame = new ClockFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

/**

* This class is used to define the main frame of the clock

*/

class ClockFrame extends JFrame{

//constructor function

public ClockFrame(){

setTitle("小时钟");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);

ClockPanel panel = new ClockPanel();

add(panel);

}

//variables of the frame

private int DEFAULT_LOC_WIDTH = 300;

private int DEFAULT_LOC_HEIGHT = 300;

private int DEFAULT_WIDTH = 330;

private int DEFAULT_HEIGHT = 380;

}

/**

* This class is used to defind the main panel of the clock

*/

class ClockPanel extends JPanel{

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

//get the time of the system

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);

GregorianCalendar calendar = new GregorianCalendar();

calendar.setTime(new Date());//dateFormat.format(calendar.getTime())

String timeInfo=""; //输出信息

int hour = calendar.get(Calendar.HOUR);

int minute = calendar.get(Calendar.MINUTE);

int second = calendar.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.black);  //设置当前颜色为橙色

g.drawString(timeInfo,130,300);  //显示时间字符串

g.setColor(Color.black);

g.drawString(dateFormat.format(calendar.getTime()),10,30);

g.setColor(Color.black);

g.setFont(new Font("SAN_SERIF",Font.BOLD,15));

for(int i=0,num=12;i<360;i+=6)

{

double alfa = Math.toRadians(i);

int xPos=CENTER_X+(int)(R*Math.sin(alfa));

int yPos=CENTER_Y-(int)(R*Math.cos(alfa));

if(i%10==0)

{

if (num%3==0)

g.setColor(Color.red);  //  数字3,6,9,12为红色

else

g.setColor(Color.black);  // 其余数字为黑色

if(num==12)

{

g.drawString(""+num,xPos-5,yPos+3);}// 写数字

else

g.drawString(""+(num%12),xPos-5,yPos+3);

num=num+1;

}

}

//draw the clock face

Ellipse2D clockFace = new Ellipse2D.Double();

clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);

g2.setColor(Color.BLUE);

g2.draw(clockFace);

//draw the clock center

Ellipse2D clockCenter = new Ellipse2D.Double();

clockCenter.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);

g2.setColor(Color.RED);

g2.fill(clockCenter);

//help to get the exact position of the lines

double lenX, lenY, posX, posY;

//draw the clock second line

Line2D clockSecond = new Line2D.Double();

double secondTime = (double) calendar.get(Calendar.SECOND);

lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);

lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);

posX = CENTER_X + lenX;

posY = CENTER_Y - lenY;

clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);

g2.setColor(Color.PINK);

g2.draw(clockSecond);

//draw the clock minute line

Line2D clockMinute = new Line2D.Double();

double minuteTime = (double) calendar.get(Calendar.MINUTE);

lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);

lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);

posX = CENTER_X + lenX;

posY = CENTER_Y - lenY;

clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);

g2.setColor(Color.GREEN);

g2.draw(clockMinute);

//draw the clock hour line

Line2D clockHour = new Line2D.Double();

double hourTime = (double) calendar.get(Calendar.HOUR);

lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));

lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));

posX = CENTER_X + lenX;

posY = CENTER_Y - lenY;

clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);

g2.setColor(Color.BLUE);

g2.draw(clockHour);

int delay = 1000;

// actionListener

ActionListener drawClock;

drawClock=new ActionListener(){

public void actionPerformed(ActionEvent evt){

repaint();

}

};

//create timer

new Timer(delay, drawClock).start();

}

//variables of the panel

private int HOUR_LEN = 50;

private int MINUTE_LEN = 70;

private int SECOND_LEN = 90;

private int RADIUS = 100;

private int INNER_RADIUS = 2;

private int CENTER_X = 150;

private int CENTER_Y = 150;

private int R=90;

}

d60bdd639b26e7b7479041a0ff9c5ff6.png如图所示点击“时钟窗口”弹出时钟。主窗口和时钟写在不同类中。大神,代码在上方请问如何实现?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值