📄 demomenu.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.geom.*;
public class DemoMenu
{
public static void main(String[] args)
{
//初始化主画框,调整其位置和宽度,使得显示出来的按钮更漂亮
MainFrame.init("欢迎进入JAVA语言的GUI世界",MainFrame.screenWidth/2,MainFrame.screenHeight/4,MainFrame.screenWidth/3,MainFrame.screenHeight/3,"system");
DemoMenuCreator demo=new DemoMenuCreator(MainFrame.getContentPane(),MainFrame.getMainFrame());
//创建演示用的菜单和组件,并将菜单放置在主画框中,组件放置在主画框的内容面板中
demo.createMenu();
//启动主画框,并进行演示
MainFrame.start();
}
}
class DemoMenuCreator
{
private Container place;//放置演示组件的容器
private JFrame topLevelFrame;//放置菜单的顶层容器
private JTextArea output;//用于输出结果的文本区域
public DemoMenuCreator(Container place,JFrame topLevelFrame){
this.place=place;
this.topLevelFrame=topLevelFrame;
}
//创建用于演示的组件
public void createMenu(){
//创建一放置在滚动窗格中的文本区域,用来输出用户对菜单的选择结果
output=new JTextArea();
output.setEditable(false);
JScrollPane scrollPane=new JScrollPane(output);
place.add(scrollPane,BorderLayout.CENTER);
//创建菜单的监听器
MenuListener menuListener=new MenuListener();
//创建菜单条
JMenuBar menuBar=new JMenuBar();
topLevelFrame.setJMenuBar(menuBar);//放置在顶层容器中
//创建第一个主菜单项
JMenu menu= new JMenu("FILE(F)");
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);//加入到菜单条
//设置第一个主菜单项的第一个子菜单项
JMenuItem menuItem=new JMenuItem("新建(N)",new ImageIcon("images/new.gif"));
menuItem.setMnemonic(KeyEvent.VK_N);
//设置此菜单项的加速键为,<ctrl+N>
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
menuItem.addActionListener(menuListener);
menu.add(menuItem);//加入到第一个主菜单项中
//加入一带子菜单项的菜单项
menu.addSeparator();
JMenu submenu =new JMenu("发送");
menuItem =new JMenuItem("到邮件(M)",KeyEvent.VK_M);
//使用<CTL+ALT+M>作为加速键
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,ActionEvent.ALT_MASK|ActionEvent.CTRL_MASK));
menuItem.addActionListener(menuListener);
submenu.add(menuItem);
menuItem =new JMenuItem("到桌面快捷方式");
menuItem.addActionListener(menuListener);
submenu.add(menuItem);//加入到子菜单中
menu.add(submenu);//将该子菜单加入到第一个主菜单中
menu.addSeparator();
//为第一个主菜单添加最后一个菜单项
menuItem =new JMenuItem("退出");
menuItem.addActionListener(menuListener);
menuItem.setActionCommand("exit");
menu.add(menuItem);
//第二个主菜单项
menu =new JMenu("帮助(H)");
menu.setMnemonic(KeyEvent.VK_H);
menuBar.add(menu);
menuItem =new JMenuItem("关于```");
menuItem.setAccelerator(KeyStroke.getKeyStroke("F1"));
menuItem.addActionListener(menuListener);
menuItem.setActionCommand("about");
menu.add(menuItem);
}
//监听菜单项的按下动作
private class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
JMenuItem source=(JMenuItem)(e.getSource());
String command =source.getActionCommand();
if (command.equals("about"))
{
MouseTest mousetest=new MouseTest();
//弹出一窗口显示一些信息
//JOptionPane.showMessageDialog(MainFrame.getMainFrame(),"面向对象程序设计与JAVA语言V1.0","关于",JOptionPane.WARNING_MESSAGE);
}else if (command.equals("exit"))
{
System.exit(1);//退出整个程序
}
else {
//其他菜单项则简单地显示用户的选择
String s="你选择了菜单项:" + source.getText()+"\n";
output.append(s);//添加到输出文本区域中
}
}
}
}
class MainFrame
{
//获取显示器的高度和宽度,并置为公有属性,使用者可据此计算画框的位置
public static final int screenWidth= (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
public static final int screenHeight=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
//设置主画框的缺省位置和缺省宽度
private static int width =screenWidth/3;
private static int height =screenHeight/4;
private static int startX =screenWidth/3;
private static int startY =screenHeight/3;
private static JFrame frame;
private static JPanel contentPane;
//使用私有的构造方法可防止使用者创建MainFrame对象,这是工具类的常见做法
private MainFrame() {}
//不使用构造方法,而使用INIT()方法初始化,任何使用类MainFrame的程序必须先调用INIT()方法
public static void init(String title) {
frame =new JFrame(title);
frame.setLocation(new Point(startX,startY));
contentPane=(JPanel)frame.getContentPane();
contentPane.setPreferredSize(new Dimension(width,height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void init(String title,int w,int h,int x,int y) {
width=w;height=h;startX=x;startY=y;
init(title);
}
//初始化画框并设置画框的观感
public static void init(String title,int w,int h,int x,int y,String lookAndFeel){
try
{
if (lookAndFeel.equalsIgnoreCase("windows"))
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
else if (lookAndFeel.equalsIgnoreCase("system"))
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
else if (lookAndFeel.equalsIgnoreCase("motif"))
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
else UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
}
width=w;height=h;startX=x;startY=y;
init(title);
}
//使画框可见,从而启动整个GUI
public static void start() {frame.pack();frame.setVisible(true);}
//获取画框的内容窗格,使用者可往此窗格添加所创建的GUI组件
public static JPanel getContentPane(){return contentPane;}
//获取画框,使用对话框和菜单的程序要直接基于画框本身
public static JFrame getMainFrame(){return frame;}
class MouseTest
{
public MouseTest()
{
MouseFrame frame1=new MouseFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.show();
}
}
/**
A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(300,200);
//add panel to frame
MousePanel panel=new MousePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
/**
A panel with mouse operations for adding and removing squares
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares=new ArrayList();
current=null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//draw all squares
for (int i=0;i<squares.size() ;i++ )
g2.draw((Rectangle2D)squares.get(i));
}
/**
Finds the first square containing a point
@param p a point
@return the index of the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (int i=0;i<squares.size() ;i++ )
{
Rectangle2D r=(Rectangle2D)squares.get(i);
if (r.contains(p)) return r;
}
return null;
}
/**
Adds a square to the collection.
@param p the center of the square
*/
public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();
current =new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,
SIDELENGTH,SIDELENGTH);
squares.add(current);
repaint();
}
/**
removes a square from the collection.
@param s the square to remove
*/
public void remove(Rectangle2D s)
{
if(s==null) return;
if(s==current) current=null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH=10;
private ArrayList squares;
private Rectangle2D current;
//the square containing the mouse cursor
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
//add a new square if the cursor isn't inside a square
current=find(event.getPoint());
if (current==null)
add(event.getPoint());
}
public void mouseClicked(MouseEvent event)
{
//remove the current square if double clicked
current =find(event.getPoint());
if (current !=null && event.getClickCount()>=2)
remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
//set the mouse cursor to cross hairs if it is inside a rectangle
if (find(event.getPoint())==null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event)
{
if (current !=null)
{
int x=event.getX();
int y=event.getY();
//drag the current rectangle to center it at(x,y)
current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2,
SIDELENGTH,SIDELENGTH);
repaint();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -