java程序这个为什么我添加菜单栏不报错也没出现菜单栏? 20
importjavax.swing.*;importjava.awt.event.InputEvent;importjava.awt.event.KeyEvent;imp...
import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.text.DateFormat;
import java.util.*;
import javax.swing.*;
//数字时钟
public class ClockDemo extends JFrame implements Runnable
{
Thread clock;
final int Xpoint=180;
final int Ypoint=180;
final int R=80;
int xHour=0,yHour=0,xSecond=0,ySecond=0,xMin=0,yMin=0;
public ClockDemo()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//创建并添加各菜单,注意:菜单的快捷键是同时按下Alt键和字母键,方法setMnemonic('F')是设置快捷键为Alt +F
JMenu menuFile = new JMenu("文件(F)"), menuEdit = new JMenu("编辑(E)"), menuView = new JMenu("查看(V)");
menuFile.setMnemonic('F');
menuEdit.setMnemonic('E');
menuView.setMnemonic('V');
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuView);
//添加“文件”菜单的各菜单项
JMenu itemOpen = new JMenu("打开");
itemOpen.setMnemonic('O');
JMenuItem itemOpen1 = new JMenuItem("打开x");
JMenuItem itemOpen2 = new JMenuItem("打开y");
itemOpen.add(itemOpen1);
itemOpen.add(itemOpen2);
JMenuItem itemSave = new JMenuItem("保存");
itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menuFile.add(itemOpen);
menuFile.add(itemSave);
//添加“编辑”菜单的各菜单项
JMenuItem itemCopy = new JMenuItem("复制");
itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
menuEdit.add(itemCopy);
//添加“查看”菜单的各菜单项
JMenuItem itemStop = new JMenuItem("停止"), itemRefresh = new JMenuItem("刷新");
itemStop.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
itemRefresh.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
menuView.add(itemStop);
menuView.add(itemRefresh);
//调用父类构造函数
setFont(new Font("宋体",Font.BOLD,20)); //设置时钟的显示字体
start(); //开始进程
setSize(400,400); //设置窗口尺寸
setVisible(true); //窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void start()
{ //开始进程
if(clock==null)//如果进程为空值
{
clock=new Thread(this); //实例化进程
clock.start(); //开始进程
}
} 展开
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.text.DateFormat;
import java.util.*;
import javax.swing.*;
//数字时钟
public class ClockDemo extends JFrame implements Runnable
{
Thread clock;
final int Xpoint=180;
final int Ypoint=180;
final int R=80;
int xHour=0,yHour=0,xSecond=0,ySecond=0,xMin=0,yMin=0;
public ClockDemo()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//创建并添加各菜单,注意:菜单的快捷键是同时按下Alt键和字母键,方法setMnemonic('F')是设置快捷键为Alt +F
JMenu menuFile = new JMenu("文件(F)"), menuEdit = new JMenu("编辑(E)"), menuView = new JMenu("查看(V)");
menuFile.setMnemonic('F');
menuEdit.setMnemonic('E');
menuView.setMnemonic('V');
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuView);
//添加“文件”菜单的各菜单项
JMenu itemOpen = new JMenu("打开");
itemOpen.setMnemonic('O');
JMenuItem itemOpen1 = new JMenuItem("打开x");
JMenuItem itemOpen2 = new JMenuItem("打开y");
itemOpen.add(itemOpen1);
itemOpen.add(itemOpen2);
JMenuItem itemSave = new JMenuItem("保存");
itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menuFile.add(itemOpen);
menuFile.add(itemSave);
//添加“编辑”菜单的各菜单项
JMenuItem itemCopy = new JMenuItem("复制");
itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
menuEdit.add(itemCopy);
//添加“查看”菜单的各菜单项
JMenuItem itemStop = new JMenuItem("停止"), itemRefresh = new JMenuItem("刷新");
itemStop.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
itemRefresh.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
menuView.add(itemStop);
menuView.add(itemRefresh);
//调用父类构造函数
setFont(new Font("宋体",Font.BOLD,20)); //设置时钟的显示字体
start(); //开始进程
setSize(400,400); //设置窗口尺寸
setVisible(true); //窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void start()
{ //开始进程
if(clock==null)//如果进程为空值
{
clock=new Thread(this); //实例化进程
clock.start(); //开始进程
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询